cmd2
cmd2 copied to clipboard
[1.5.0] Is it possible to provide a `type` callable access to `self` from within a command set, similar to how `choices_method` works?
I'd like to run some type conversion on a few arguments inside a command set, but it needs access to self. If I simply add the methods as the type argument in parser.add_argument I always get an invalid value warning from the parser without that callable having even been called.
I assume this is caused by the fact that the type callable requires there to be one argument, but if using a method, self must be provided?
Since the parser is created before the CommandSet instance, type is pointing at an unbound method.
This is the same for tab completion methods like choice_provider. Command parsers are basically class objects, not instance objects.
cmd2 gets around this for tab completion methods since they're called in our custom code and we pass self manually as the first argument to the unbound method. All the type stuff is run by argparse, and it's basically just used as a cast. See https://github.com/python/cpython/blob/3.10/Lib/argparse.py#L2484.
If you want argparse to call the bounded type method, then you need to set type after the CommandSet instance is created. That way the method will be bounded to an instance and self is already populated when called.
Here is 1 option.
class MyCommandSet(CommandSet):
def __init__(self) -> None:
super().__init__()
# Set the argument's type using the bounded method
MyCommandSet.saved_arg.type = self.custom_type
parser = Cmd2ArgumentParser("Example")
# Save this argument so we can edit it later in __init__()
saved_arg = parser.add_argument(..., choices_method=my_choices)
@with_argparser(parser)
def do_blah(self, args: argparse.Namespace):
...
Just to clarify, you've got saved_arg but then in the __init__ you're setting the type of MyCommandSet.my_arg. This is just a typo, right?
@ddejohn Yes, that is a typo. I've corrected the code now.