Designing command-line programs.
Turn a useful script into a repeatable tool with arguments, output, and exit status.
Start here.
A command line is a text interface to programs. A shell splits a command into words and starts a process. In python greet.py Ada, python selects the interpreter, greet.py is the source file, and Ada is an argument the program can read. Arguments arrive as text, even when they look numeric. A command-line tool translates those words into named values, performs an operation, and reports a result.
The argparse module handles this translation. ArgumentParser creates a parser; add_argument('name') declares one required positional value; parse_args() reads the words supplied at launch. The returned object stores the value as args.name. print writes normal output to standard output. If a required argument is missing, argparse explains the expected usage and ends with a nonzero exit status, a signal that the command did not complete successfully.
A useful command separates its core rule from parsing and printing. The function that calculates a result can receive ordinary Python values; the command layer decides how to obtain those values from words and where to display the result. if __name__ == '__main__': runs the command entry point when a file is executed directly, while importing that file can reuse its definitions without launching the command. This makes a script easier to understand as it grows into a tool.
Build the model.
A command-line program receives arguments from the shell, performs a job, and reports its result. A good command tells the user what it will read, where it will write, and what failure means. argparse gives you named options, help text, type conversion, and validation so you do not have to decode raw strings by hand.
Design the core logic as ordinary functions that accept values and return results. Keep parsing and printing near the entry point. That separation makes the logic testable without launching a subprocess, while the command-line layer remains a thin translation between human input and your function's contract.
How it works.
A program has more than one output channel. Print useful results to standard output, explanatory errors to standard error, and use a nonzero exit status for failure. This lets a shell script or scheduled job tell success from failure without reading prose. The -h or --help output is part of the user interface; show a concrete example in its description or README.
A Python module can expose a main() function and run it under if __name__ == '__main__'. Importing the module then defines functions without executing the command. Dependencies belong in a virtual environment or documented package list. Do not assume every user launches the tool from the same working directory; accept a path argument when location matters.
Read it line by line.
import argparse
parser = argparse.ArgumentParser(description='Repeat a greeting')
parser.add_argument('name')
args = parser.parse_args()
print(f'Hello, {args.name}!')argparse defines one required positional argument. Running python greet.py Ada sets args.name to 'Ada' and prints Hello, Ada! Running without a name prints usage information and exits with an error. Running with -h prints the generated help. The parsing step turns shell text into named program data.
See the rule again.
import argparse
def greet(name, times):
return [f'Hello, {name}!' for _ in range(times)]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('name')
parser.add_argument('--times', type=int, default=1)
args = parser.parse_args()
for line in greet(args.name, args.times):
print(line)
if __name__ == '__main__':
main()The command can run as python greet.py Ada --times 2. argparse converts the option to an integer, greet creates the lines, and main prints them. Consider rejecting a negative count if the tool's contract requires at least one greeting.
A mistake to avoid.
Do not call parse_args at import time in a reusable module. A test or another program importing it may unexpectedly parse that caller's arguments.