diff -r af388201c997 Doc/library/argparse.rst --- a/Doc/library/argparse.rst Tue Mar 22 18:48:50 2016 +0100 +++ b/Doc/library/argparse.rst Wed Mar 23 00:08:48 2016 -0400 @@ -20,58 +20,61 @@ The :mod:`argparse` module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and :mod:`argparse` -will figure out how to parse those out of :data:`sys.argv`. The :mod:`argparse` -module also automatically generates help and usage messages and issues errors +will attempt to parse those arguments from the command line. The :mod:`argparse` +module also automatically generates help and usage messages, and it will issue errors when users give the program invalid arguments. Example ------- -The following code is a Python program that takes a list of integers and -produces either the sum or the max:: +The following code is a Python program that takes a String and outputs the +first letter of the String:: import argparse - parser = argparse.ArgumentParser(description='Process some integers.') - parser.add_argument('integers', metavar='N', type=int, nargs='+', - help='an integer for the accumulator') - parser.add_argument('--sum', dest='accumulate', action='store_const', - const=sum, default=max, - help='sum the integers (default: find the max)') - + parser = argparse.ArgumentParser(description='Return the first letter of your message.') + parser.add_argument("message", help='Type in any message') args = parser.parse_args() - print(args.accumulate(args.integers)) + + print args.message[:1] + Assuming the Python code above is saved into a file called ``prog.py``, it can -be run at the command line and provides useful help messages:: +be run at the command line:: $ python prog.py -h - usage: prog.py [-h] [--sum] N [N ...] - - Process some integers. + usage: prog.py [-h] message + + Return the first letter of your message positional arguments: - N an integer for the accumulator + message type in any message optional arguments: -h, --help show this help message and exit - --sum sum the integers (default: find the max) - -When run with the appropriate arguments, it prints either the sum or the max of -the command-line integers:: - - $ python prog.py 1 2 3 4 - 4 - - $ python prog.py 1 2 3 4 --sum - 10 - -If invalid arguments are passed in, it will issue an error:: - - $ python prog.py a b c - usage: prog.py [-h] [--sum] N [N ...] - prog.py: error: argument N: invalid int value: 'a' + + +Optional arguments are denoted with the "--" or "-" prefix:: + + import argparse + + parser = argparse.ArgumentParser(description='Return the first letter of your message.') + parser.add_argument("message", help='Type in any message') + parser.add_argument("--last", help='return the last letter instead', action='store_true') + args = parser.parse_args() + + if args.last: + print args.message[-1:] + else: + print args.message[:1] + + +When run with the appropriate arguments, it prints the first letter of the input string:: + + $ python prog.py hello + h + The following sections walk you through this example. @@ -82,7 +85,7 @@ The first step in using the :mod:`argparse` is creating an :class:`ArgumentParser` object:: - >>> parser = argparse.ArgumentParser(description='Process some integers.') + >>> parser = argparse.ArgumentParser(description='Return the first letter of your message.') The :class:`ArgumentParser` object will hold all the information necessary to parse the command line into Python data types. @@ -97,17 +100,15 @@ on the command line and turn them into objects. This information is stored and used when :meth:`~ArgumentParser.parse_args` is called. For example:: - >>> parser.add_argument('integers', metavar='N', type=int, nargs='+', - ... help='an integer for the accumulator') - >>> parser.add_argument('--sum', dest='accumulate', action='store_const', - ... const=sum, default=max, - ... help='sum the integers (default: find the max)') + >>> parser.add_argument('message', help='an integer for the accumulator') + >>> parser.add_argument('--last', action='store_true', + ... help='Return the last letter instead') Later, calling :meth:`~ArgumentParser.parse_args` will return an object with -two attributes, ``integers`` and ``accumulate``. The ``integers`` attribute -will be a list of one or more ints, and the ``accumulate`` attribute will be -either the :func:`sum` function, if ``--sum`` was specified at the command line, -or the :func:`max` function if it was not. +two attributes, ``message`` and ``last``. The ``message`` attribute +will be a string, and the ``last`` attribute will be +either True, if ``--last`` was specified at the command line, +or the False function if it was not. Parsing arguments @@ -119,8 +120,8 @@ In most cases, this means a simple :class:`Namespace` object will be built up from attributes parsed out of the command line:: - >>> parser.parse_args(['--sum', '7', '-1', '42']) - Namespace(accumulate=, integers=[7, -1, 42]) + >>> parser.parse_args(['--last', 'hello']) + Namespace(last=True, message='hello']) In a script, :meth:`~ArgumentParser.parse_args` will typically be called with no arguments, and the :class:`ArgumentParser` will automatically determine the