Last modified: Apr 10, 2026 By Alexander Williams

Python Argparse Store True: Boolean Flags Guide

Command-line interfaces make scripts powerful. They let users control program behavior. Python's argparse module is the standard tool for building them. One of its most useful features is store_true.

This article explains store_true. You will learn what it is, how it works, and when to use it. We include clear code examples and outputs.

What is Argparse Store True?

The add_argument() method in argparse defines a command-line argument. The action parameter controls what happens when the argument is parsed. Setting action='store_true' creates a boolean flag.

A boolean flag is an optional argument. When the user includes it on the command line, its value becomes True. If they omit it, the value is False. It's perfect for enabling or disabling features.

This is different from store_false, which sets the value to False when the flag is present. For a broader look at optional arguments, see our Python Argparse Optional Arguments Guide.

Basic Store True Example

Let's start with a simple script. We will add a --verbose flag. When used, it prints extra information.


import argparse

# Create the parser
parser = argparse.ArgumentParser(description="A sample script with a verbose flag.")

# Add a store_true argument
parser.add_argument('--verbose', action='store_true', help='enable verbose output')

# Parse the arguments
args = parser.parse_args()

# Use the flag value
if args.verbose:
    print("Verbose mode is ON. Starting detailed logging...")
else:
    print("Running in normal mode.")
    

Save this as script.py. Now run it from the terminal.


# Run without the flag
$ python script.py
Running in normal mode.

# Run with the flag
$ python script.py --verbose
Verbose mode is ON. Starting detailed logging...
    

The args.verbose attribute is False by default. It becomes True only when --verbose is provided. No extra value like --verbose yes is needed.

Understanding the Default Value

With action='store_true', the default value is always False. You don't need to set default=False explicitly. Doing so is redundant.

However, you can change the default using the default parameter. This is less common but useful in specific cases. For more on setting defaults, check our Python Argparse Default Value Guide.


import argparse

parser = argparse.ArgumentParser()
# The default is False, even if not stated.
parser.add_argument('--debug', action='store_true', help='enable debug mode (default: False)')

args = parser.parse_args()
print(f"Debug mode: {args.debug}")
    

$ python script2.py
Debug mode: False

$ python script2.py --debug
Debug mode: True
    

Common Use Cases for Store True

Boolean flags are everywhere in command-line tools. They are ideal for toggling features.

Feature Toggles: Enable optional behavior like verbose output, debug mode, or dry runs.

Configuration Switches: Turn settings on or off, like --force to overwrite files or --recursive for directory operations.

Help and Version Flags: While argparse provides --help and --version automatically, you can create similar custom flags.

Advanced Usage and Combinations

You can use multiple store_true flags in one script. They operate independently.


import argparse

parser = argparse.ArgumentParser(description="Process data with options.")
parser.add_argument('--verbose', action='store_true', help='show detailed logs')
parser.add_argument('--force', action='store_true', help='overwrite existing files')
parser.add_argument('--dry-run', action='store_true', help='simulate without making changes')

args = parser.parse_args()

print(f"Configuration: Verbose={args.verbose}, Force={args.force}, Dry-Run={args.dry_run}")
    

$ python advanced_script.py --verbose --force
Configuration: Verbose=True, Force=True, Dry-Run=False

$ python advanced_script.py --dry-run
Configuration: Verbose=False, Force=False, Dry-Run=True
    

You can also combine boolean flags with other argument types, like positional arguments or options that require a value. For a complete example, visit our Python Argparse Example: Command Line Arguments.

Store True vs. Store Const

store_true is a specialized case of store_const. The store_const action stores a constant value you define.

Use store_true for simple boolean toggles. Use store_const when you need to store a specific, non-boolean value.


import argparse

parser = argparse.ArgumentParser()
# store_true is equivalent to store_const with const=True, default=False
parser.add_argument('--enable', action='store_true')
parser.add_argument('--mode', action='store_const', const='expert', default='novice')

args = parser.parse_args()
print(f"Enable: {args.enable}, Mode: {args.mode}")
    

$ python const_script.py
Enable: False, Mode: novice

$ python const_script.py --enable --mode
Enable: True, Mode: expert
    

Best Practices and Tips

Follow these tips for clean and user-friendly scripts.

Use Clear, Descriptive Names: Flag names should indicate the action. Use --verbose, not -v alone for long options. You can also add a short alias like -v.

Provide Help Text: Always use the help parameter. Explain what the flag does.

Set Sensitive Defaults: The default should be the safest or most common option. For a destructive flag like --force, default to False.

Group Related Flags: For many options, use argument groups to organize the help output.

For a deeper dive into boolean logic with argparse, our Python Argparse Boolean Flags Guide has more details.

Conclusion

The argparse module's store_true action is essential. It creates clean, intuitive boolean command-line flags. These flags let users toggle features without providing extra values.

You learned its basic syntax, saw practical examples, and explored advanced use. Remember to use descriptive names and helpful descriptions. This makes your scripts professional and easy to use.

Start adding store_true flags to your Python scripts. They will become more flexible and powerful. Your users will appreciate the clear control over program behavior.