You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[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? #1243
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.
classMyCommandSet(CommandSet):
def__init__(self) ->None:
super().__init__()
# Set the argument's type using the bounded methodMyCommandSet.saved_arg.type=self.custom_typeparser=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)defdo_blah(self, args: argparse.Namespace):
...
Activity
kmvanbrunt commentedon Jul 22, 2022
Since the parser is created before the
CommandSetinstance,typeis 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.cmd2gets around this for tab completion methods since they're called in our custom code and we passselfmanually as the first argument to the unbound method. All thetypestuff is run byargparse, 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
argparseto call the boundedtypemethod, then you need to settypeafter theCommandSetinstance is created. That way the method will be bounded to an instance andselfis already populated when called.Here is 1 option.
ddejohn commentedon Jul 22, 2022
Just to clarify, you've got
saved_argbut then in the__init__you're setting the type ofMyCommandSet.my_arg. This is just a typo, right?kmvanbrunt commentedon Jul 22, 2022
@ddejohn Yes, that is a typo. I've corrected the code now.