The optparse-inspired command-line parsing library Argparse takes the best of the optparse command-line parsing module and brings it new life. Argparse adds positional as well as optional arguments, the ability to create parsers for sub-commands, more informative help and usage messages, and much more. At the same time, it retains the ease and flexibility of use that made optparse so popular. The following command must be run for Python version 2.5 or 2.6 only running on Ubuntu or Debian Linux v6.x.x.
Installation
Type the following command:
$ sudo apt-get install python-argparse
Sample outputs:
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libavutil-extra-51 libggiwmh0-target-x libggi2 libgii1 libvo-aacenc0 libgii1-target-x mplayer-skin-blue libggiwmh0 libggi-target-x libvo-amrwbenc0 Use 'apt-get autoremove' to remove them. Suggested packages: python-argparse-doc The following NEW packages will be installed: python-argparse 0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded. Need to get 42.9 kB of archives. After this operation, 307 kB of additional disk space will be used. Get:1 http://mirror.anl.gov/debian/ squeeze/main python-argparse all 1.1-1 [42.9 kB] Fetched 42.9 kB in 1s (25.9 kB/s) Selecting previously deselected package python-argparse. (Reading database ... 333589 files and directories currently installed.) Unpacking python-argparse (from .../python-argparse_1.1-1_all.deb) ... Setting up python-argparse (1.1-1) ... Processing triggers for python-support ...
Example
From the “Python Command Line Arguments Examples” page::
<pre lang="python"> #!/usr/bin/python import argparse __author__ = 'nixCraft' parser = argparse.ArgumentParser(description='This is a demo script by nixCraft.') parser.add_argument('-i','--input', help='Input file name',required=True) parser.add_argument('-o','--output',help='Output file name', required=True) args = parser.parse_args() ## show values ## print ("Input file: %s" % args.input ) print ("Output file: %s" % args.output ) |