parser.add_argument python code example

Example 1: 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 2: python argument parser default value

parser.add_argument("-v", "--verbose", action="store_true",
                    default="your default value", help="verbose output")

Example 3: python argparser flags

parser.add_argument("-v", "--verbose", action="store_true",
                    help="verbose output")

Example 4: parser.add_argument array python

parser.add_argument('-l','--list', nargs='+', help='<Required> Set flag', required=True)
# Use like:
# python arg.py -l 1234 2345 3456 4567

Tags:

Php Example