Last updated: Apr 13, 2024
Reading timeยท3 min

To set a default value for a command-line argument when one is not supplied when using argparse:
nargs argument to "?".const argument to the default value.import argparse parser = argparse.ArgumentParser( description='A sample Argument Parser.' ) parser.add_argument('-f', '--fruit', nargs='?', const='apple', type=str ) args = parser.parse_args() print(args.fruit)
Here are some examples of running the Python program.
# ๐๏ธ melon python main.py --fruit melon # ๐๏ธ apple python main.py --fruit # ๐๏ธ None python main.py

When the nargs argument
is set to a question mark "?", one argument is consumed from the command line
if supplied.
# ๐๏ธ melon python main.py --fruit melon
If the argument is not passed at all, None is returned.
# ๐๏ธ None python main.py
If the argument name is supplied, but a value is not, the value of the const
argument is returned.
# ๐๏ธ apple python main.py --fruit
In other words, just like with regular expressions, the question mark "?"
means "0 or 1 arguments".
The value for the const argument is used when the argument name is supplied,
but a value is not given.
parser.add_argument('-f', '--fruit', nargs='?', const='apple', type=str )
We set the type argument to str to take string values as arguments but you
can set type=int if you need to take integer values.
default value even when the argument name is not suppliedIf you want to return the default value even if the argument name is not
supplied, set the default argument when calling parser.add_argument().
import argparse parser = argparse.ArgumentParser( description='A sample Argument Parser.' ) parser.add_argument('-f', '--fruit', nargs='?', const='apple', default='apple', type=str ) args = parser.parse_args() print(args.fruit)
Here are some examples of running the Python program.
# ๐๏ธ apple python main.py # ๐๏ธ melon python main.py --fruit melon # ๐๏ธ apple python main.py --fruit
The default value is used when:
--fruit in the example).# ๐๏ธ apple python main.py --fruit
# ๐๏ธ apple python main.py
If the user supplies a value for the argument, the given value is used.
# ๐๏ธ melon python main.py --fruit melon
You can also set the const and default arguments to different values.
import argparse parser = argparse.ArgumentParser( description='A sample Argument Parser.' ) parser.add_argument('-f', '--fruit', nargs='?', const='apple', default='pear', type=str ) args = parser.parse_args() print(args.fruit)
Here are some examples of running the Python program.
# ๐๏ธ pear python main.py # ๐๏ธ apple python main.py --fruit # ๐๏ธ melon python main.py --fruit melon
When the user doesn't pass the argument at all, the default value is used.
# ๐๏ธ pear python main.py
When the user passes only the argument name, the const value is used.
# ๐๏ธ apple python main.py --fruit
When the user passes the argument name and value, the supplied value is used.
# ๐๏ธ melon python main.py --fruit melon
For completeness' sake, let's look at an example, let's look at an example of using a default integer value or the specified value.
import argparse parser = argparse.ArgumentParser( description='A sample Argument Parser.' ) parser.add_argument('-n', '--number', nargs='?', const=0, default=0, type=int ) args = parser.parse_args() print(args.number)
Notice that we set type to int.
Here are some examples of running the program.
# ๐๏ธ 0 python main.py # ๐๏ธ 0 python main.py --number # ๐๏ธ 100 python main.py --number 100

We set the nargs argument to "?" and the const and default arguments to
0.
If the user doesn't supply the --number argument at all, 0 is returned
(default is used).
# ๐๏ธ 0 python main.py
If the user passes the argument name, 0 is returned (const is used).
# ๐๏ธ 0 python main.py --number
If the user passes the argument name and value, the supplied value is used.
# ๐๏ธ 100 python main.py --number 100
You can learn more about the related topics by checking out the following tutorials: