Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The python's argparse errors

where report this error : TypeError: 'Namespace' object is not iterable

import argparse

def parse_args():
    parser = argparse.ArgumentParser(add_help=True)
    parser.add_argument('-a', '--aa', action="store_true", default=False)
    parser.add_argument('-b', action="store", dest="b")
    parser.add_argument('-c', action="store", dest="c", type=int)

    return parser.parse_args()

def main():
    (options, args) = parse_args()

if __name__ == '__main__':
    main()
like image 519
Paul Avatar asked Sep 29 '13 10:09

Paul


People also ask

What is Argparse used for in Python?

You can use the argparse module to write a command-line interface that accepts a positional argument. Positional arguments (as opposed to optional arguments, which we'll explore in a subsequent section), are generally used to specify required inputs to your program.

Is Argparse a standard Python library?

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 click instead of Argparse?

Click actually implements its own parsing of arguments and does not use optparse or argparse following the optparse parsing behavior. The reason it's not based on argparse is that argparse does not allow proper nesting of commands by design and has some deficiencies when it comes to POSIX compliant argument handling.


1 Answers

Your issue has to do with this line:

(options, args) = parse_args()

Which seems to be an idiom from the deprecated "optparse".

Use the argparse idiom without "options":

import argparse
parser = argparse.ArgumentParser(description='Do Stuff')
parser.add_argument('--verbosity')
args = parser.parse_args()
like image 101
user2688272 Avatar answered Oct 10 '22 12:10

user2688272