Display help message with Python argparse when script is called without any arguments
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
argparse prints help when the user asks for it with -h or --help, but it does not automatically show help just because no arguments were passed. If you want that behavior, you need to detect the empty invocation yourself or structure the parser so a required argument fails with a helpful message. The cleanest solution depends on whether your command has required positionals, optional flags, or subcommands.
The Direct Approach: Check sys.argv
The simplest pattern is to inspect the raw argument list before parsing.
Why this works:
- '
sys.argvalways includes the script name as the first element' - length
1means the user passed nothing else - '
print_help()shows the usage text without needing a parse failure'
This is the best general-purpose pattern when you want help text instead of a cryptic no-op.
Why parse_args() Alone Is Not Enough
If your parser has only optional arguments, calling parse_args() with no user input is valid. The result is just a namespace full of defaults.
That means argparse has no reason to print help automatically.
So this parser:
accepts an empty command line perfectly. If you want the help screen instead, you must make that behavior explicit.
A Cleaner Pattern for Subcommands
For command-line tools with subcommands, the most useful no-argument behavior is often to show the top-level help.
This is a common pattern in tools like git-style CLIs where the subcommand is the real required action.
Alternative: Let Required Arguments Trigger Errors
Sometimes you do not need a manual sys.argv check. If the command truly requires a positional argument, argparse already prints usage and an error when that argument is missing.
Running that with no arguments produces usage text automatically because the parser has a required positional.
Use the manual help-print pattern only when the built-in failure behavior is not what you want.
Exit Code Choice Matters
Many examples exit with code 1, which is fine if no arguments should be treated as incorrect usage. If showing help with no arguments is considered a normal informational behavior in your CLI, exiting with 0 may be more appropriate.
Example:
The right choice depends on whether an empty invocation is an error in your tool design.
Keep Help Useful
A good help screen is more than generated syntax. Add descriptions and defaults so the output is worth showing.
If you are going to display help automatically, make sure the text actually helps.
Common Pitfalls
- Expecting
argparseto print help automatically when only optional arguments exist. - Calling
parse_args()first and only then trying to decide whether to show help. - Using exit code
1or0without thinking about whether no-argument invocation is an error. - Building a subcommand CLI but forgetting to guide the user when no subcommand is supplied.
- Showing a help screen that is too sparse to be useful.
Summary
- '
argparsedoes not automatically show help for empty input unless required parsing fails.' - The common fix is
if len(sys.argv) == 1: parser.print_help(); parser.exit(...). - Subcommand-based CLIs especially benefit from this pattern.
- Use required positionals when empty invocation should naturally be an error.
- Decide deliberately whether the no-argument exit code should be
0or1.

