from argparse import argumentparser code example
Example 1: how to use argparse
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="what the program does")
parser.add_argument("arg1", help="advice on arg")
parser.add_argument("arg2", help="advice on arg")
parser.add_argument("argn", help="advice on arg")
args = parser.parse_args()
args.arg1, args.arg2 ... args.argn
Example 2: python argparse file argument
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
args = parser.parse_args()
print(args.file.readlines())
Example 3: argparse list of options
parser.add_argument('--list',
default='all',
const='all',
nargs='?',
choices=['servers', 'storage', 'all'],
help='list servers, storage, or both (default: %(default)s)')