question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ArgumentParser changes between 0.9.11 and 0.9.21

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kmvanbruntcommented, Dec 4, 2019

The with_argparser decorator is used on the command’s do_xxx function.

1reaction
kmvanbruntcommented, Dec 4, 2019

@dannypete The class is still being defined at that point, so it can’t be referenced by class name. The same principle is causing the error with your choices function. It needs to be defined before you refer to it.

This should work:

class MyClass(cmd2.Cmd):
    def _myarg_choices_function(self):
        return self.my_arg_choices

    parser = argparse_custom.Cmd2ArgumentParser()
    parser.add_argument("my_arg", choices_method=_myarg_choices_function)
    ...
Read more comments on GitHub >

github_iconTop Results From Across the Web

cmd2/CHANGELOG.md at master · python-cmd2/cmd2 - GitHub
CompletionItems now saves the original object from which it creates a string. Using CompletionItems as argparse choices is fully supported. cmd2 patched ...
Read more >
Argument Processing — cmd2 2.4 documentation
This changes the second argument to the command method, which will contain the results of ArgumentParser.parse_args() . Here's what it looks like: from...
Read more >
easydev Changelog - pyup.io
0.9.8. * CHANGES: * remove ordereddict package in the setup (not used) * remove appdirs from install_requires in the setup.py file. And add...
Read more >
dnslib - PyPI
A library to encode/decode DNS wire-format packets supporting both Python 2.7 and Python 3.2+. The library provides: Support for encoding/decoding DNS packets ...
Read more >
Argparse4j - The Java command-line argument parser library ...
The documentation describes the change and how to migrate from earlier versions. There are still missing features which exist in argparse but not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found