Pythonista-Issues icon indicating copy to clipboard operation
Pythonista-Issues copied to clipboard

argparse not working from the interactive console

Open jmmelko opened this issue 5 years ago • 3 comments
trafficstars

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.

jmmelko avatar Sep 17 '20 19:09 jmmelko

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

jmmelko avatar Sep 17 '20 19:09 jmmelko

Does that code work on the Linux, macOS, or Windows interactive console?

cclauss avatar Sep 17 '20 19:09 cclauss

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

jmmelko avatar Sep 17 '20 19:09 jmmelko