Skip to content

[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

Closed

Description

@ddejohn

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?

Activity

  1. kmvanbrunt commented on Jul 22, 2022

    @kmvanbrunt
    Member

    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):
           ...
  2. ddejohn commented on Jul 22, 2022

    @ddejohn
    Author

    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?

  3. kmvanbrunt commented on Jul 22, 2022

    @kmvanbrunt
    Member

    @ddejohn Yes, that is a typo. I've corrected the code now.

  4. locked and limited conversation to collaborators on Sep 12, 2022
  5. converted this issue into a discussion #1247 on Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions