how to use python argparse 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: 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 4: argparse python

# Generic parser function intialization in PYTHON
def create_parser(arguments):
    """Returns an instance of argparse.ArgumentParser"""
    # your code here
    
    parser = argparse.ArgumentParser(
        description="Description of your code")
    parser.add_argument("argument", help="mandatory or positional argument")
    parser.add_argument("-o", "--optional", 
    	help="Will take an optional argument after the flag")
    namespace = parser.parse_args(arguments)
    
    # Returns a namespace object with your arguments
    return namespace