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()
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.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With