python
argparse
programming
command-line
tutorial

Simple argparse example wanted 1 argument, 3 results

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

argparse is Python's standard way to parse command-line arguments, and it is especially useful when a script should behave differently based on user input. If you want one argument to produce three related results, the clean approach is to parse the value once and then compute all three outputs explicitly in normal Python code.

A Single Positional Argument

The most direct design is to accept one positional integer and derive three results from it. For example, we can print the square, cube, and factorial of the number.

python
1import argparse
2import math
3
4
5parser = argparse.ArgumentParser(description="Compute three results from one integer.")
6parser.add_argument("number", type=int, help="input integer")
7
8args = parser.parse_args()
9
10number = args.number
11
12print("square:", number ** 2)
13print("cube:", number ** 3)
14print("factorial:", math.factorial(number))

Run it like this:

bash
python script.py 5

Example output:

text
square: 25
cube: 125
factorial: 120

This pattern keeps parsing and computation separate, which makes the code easier to test and extend.

Why argparse Helps Even for One Argument

At first glance, using argparse for a single input may seem like overkill. But it buys you several useful things immediately:

  • automatic help text
  • automatic type conversion
  • clear error messages for invalid input

For example, if the user passes a non-integer string, argparse reports the error cleanly instead of forcing you to write custom validation from scratch.

A Version With Named Arguments

Sometimes a named option is clearer than a positional one.

python
1import argparse
2import math
3
4
5parser = argparse.ArgumentParser(description="Compute three results from one integer.")
6parser.add_argument("--number", type=int, required=True, help="input integer")
7
8args = parser.parse_args()
9
10print("square:", args.number ** 2)
11print("cube:", args.number ** 3)
12print("factorial:", math.factorial(args.number))

Then the command becomes:

bash
python script.py --number 5

This is more verbose, but it is sometimes preferable in scripts with several options because it makes the call self-documenting.

If the "Three Results" Are Different Modes

Some people mean something slightly different by "one argument, three results." They may want one command-line argument that selects among three behaviors rather than one number that produces three values.

In that case, choices is a good fit:

python
1import argparse
2
3
4parser = argparse.ArgumentParser()
5parser.add_argument("mode", choices=["small", "medium", "large"])
6args = parser.parse_args()
7
8if args.mode == "small":
9    print("You chose the small mode")
10elif args.mode == "medium":
11    print("You chose the medium mode")
12else:
13    print("You chose the large mode")

So the right argparse design depends on whether "three results" means three outputs from one value or three branches selected by one value.

Help Text Comes for Free

One of the most useful features of argparse is that the script documents itself:

bash
python script.py --help

That command prints the usage message, argument descriptions, and type expectations. It is a small feature, but it is one reason argparse is better than manual sys.argv handling in most real scripts.

Common Pitfalls

The biggest pitfall is putting too much logic into the parsing layer. argparse should gather and validate input; the actual program behavior should usually happen afterward in ordinary Python code.

Another common mistake is forgetting type conversion. If you do not specify type=int, the parsed argument arrives as a string, which can silently break later calculations.

Developers also sometimes choose a positional argument when a named option would make the command clearer, or vice versa. The right choice depends on how often the script is used and how many arguments it is likely to grow.

Summary

  • Use argparse to parse the input once, then compute the three outputs in normal Python code.
  • A single positional argument is often the simplest design for one required value.
  • Named options such as --number can be clearer in more explicit command lines.
  • If one argument selects among three behaviors, choices is often the right tool.
  • 'argparse adds validation, help text, and type conversion with very little code.'

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.