Parsing arguments using docopt

Docopt is a new generation argument parser. Its very simple and yet powerful augment parser.

Docopt module doesn't available in python library by default.  It can be downloaded from this URL and installed by running command 'python setup.py install'.


Demo Script:

generalInfo.py

#!/usr/bin/env python
#

"""
Usage:
generalInfo.py --name=<provideName> --age=<provideAge> [--sex=<provideSex>]
"""
from docopt import docopt

args = docopt(__doc__)
print args


Output:

$ python generalInfo.py --name raja --age 26 --sex male

{'--age': '24',
 '--name': 'raja',
 '--sex': 'male'}

Notes:

(1) Output of the args is dictionary. Hence you can easily access the data passed as arguments.

print 'Name:', args['--name']
print 'age:', args['--age']
print 'sex:',  args['--sex']

(2) By default all the arguments except the arguments with in  square bracket is mandatory arguments.  Optional arguments can be specified with in square brackets. As in the demo script, --sex argument is an  optional.




Post a Comment

3 Comments

  1. There is a huge demand for professional big data analysts who are able to use the software which is used to process the big data in order to get accurate results. MNC's are looking for professionals who can process their data so that they can get into a accurate business decision which would eventually help them to earn more profits, they can serve their customers better, and their risk is lowered.
    big data training in chennai|big data training|big data course in chennai|big data training chennai

    ReplyDelete
  2. Big data is used extensively in MNC today as using big data leads to accurate decision making and there are is a huge demand for the big data analysts.
    Big data training in Chennai | Hadoop training in Chennai | Big data training institute in Chennai

    ReplyDelete
  3. Thank you for sharing an amazing & wonderful blog. This content is very useful, informative and valuable in order to enhance knowledge. Keep sharing this type of content with us & keep updating us with new blogs. Apart from this, if anyone who wants to join the Python with data science training institute in Delhi, can contact 9311002620 or visit our website-
    https://www.htsindia.com/Courses/python/python-with-data-science-training-course

    ReplyDelete