I’m writing a new CLI with pyton and using cmd and argparse libraries. When i set a argument and call it with a long text option it does not recognize it was a text and throws an exception.
Here is how i use arguments:
WACP:~ cli$ add -t group -n NameForNewGroup -desc 'Description for new group' -c 2
Response is:
unrecognized arguments: - t g r o u p - n N a m e F o r N e w G r o u p - d e s c ' D e s c r i p t i o n f o r n e w g r o u p ' - c 2
Here how i add arguments:
self.parser.add_argument('-t', '--type',
dest='type',
help=Language.MSG_ADD_TYPE_HELP,
action='store',
default='device'
)
self.parser.add_argument('-c', '--config',
dest='config',
help=Language.MSG_ADD_GROUP_HELP,
action='store',
default=0
)
self.parser.add_argument('-n', '--name',
dest='name',
help=Language.MSG_ADD_NAME_HELP,
action='store',
default='New Device'
)
self.parser.add_argument('-desc', '--description',
dest='description',
help=Language.MSG_ADD_DESC_HELP,
type=complex,
action='store',
default='Default description for device or group included in configuration values')
I looked argparse documentation and did not configure how it could be.
I have to handle this long text parsing and make it work.
Is there anyone has an idea to solve that?
Works for me. Maybe you can produce a minimal test case? What version of Python? Are you sure you’re using
argparseand notoptparse? Are you callingparse_args()on the right object? What’s the output of-h?When run:
Note
Normally, if you have short (e.g.
-t) and long (e.g.--type) arguments, the short arguments should be one character each. So-descis a bit weird.You don’t want
type=complex. Thecomplextype is used for complex numbers, such as1+0.5j.The default action is
store, so you can omit it.