I'm trying to learn argparse in order to use it in my program, the syntax should be like this:
-a --aLong <String> <String>
-b --bLong <String> <String> <Integer>
-c --cLong <String>
-h --help
I have this code:
#!/usr/bin/env python
#coding: utf-8
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Lorem Ipsum')
parser.add_argument('-a','--aLong', help='Lorem Ipsum', required=False)
parser.add_argument('-b','--bLong', help='Lorem Ipsum', required=False)
parser.add_argument('-c','--cLong', help='Lorem Ipsum', required=False)
parser.add_argument('-h','--help', help='Lorem Ipsum', required=False)
parser.parse_args()
The question is, I read in the official doc, saw YouTube videos, etc, but I couldn't understand how can I determine the number of "sub-arguments" of the "main-argument"?
Example: myApp.py -b Foobar 9000
, how can I set that -b
must have two "sub-arguments", and how can I get the values, Foobar
and 9000
?
And another doubt, I know I can set an argument to be required
or not, but I wanted to make my program only executes when at least one argument is passed, any of the four mentioned.
Maybe it's a stupid question, but sorry, I can't understand it, and hopefully there is someone here with "teacher powers" to explain it.
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.
As of Python >= 2.7 and >= 3.2, the argparse module is maintained within the Python standard library. For users who still need to support Python < 2.7 or < 3.2, it is also provided as a separate package, which tries to stay compatible with the module in the standard library, but also supports older Python versions.
To add your arguments, use parser. add_argument() . Some important parameters to note for this method are name , type , and required . The name is exactly what it sounds like — the name of the command line field.
import argparse
# Use nargs to specify how many arguments an option should take.
ap = argparse.ArgumentParser()
ap.add_argument('-a', nargs=2)
ap.add_argument('-b', nargs=3)
ap.add_argument('-c', nargs=1)
# An illustration of how access the arguments.
opts = ap.parse_args('-a A1 A2 -b B1 B2 B3 -c C1'.split())
print(opts)
print(opts.a)
print(opts.b)
print(opts.c)
# To require that at least one option be supplied (-a, -b, or -c)
# you have to write your own logic. For example:
opts = ap.parse_args([])
if not any([opts.a, opts.b, opts.c]):
ap.print_usage()
quit()
print("This won't run.")
The key to this is to define a required, mutually exclusive group.
import argparse
# Use nargs to specify how many arguments an option should take.
ap = argparse.ArgumentParser()
group = ap.add_mutually_exclusive_group(required=True)
group.add_argument('-a', nargs=2)
group.add_argument('-b', nargs=3)
group.add_argument('-c', nargs=1)
# Grab the opts from argv
opts = ap.parse_args()
# This line will not be reached if none of a/b/c are specified.
# Usage/help will be printed instead.
print(opts)
print(opts.a)
print(opts.b)
print(opts.c)
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