Skip to content

ArgumentParser changes between 0.9.11 and 0.9.21 #828

@dannypete

Description

@dannypete

Hello!

I'm trying to understand the differences in how ArgumentParsers are defined and used in this new form as I am trying to update a project.

My cmd2 app (using cmd2 0.9.11) uses the following structure:

def MyClass(cmd2.Cmd):
...

    parser = argparse_completer.ACArgumentParser()
    setattr(filter_create_sp.add_argument("my_arg"),
              argparse_completer.ACTION_ARG_CHOICES, ("_my_completer_function",))

    @cmd2.with_argparser(parser)
    def do_command(self, args):
        ...

where _my_completer_function is a method of the MyClass class.


My understanding is that the new format of this code is something along the lines of:

def MyClass(cmd2.Cmd):
...

    parser = cmd2.argparse_custom.Cmd2ArgumentParser()
    parser.add_argument("my_arg", choices_method=self._my_completer_function)

    @cmd2.with_argparser(parser)
    def do_command(self, args):
        ....

Unfortunately, I cannot work out how to pass/use self(or other reference to the instance of my cmd2 app) for the parser.add_argument("my_arg", choices_method=self._my_completer_function) line. I need the MyClass instance to be passed to my custom completer function. Am I missing something obvious?

Thanks in advance.

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