Python3 provides
| Module | Function | Usage |
|---|---|---|
| argparse | <Argument_Parser> . add_argument ( agrument name or flags, … ) | defines the parsing of the command line arguments. |
The add_argument function expects an argument name ( positional argument ) or a series of flags ( optional argument ). Optional arguments are identified by the ( - ) prefix.
example.py
import argparse
def main() :
arg_parser = argparse.ArgumentParser(description = 'Enter user details')
# Add the arguments
# Add positional argument
arg_parser.add_argument('alfa')
# Add optional argument
arg_parser.add_argument('-p', '--particle', default = 'Electron')
# Add positional argument
arg_parser.add_argument('beta')
args = arg_parser.parse_args()
print ("Arguments passed : ", end = '')
print ((args.alfa, args.particle, args.beta))
if __name__ == "__main__" :
main()
Output
$ python3 example.py --help
usage: example.py [-h] [-p PARTICLE] alfa beta
Enter user details
positional arguments:
alfa
beta
optional arguments:
-h, --help show this help message and exit
-p PARTICLE, --particle PARTICLE
$ python3 example.py
usage: exmaple.py [-h] [-p PARTICLE] alfa beta
example.py: error: the following arguments are required: alfa, beta
$ python3 example.py arg_alfa arg_beta
Arguments passed : ('arg_alfa', 'Electron', 'arg_beta')
$ python3 example.py arg_alfa arg_beta -p Photon
Arguments passed : ('arg_alfa', 'Photon', 'arg_beta')
example.py
import argparse
from pathlib import Path
def main() :
arg_parser = argparse.ArgumentParser(description='Enter user details')
# Add the arguments
# Required argument
arg_parser.add_argument('-u',
'--user',
help = 'Name of user.',
required = True)
# Accepting multiple arguments
arg_parser.add_argument('-s',
'--subscription',
help = 'Online subscription',
action = 'append',
required = True)
args = arg_parser.parse_args()
print ("Arguments passed : ", end = '')
print ((args.user, args.subscription))
if __name__ == "__main__" :
main()
Output
$ python3 example.py --help
usage: example.py [-h] -u USER -s SUBSCRIPTION
Enter user details
optional arguments:
-h, --help show this help message and exit
-u USER, --user USER Name of user.
-s SUBSCRIPTION, --subscription SUBSCRIPTION
Online subscription
$ python3 example.py
usage: example.py [-h] -u USER -s SUBSCRIPTION
example.py: error: the following arguments are required: -u/--user, -s/--subscription
$ python3 example.py -u Deltoid -s Netflix -s 'Amazon Prime' -s 'Disney'
Arguments passed : ('Deltoid', ['Netflix', 'Amazon Prime', 'Disney'])
example.py
import argparse
from pathlib import Path
def main() :
arg_parser = argparse.ArgumentParser(description = 'Enter user details')
# Required argument
arg_parser.add_argument('-u',
'--user',
help = 'Name of user.',
required = True)
# Required type of the argument set to int
arg_parser.add_argument('-a',
'--age',
help = 'User\'s age (integer).',
type = int,
required = True)
# Required type set to float
arg_parser.add_argument('-w',
'--weight',
help = 'User\'s weight (float).',
type = float,
required = True)
arg_parser.add_argument('-k',
'--public_key',
help = 'User\'s ssh public key.',
type = Path,
required = True)
args = arg_parser.parse_args()
print ("Arguments passed : ", end = '')
print ((args.user, args.age, args.weight, args.public_key))
if __name__ == "__main__" :
main()
Output
$ python3 example.py --help
usage: example.py [-h] -u USER -a AGE -w WEIGHT -k PUBLIC_KEY
Enter user details
optional arguments:
-h, --help show this help message and exit
-u USER, --user USER Name of user.
-a AGE, --age AGE User's age (integer).
-w WEIGHT, --weight WEIGHT
User's weight (float).
-k PUBLIC_KEY, --public_key PUBLIC_KEY
User's ssh public key.
$ python3 example.py --age 10a
usage: example.py [-h] -u USER -a AGE -w WEIGHT -k PUBLIC_KEY
example.py: error: argument -a/--age: invalid int value: '10a'
$ python3 example.py -u noddy --age 10 -w 20 -k ~/.ssh/id_rsa.pub
Arguments passed : ('noddy', 10, 20.0, PosixPath('/home/algotree/.ssh/id_rsa.pub'))
© 2019-2026 Algotree.org | All rights reserved.
This content is provided for educational purposes. Feel free to learn, practice, and share knowledge.
For questions or contributions, visit algotree.org
"Weeks of coding can save you hours of planning."