May-02-2022, 01:03 PM
You could use
Also, arguments with more than one parameter (nargs) are possible.
ArgumentParser
, which returns a Namespace
, where you can access for example to answer.from argparse import ArgumentParser ANSWERS = ("n", "y") def get_args(): parser = ArgumentParser() parser.add_argument("--answer", dest="ANSWER", metavar="YOUR_CHOICE", default="n", choices=ANSWERS) return parser.parse_args() if __name__ == "__main__": print(get_args())I named the script c.py:
Quote:[andre@andre-Fujitsu-i5 ~]$ python c.py -h
usage: c.py [-h] [--answer YOUR_CHOICE]
options:
-h, --help show this help message and exit
--answer YOUR_CHOICE
[andre@andre-Fujitsu-i5 ~]$ python c.py --answer y
Namespace(ANSWER='y')
[andre@andre-Fujitsu-i5 ~]$ python c.py --answer i
usage: c.py [-h] [--answer YOUR_CHOICE]
c.py: error: argument --answer: invalid choice: 'i' (choose from 'y', 'n')
Also, arguments with more than one parameter (nargs) are possible.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!