Skip to content

Latest commit

 

History

History
99 lines (85 loc) · 3.15 KB

argparse.adoc

File metadata and controls

99 lines (85 loc) · 3.15 KB

PYTHON Python::argparse

Par l’exemple

CODE

argparse_pgr.py
def main():
    parser = argparse.ArgumentParser(description='Description of the program.')
    only_one = parser.add_mutually_exclusive_group()                                 # (1)
    only_one.add_argument('-q', '--quiet', action='store_true', help='quiet')        # (2)
    only_one.add_argument('-v', '--verbose', action='count', help='verbose message') # (3)
    parser.add_argument('-i', '--input', required=False, help='input')               # (4)
    parser.add_argument('-n', '--number', type=int, default=1, help='number')        # (5)
    parser.add_argument('-l', '--level', type=int, choices=[1, 2, 4], help='level')  # (6)
    parser.add_argument('-x', default=None, const='zip', nargs='?', help='format')   # (7)
    subparsers = parser.add_subparsers(help='sub-command help', dest="command")
    # create the parser for the "a" command
    parser_a = subparsers.add_parser('aaa', help='a help')
    parser_a.add_argument('foo', help='bar help')
    # create the parser for the "b" command
    parser_b = subparsers.add_parser('bbb', help='b help')
    parser_b.add_argument('--baz', choices='321', help='baz help')

    #################################
    # some possible usages
    usages = [
        '-i input',
        '-i input -vvv',
        '-i input -n 10',
        '-i input -l 2',
        '-i input -x',
        '-i input -x tar',
        'aaa bar',
        'bbb --baz 1'
    ]
    # see argparse_pgr.py and  argparse_out.adoc
    ...
  1. tells -q and -v cant be called together

  2. Boolean argument eg. -q

  3. counting occurrences eg. -vvv

  4. required arguments

  5. expect an integer, set to 1 if not given. eg. -n 10

  6. choose among a list eg. -l 4 is allowed.

  7. an optional argument, with an optional value. eg. -x is equivalent to -x zip

OUTPUT

> python argparse_pgr.py -i input
Namespace(command=None, input='input', level=None, number=1, quiet=False, verbose=None, x=None)
> python argparse_pgr.py -i input -vvv
Namespace(command=None, input='input', level=None, number=1, quiet=False, verbose=3, x=None)
> python argparse_pgr.py -i input -n 10
Namespace(command=None, input='input', level=None, number=10, quiet=False, verbose=None, x=None)
> python argparse_pgr.py -i input -l 2
Namespace(command=None, input='input', level=2, number=1, quiet=False, verbose=None, x=None)
> python argparse_pgr.py -i input -x
Namespace(command=None, input='input', level=None, number=1, quiet=False, verbose=None, x='zip')
> python argparse_pgr.py -i input -x tar
Namespace(command=None, input='input', level=None, number=1, quiet=False, verbose=None, x='tar')
> python argparse_pgr.py aaa bar
Namespace(command='aaa', foo='bar', input=None, level=None, number=1, quiet=False, verbose=None, x=None)
> python argparse_pgr.py bbb --baz 1
Namespace(baz='1', command='bbb', input=None, level=None, number=1, quiet=False, verbose=None, x=None)