Argparse notes

From raju

Introduction

For parsing command line arguments in Python use argparse. Do not use optparse (which is deprecated) or getopt (which involves writing more code).

parse and validate

tags| fail if an option's value is not specified

    def parse_arguments(args):
        import argparse
        parser = argparse.ArgumentParser(
            description='Get input'
            )
        parser.add_argument(
            "--foo", action='store',
            dest='foo',
            help='User must specify a value for foo on the command line.')
        parser.add_argument(
            "--debug", action='store_true',
            default=False, dest='debug',
            help='show debug output. Default is false.')
    
        res = parser.parse_args(args)
        if res.debug:
            print(res)
        return res
    
    def validate_arguments(args):
        foo = args.foo
        if (foo is None):
            print("Error: foo is not specified. Please specify one.")
            raise()
    
    if __name__ == "__main__":
        args = parse_arguments(sys.argv[1:])
        validate_arguments(args)
    

Sample usage of argparse

    import argparse
    import textwrap  # for dedent

    parser = argparse.ArgumentParser(
        description='Explain what the script does')

    # Description spanning across multiple lines
    parser = argparse.ArgumentParser(
        description='''Utility script that tells why a given set of packages
        are installed.''')

    # preformatted description and epilog
    parser = argparse.ArgumentParser(
        description='My super duper space probe',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=textwrap.dedent('''\
        Features:
        * Ability to orbit Jupiter
        * Ability to scratch Sun
        '''))


    # mandatory positional argument
    parser.add_argument(
        "pattern", action="store",
        help="pattern to search for")

    # optional positional argument
    parser.add_argument(
        "dir", nargs='?',
        default=os.getcwd(),
        help="directory")

    # optional argument that has a default value
    parser.add_argument(
        "--out_dir", default='~/scratch',
        help="output directory name")

    parser.add_argument("--dry", action="store_true",
        default=False, dest="dry",
        help="perform a trial run with no actual changes")
    parser.add_argument("--debug", action="store_true",
        default=False, dest="debug",
        help="show debug output")

    args = parser.parse_args()

pass dictionary

    import json
    parser.add_argument('-d', '--my_dict', type=json.loads)
    args = parse.parse_args()
    
    mydict = args.my_dict  # Will return a dictionary
    

Call the script as

    <script> --my_dict '{"value1":"key1"}'
    

Ref:- http://stackoverflow.com/questions/18608812/accepting-a-dictionary-as-an-argument-with-argparse-and-python

--feature and --no-feature

        parser.add_argument(
            "--header", action='store_true',
            dest='header',
            help='use 1st line as header')
        parser.add_argument(
            "--no-header", action='store_false',
            dest='header',
            help='assumes that there is no header')
        parser.set_defaults(header=True)
    

all positional arguments into a list

        parser.add_argument(
            'input_files', nargs='*',
            action='store',
            help='list of input files'