python args parser code example
Example 1: python parse args
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args.accumulate(args.integers))
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: python argument parser default value
parser.add_argument("-v", "--verbose", action="store_true",
default="your default value", help="verbose output")
Example 4: argparse accept only few options
...
parser.add_argument('--val',
choices=['a', 'b', 'c'],
help='Special testing value')
args = parser.parse_args(sys.argv[1:])
Example 5: python argparser flags
parser.add_argument("-v", "--verbose", action="store_true",
help="verbose output")
Example 6: parser = argparse.ArgumentParser()
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()