python
argparse
command-line
script
help-message

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.

python
1import argparse
2import sys
3
4parser = argparse.ArgumentParser(description="Example utility")
5parser.add_argument("--name")
6
7if len(sys.argv) == 1:
8    parser.print_help()
9    parser.exit(1)
10
11args = parser.parse_args()
12print(args)

Why this works:

  • 'sys.argv always includes the script name as the first element'
  • length 1 means 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:

python
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", action="store_true")

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.

python
1import argparse
2import sys
3
4parser = argparse.ArgumentParser(prog="tool")
5subparsers = parser.add_subparsers(dest="command")
6
7run_parser = subparsers.add_parser("run", help="Run the job")
8run_parser.add_argument("--count", type=int, default=1)
9
10if len(sys.argv) == 1:
11    parser.print_help()
12    parser.exit(1)
13
14args = parser.parse_args()
15print(args)

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.

python
1import argparse
2
3parser = argparse.ArgumentParser()
4parser.add_argument("input_file")
5args = parser.parse_args()

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:

python
if len(sys.argv) == 1:
    parser.print_help()
    parser.exit(0)

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.

python
1import argparse
2
3parser = argparse.ArgumentParser(
4    description="Upload reports to the internal service"
5)
6parser.add_argument("--env", default="dev", help="Target environment")
7parser.add_argument("--dry-run", action="store_true", help="Show actions without executing")

If you are going to display help automatically, make sure the text actually helps.

Common Pitfalls

  • Expecting argparse to 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 1 or 0 without 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

  • 'argparse does 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 0 or 1.

Course illustration
Course illustration

All Rights Reserved.