Skip to main content

How to use Python argparse: Default Values, Optional Arguments, and nargs='?'

Python's argparse module provides a powerful way to create command-line interfaces.

This guide explains how to use nargs='?', const, and default in argparse to create optional arguments that have default values if the argument is either not provided at all, or is provided without a value.

Understanding nargs='?', const, and default

When working with argparse, these three arguments are often used together to define optional arguments with defaults:

  • nargs='?': This makes the command-line argument optional.
    • If the argument is provided with a value (e.g., --fruit banana), that value is used.
    • If the argument is provided without a value (e.g., --fruit), the value from const is used.
    • If the argument is not provided at all (e.g., no --fruit), the value from default is used.
  • const: The value to use if the argument is present on the command line but without a value. This is only relevant when nargs='?'.
  • default: The value to use if the argument is not present on the command line at all.

Example: Optional String Argument with Default

import argparse

parser = argparse.ArgumentParser(
description='A sample Argument Parser.'
)

parser.add_argument('-f',
'--fruit',
nargs='?', # Argument is optional
const='apple', # Value if --fruit is present but has no value
default='apple', # Value if --fruit is NOT present
type=str
)

args = parser.parse_args()
print(args.fruit)

Let's break down the possible command-line invocations and their results:

Command LineOutputExplanation
python main.pyapple--fruit is not present, so default='apple' is used.
python main.py --fruitapple--fruit is present, but no value is given, so const='apple' is used.
python main.py --fruit bananabanana--fruit is present and a value is given, so the provided value (banana) is used. const and default are ignored.
  • If the default value is not specified, and the user does not pass any value when calling the script, then the default value will be None.

Example: Optional Integer Argument with Default

import argparse

parser = argparse.ArgumentParser(
description='A sample Argument Parser.'
)

parser.add_argument('-n',
'--number',
nargs='?',
const=0, # Value if --number is present but has no value
default=0, # Value if --number is NOT present
type=int
)

args = parser.parse_args()
print(args.number)

The command line behavior will be analogous to the string example:

Command LineOutputExplanation
python main.py0--number is not present; default=0 is used.
python main.py --number0--number is present, no value; const=0 is used.
python main.py --number 100100--number is present with a value; that value is used.

Example: Different const and default values.

It is also possible for the const and default arguments to have 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)

In the example above:

  • If the user doesn't pass the argument at all, default value pear is used.
  • If the user provides only the flag -f, then the const value apple is used.
  • If the user provides a value to the flag -f value, then that value is used.