Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this argparse code behave differently between Python 2 and 3?

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'
like image 621
Wedgwood Avatar asked Apr 10 '14 14:04

Wedgwood


People also ask

How does Argparse work in Python?

The standard Python library argparse used to incorporate the parsing of command line arguments. Instead of having to manually set variables inside of the code, argparse can be used to add flexibility and reusability to your code by allowing user input values to be parsed and utilized.

Why do we use Argparse in Python?

The argparse module in Python helps create a program in a command-line-environment in a way that appears not only easy to code but also improves interaction. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

Is Argparse a standard?

The argparse is a standard module; we do not need to install it. A parser is created with ArgumentParser and a new parameter is added with add_argument . Arguments can be optional, required, or positional.


1 Answers

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.

like image 72
hpaulj Avatar answered Oct 04 '22 11:10

hpaulj