Pythonista-Issues
Pythonista-Issues copied to clipboard
argparse not working from the interactive console
The following lines, that are very simple and comply with the documentation (as far as I now), do not work from the interactive console of Pythonista:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', action='store_true')
parser.parse_args(['--foo'])
The error message is:
error: unrecognized arguments: --foo
However, they work when put within a script, and also from the IPython console of Spyder.
So I think this is a Pythonista-specific bug.
What's more, when defining the parser in a module function, as follows:
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-f', action='store_true')
parser.add_argument('--foo', action='store_true')
parser.parse_args()
and then setting sys.argv to:
['', '-f', '--foo']
and finally calling the function from the console with:
<module>.parse_args()
the -f argument is parsed correctly but I get an error for the long-name argument:
error: unrecognized arguments: --foo
Does that code work on the Linux, macOS, or Windows interactive console?
Yes, it works on the normal console of:
Python 3.7.4 (default, Aug 13 2019, 15:17:50) [Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
where it returns: Namespace(foo=True)
and also work in the IPython console of Spyder 3.3.6 / Python 3.7.0
both on MacOS