17

The following code, using argparse's subparsers, fails on Python 3 but runs as expected in Python 2. After comparing the docs, I still can't tell why.

#!/usr/bin/env python
from __future__ import print_function
from argparse import ArgumentParser


def action(args):
    print(args)

if __name__ == '__main__':
    std = ArgumentParser(add_help=False)
    std.add_argument('standard')

    ap = ArgumentParser()
    sp = ap.add_subparsers()

    cmd = sp.add_parser('subcommand', parents=[std], description='Do subcommand')
    cmd.add_argument('arg')
    cmd.set_defaults(do=action)

    args = ap.parse_args()
    args.do(args)

The output from Python 2.7.6 is:

me@computer$ python test.py 
usage: test.py [-h] {subcommand} ...
test.py: error: too few arguments

In Python 3.3.5, I get:

me@computer$ python3 test.py 
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    args.do(args)
AttributeError: 'Namespace' object has no attribute 'do'
3
  • Note that if you use a subparser: args = cmd.parse_args() it would work. Commented Apr 10, 2014 at 14:39
  • This code seems to give the same error for me on Python 2.7.4. Most likely you are running the wrong file version or something. It should not work. Try it more carefully again. Commented Apr 10, 2014 at 14:40
  • When I type your code in at a Python 2.7.6 interpreter, I get the same error at the line args = ap.parse_args(). Commented Apr 10, 2014 at 14:41

1 Answer 1

26

the latest argparse release changed how it tested for required arguments, and subparsers fell through the cracks. They are no longer 'required'. http://bugs.python.org/issue9253#msg186387

When you get test.py: error: too few arguments, it's objecting that you did not give it a 'subcommand' argument. In 3.3.5 it makes it past that step, and returns args.

With this change, 3.3.5 should behave the same as earlier versions:

ap = ArgumentParser()
sp = ap.add_subparsers(dest='parser')  # dest needed for error message
sp.required = True   # force 'required' testing

Note - both dest and required need to be set. dest is needed to give this argument a name in the error message.


This error:

AttributeError: 'Namespace' object has no attribute 'do'

was produced because the cmd subparser did not run, and did not put its arguments (default or not) into the namespace. You can see that effect by defining another subparser, and looking at the resulting args.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that nailed it. In case there was any wonder, this solution is backward-compatible with Python 2.7
python 3.8.2. I tried to set the required attribute, but got a traceback instead of a friendly help message. File "/usr/lib/python3.8/argparse.py", line 2035, in _parse_known_args ', '.join(required_actions)) TypeError: sequence item 0: expected str instance, NoneType found
@tobixen, see also stackoverflow.com/q/23349349/901925. Did you set the dest?
No, didn't try dest (hm, it's written plainly in the answer above that dest is required, strange I didn't see it). I tried setting required=True without success. Ended up doing a workaround - github.com/tobixen/calendar-cli/commit/… - perhaps I'm doing this in the wrong way, but ... it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.