Python argparse: Default value or Specified value

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# Table of Contents

  1. Python argparse: Default value or Specified value
  2. Using the default value even when the argument name is not supplied
  3. Python argparse: Default Integer value or Specified value

# Python argparse: Default value or Specified value

To set a default value for a command-line argument when one is not supplied when using argparse:

  1. Set the nargs argument to "?".
  2. Set the const argument to the default value.
main.py
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)
The code for this article is available on GitHub

Here are some examples of running the Python program.

shell
# ๐Ÿ‘‡๏ธ melon python main.py --fruit melon # ๐Ÿ‘‡๏ธ apple python main.py --fruit # ๐Ÿ‘‡๏ธ None python main.py

python argparse default value or specified value

When the nargs argument is set to a question mark "?", one argument is consumed from the command line if supplied.

shell
# ๐Ÿ‘‡๏ธ melon python main.py --fruit melon

If the argument is not passed at all, None is returned.

shell
# ๐Ÿ‘‡๏ธ None python main.py

If the argument name is supplied, but a value is not, the value of the const argument is returned.

shell
# ๐Ÿ‘‡๏ธ 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.

main.py
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.

# Using the default value even when the argument name is not supplied

If you want to return the default value even if the argument name is not supplied, set the default argument when calling parser.add_argument().

main.py
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)
The code for this article is available on GitHub

Here are some examples of running the Python program.

shell
# ๐Ÿ‘‡๏ธ apple python main.py # ๐Ÿ‘‡๏ธ melon python main.py --fruit melon # ๐Ÿ‘‡๏ธ apple python main.py --fruit

The default value is used when:

  1. The user passes the argument name (--fruit in the example).
shell
# ๐Ÿ‘‡๏ธ apple python main.py --fruit
  1. The user doesn't supply the argument at all.
shell
# ๐Ÿ‘‡๏ธ apple python main.py

If the user supplies a value for the argument, the given value is used.

shell
# ๐Ÿ‘‡๏ธ melon python main.py --fruit melon

You can also set the const and default arguments to different values.

main.py
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.

shell
# ๐Ÿ‘‡๏ธ 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.

shell
# ๐Ÿ‘‡๏ธ pear python main.py

When the user passes only the argument name, the const value is used.

shell
# ๐Ÿ‘‡๏ธ apple python main.py --fruit

When the user passes the argument name and value, the supplied value is used.

shell
# ๐Ÿ‘‡๏ธ melon python main.py --fruit melon

# Python argparse: Default Integer value or Specified value

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.

main.py
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)
The code for this article is available on GitHub

Notice that we set type to int.

Here are some examples of running the program.

shell
# ๐Ÿ‘‡๏ธ 0 python main.py # ๐Ÿ‘‡๏ธ 0 python main.py --number # ๐Ÿ‘‡๏ธ 100 python main.py --number 100

integer default value or specified value

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).

shell
# ๐Ÿ‘‡๏ธ 0 python main.py

If the user passes the argument name, 0 is returned (const is used).

shell
# ๐Ÿ‘‡๏ธ 0 python main.py --number

If the user passes the argument name and value, the supplied value is used.

shell
# ๐Ÿ‘‡๏ธ 100 python main.py --number 100

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev