Argparse

Optparse is deprecated since 3.2 and all development will continue here.

# Read a single argument with a value
parser.add_argument('--id', type=int, nargs=1)

# Read a single argument with a default
parser.add_argument('--id', type=int, nargs='?', default=0)

# Read a flag (boolean) argument
parser.add_argument('--delete', nargs='?', const=True, default=False)

# Read multiple arguments
parser.add_argument('--ids', type=int, nargs='+')

# Required arguments
parser.add_argument('--id', type=int, nargs=1, required=True)

References:

  1. optparse - Parser for command line options

  2. Python argparse cheat sheet