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.
argument-groups
won't help. They affect thehelp
display, but don't affect parsing. Testing after parsing as FMc does is the way to ensure one of the 3 arguments is given. Mutually exclusively groups can be used to prevent more than 1 of the group being used, but the OP wants 'at least one'.