Command Line Flags in Python

Here’s the translation of the Go code example to Python, formatted in Markdown suitable for Hugo:

Python provides an argparse module for parsing command-line arguments. We’ll use this module to implement our example command-line program.

import argparse

def main():
    # Basic argument declarations are available for string,
    # integer, and boolean options. Here we declare a
    # string argument 'word' with a default value "foo"
    # and a short description.
    parser = argparse.ArgumentParser()
    parser.add_argument("--word", default="foo", help="a string")
    
    # This declares 'numb' and 'fork' arguments, using a
    # similar approach to the 'word' argument.
    parser.add_argument("--numb", type=int, default=42, help="an int")
    parser.add_argument("--fork", action="store_true", help="a bool")
    
    # It's also possible to declare an option that uses an
    # existing variable declared elsewhere in the program.
    svar = "bar"
    parser.add_argument("--svar", default=svar, help="a string var")
    
    # Parse the command-line arguments
    args = parser.parse_args()
    
    # Here we'll just dump out the parsed options and
    # any trailing positional arguments.
    print("word:", args.word)
    print("numb:", args.numb)
    print("fork:", args.fork)
    print("svar:", args.svar)
    print("tail:", args.tail if hasattr(args, 'tail') else [])

if __name__ == "__main__":
    main()

To experiment with the command-line arguments program, you can run it directly with Python.

Try out the program by first giving it values for all arguments:

$ python command_line_flags.py --word=opt --numb=7 --fork --svar=flag
word: opt
numb: 7
fork: True
svar: flag
tail: []

Note that if you omit arguments, they automatically take their default values:

$ python command_line_flags.py --word=opt
word: opt
numb: 42
fork: False
svar: bar
tail: []

Trailing positional arguments can be provided after any flags:

$ python command_line_flags.py --word=opt a1 a2 a3
word: opt
numb: 42
fork: False
svar: bar
tail: ['a1', 'a2', 'a3']

Use -h or --help flags to get automatically generated help text for the command-line program:

$ python command_line_flags.py -h
usage: command_line_flags.py [-h] [--word WORD] [--numb NUMB] [--fork] [--svar SVAR]

options:
  -h, --help   show this help message and exit
  --word WORD  a string
  --numb NUMB  an int
  --fork       a bool
  --svar SVAR  a string var

If you provide an argument that wasn’t specified to the argparse module, the program will print an error message and show the help text again:

$ python command_line_flags.py --wat
usage: command_line_flags.py [-h] [--word WORD] [--numb NUMB] [--fork] [--svar SVAR]
command_line_flags.py: error: unrecognized arguments: --wat

This Python implementation provides similar functionality to the original example, using the argparse module which is part of the Python standard library. It handles argument parsing, default values, help text generation, and error handling in a way that’s idiomatic to Python.