cpython: 645969f4193b (original) (raw)

new file mode 100644 --- /dev/null +++ b/Doc/howto/argparse.rst @@ -0,0 +1,764 @@ + +:mod:argparse Tutorial + + +:author: Tshepang Lekhonkhobe tshepang@gmail.com + +.. _argparse-tutorial: + +This tutorial is intended to be a gentle introduction to :mod:argparse, the +recommended command-line parsing module in the Python standard library. + +.. note:: +

+ +Concepts +======== + +Let's show the sort of functionality that we are going to explore in this +introductory tutorial by making use of the :command:ls command: + +.. code-block:: sh +

+A few concepts we can learn from the four commands: + +* The :command:ls command is useful when run without any options at all. It defaults

+Following is a result of running the code: + +.. code-block:: sh +

+Here is what is happening: + +* Running the script without any options results in nothing displayed to

+And running the code: + +.. code-block:: sh +

+Here is what's happening: + +* We've added the :meth:add_argument method, which is what we use to specify

+And we get: + +.. code-block:: sh +

+Now, how about doing something even more useful:: +

+Following is a result of running the code: + +.. code-block:: sh +

+That didn't go so well. That's because :mod:argparse treats the options we +give it as strings, unless we tell it otherwise. So, let's tell +:mod:argparse to treat that input as an integer:: +

+Following is a result of running the code: + +.. code-block:: sh +

+That went well. The program now even helpfully quits on bad illegal input +before proceeding. + + +Introducing Optional arguments +============================== + +So far we, have been playing with positional arguments. Let us +have a look on how to add optional ones:: +

+ +And the output: + +.. code-block:: sh +

+Here is what is happening: + +* The program is written so as to display something when --verbosity is

+ +And the output: + +.. code-block:: sh +

+Here is what is happening: + +* The option is now more of a flag than something that requires a value.

+ +And here goes: + +.. code-block:: sh +

+Note that the new ability is also reflected in the help text. + + +Combining Positional and Optional arguments +=========================================== + +Our program keeps growing in complexity:: +

+ +And now the output: + +.. code-block:: sh +

+* We've brought back a positional argument, hence the complaint. + +* Note that the order does not matter. + +How about we give this program of ours back the ability to have +multiple verbosity values, and actually get to use them:: +

+ +And the output: + +.. code-block:: sh +

+These all look good except the last one, which exposes a bug in our program. +Let's fix it by restricting the values the --verbosity option can accept:: +

+ +And the output: + +.. code-block:: sh +

+ +Note that the change also reflects both in the error message as well as the +help string. + +Now, let's use a different approach of playing with verbosity, which is pretty +common. It also matches the way the CPython executable handles its own +verbosity argument (check the output of python --help):: +

+ +We have introduced another action, "count", +to count the number of occurences of a specific optional arguments: + +.. code-block:: sh +

+* Yes, it's now more of a flag (similar to action="store_true") in the

+ +And this is what it gives: + +.. code-block:: sh +

+* First output went well, and fixes the bug we had before.

+ +We've just introduced yet another keyword, default. +We've set it to 0 in order to make it comparable to the other int values. +Remember that by default, +if an optional argument isn't specified, +it gets the None value, and that cannot be compared to an int value +(hence the :exc:TypeError exception). + +And: + +.. code-block:: sh +

+You can go quite far just with what we've learned so far, +and we have only scratched the surface. +The :mod:argparse module is very powerful, +and we'll explore a bit more of it before we end this tutorial. + + +Getting a little more advanced +============================== + +What if we wanted to expand our tiny program to perform other powers, +not just squares:: +

+ +Output: + +.. code-block:: sh +

+ +Notice that so far we've been using verbosity level to change the text +that gets displayed. The following example instead uses verbosity level +to display more text instead:: +

+Output: + +.. code-block:: sh +

+ +Conflicting options +------------------- + +So far, we have been working with two methods of an +:class:argparse.ArgumentParser instance. Let's introduce a third one, +:meth:add_mutually_exclusive_group. It allows for us to specify options that +conflict with each other. Let's also change the rest of the program make the +new functionality makes more sense: +we'll introduce the --quiet option, +which will be the opposite of the --verbose one:: +

+ +Our program is now simpler, and we've lost some functionality for the sake of +demonstration. Anyways, here's the output: + +.. code-block:: sh +

+That should be easy to follow. I've added that last output so you can see the +sort of flexibility you get, i.e. mixing long form options with short form +ones. + +Before we conclude, you probably want to tell your users the main purpose of +your program, just in case they don't know:: +

+ +Note that slight difference in the usage text. Note the [-v | -q], +which tells us that we can either use -v or -q, +but not both at the same time: + +.. code-block:: sh +

+ +Conclusion +========== + +The :mod:argparse module offers a lot more than shown here. +Its docs are quite detailed and thorough, and full of examples. +Having gone through this tutorial, you should easily digest them +without feeling overwhelmed.

--- a/Doc/howto/index.rst +++ b/Doc/howto/index.rst @@ -27,4 +27,5 @@ Currently, the HOWTOs are: unicode.rst urllib2.rst webservers.rst

--- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -12,6 +12,12 @@ -------------- +.. sidebar:: Tutorial +

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

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -480,6 +480,11 @@ Build Based on patch from Hervé Coatanhay.