Parameters can be required or optional depending on how they appear in the function definition:
def myfunction(p1, p2, p3=False, p4=5)
In this definition, parameters p1 and p2 are required. p3 is optional and will acquire the value False if not provided by the caller. p4 is also optional and will acquire the value 5 if not provided.
If you really need to pass ten or more parameters into a function, it might be better to pass them as a dictionary:
args = {'a': something, 'b': something_else, 'c': 5, 'd': 99.99, ... }
myfunc(args)
Answer from John Gordon on Stack Overflowpython - Argparse: Required arguments listed under "optional arguments"? - Stack Overflow
Argparse - Optional in conditional
Missing required positional argument in class?
I'm getting this error even though the arguments are in "__init__() missing 3 required positional arguments: 'make', 'model', and 'year'
The error is because the arguments are missing in line 24. It should be:
C = Car("Toyota", "Corolla", "2005")
C.get_make()
C.get_model()
C.get_year() More on reddit.com Parameters can be required or optional depending on how they appear in the function definition:
def myfunction(p1, p2, p3=False, p4=5)
In this definition, parameters p1 and p2 are required. p3 is optional and will acquire the value False if not provided by the caller. p4 is also optional and will acquire the value 5 if not provided.
If you really need to pass ten or more parameters into a function, it might be better to pass them as a dictionary:
args = {'a': something, 'b': something_else, 'c': 5, 'd': 99.99, ... }
myfunc(args)
If a function parameter is not required, you can set it equal to None.
def hello(first_name, last_name=None):
print "Hello " + first_name
Or as John Gordon said, you can set a parameter with a default value the same way.
Note that optional parameters need to be defined after the required parameters.
Parameters starting with - or -- are usually considered optional. All other parameters are positional parameters and as such required by design (like positional function arguments). It is possible to require optional arguments, but this is a bit against their design. Since they are still part of the non-positional arguments, they will still be listed under the confusing header “optional arguments” even if they are required. The missing square brackets in the usage part however show that they are indeed required.
See also the documentation:
In general, the argparse module assumes that flags like -f and --bar indicate optional arguments, which can always be omitted at the command line.
Note: Required options are generally considered bad form because users expect options to be optional, and thus they should be avoided when possible.
That being said, the headers “positional arguments” and “optional arguments” in the help are generated by two argument groups in which the arguments are automatically separated into. Now, you could “hack into it” and change the name of the optional ones, but a far more elegant solution would be to create another group for “required named arguments” (or whatever you want to call them):
parser = argparse.ArgumentParser(description='Foo')
parser.add_argument('-o', '--output', help='Output file name', default='stdout')
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-i', '--input', help='Input file name', required=True)
parser.parse_args(['-h'])
usage: [-h] [-o OUTPUT] -i INPUT
Foo
optional arguments:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
Output file name
required named arguments:
-i INPUT, --input INPUT
Input file name
Since I prefer to list required arguments before optional, I hack around it via:
parser = argparse.ArgumentParser()
parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
optional = parser.add_argument_group('optional arguments')
required.add_argument('--required_arg', required=True)
optional.add_argument('--optional_arg')
return parser.parse_args()
and this outputs:
usage: main.py [-h] --required_arg REQUIRED_ARG [--optional_arg OPTIONAL_ARG]
required arguments:
--required_arg REQUIRED_ARG
optional arguments:
--optional_arg OPTIONAL_ARG
I can live without -h, --help showing up in the optional arguments group.