argparse nargs 0 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: 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 3: python argparser flags

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

Tags:

Misc Example