Is there an easy way to add arguments with values to python3‘s cmd.Cmd?
For example, it’s easy to implement commands with simple parameters:
> action1 param1 param2
by adding do_action1() and complete_action1(), to complete I can search the list of existing parameters.
But how would you implemented parameters with values, for example:
> action1 param1=234 param2=SomeTextValue
or
> action1 param1 234 param2 SomeTextValue
where param1 would be set to 234 and param2 to SomeTextValue.
Can this be parsed by cmd.Cmd?
I can think only about getting the whole argument list in do_action1( self, params = None ) and then parse it myself.
If I use optparse the parameters would have to be prefixed with dash like -p or --param1 and --param2, so to get completion in cmd.Cmd I would have to type 2 dashes first …
> action1 --param1=234 --param2=SomeTextValue
If I have to manually parse the parameters is there any python3 module like optparse which does not expect the parameters to have 2 dash prefix?
Any suggestions?
Well,
cmd.Cmddoesn’t actually do a whole lot of parsing for you, anyway. So, yes, you can handle parameters with arguments simply by completing parameters with a trailing=, and parsing the commands yourself:Example usage:
Note that our completions contain a trailing
=as a hint to the user, to let them know a parameter can be passed.