ArgumentParser changes between 0.9.11 and 0.9.21
See original GitHub issueHello!
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:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The
with_argparserdecorator is used on the command’s do_xxx function.@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: