Using docopt to easily implement Python command line parameter processing

Source: Internet
Author: User
Tags split

A python library in front of docopt, you can use __doc__ to implement command-line parameter processing, very simple to use; I also happen to have the need to add or remove testsuite/testcase at the command line, so I wrote a demo file.

PS: I just found that docopt has not updated for 2 years, OK, still can continue to use it.

Go directly to my demo program

#!/usr/bin/env python
"" "Just try docopt lib for Python
Usage:
try_docopt.py (H |--help)
try_docopt.py [Options]
Examples:
Try_docopt.py-s +ts5,-ts2-c +TC5,-TC3
Options:
-H,--help
-S,--testsuite Suites #add/remove some testsuites
-C,--testcase cases #add/remove some testcases
"""
From docopt import docopt
Testsuites = [' Ts1 ', ' ts2 ', ' ts3 ', ' TS4 ']
testcases = [' Tc1 ', ' tc2 ', ' tc3 ', ' TC4 ']
def add_remove (Tlist, opt_list):
'''
Add/remove item in Tlist.
Opt_list is a list like [' +ts5 ', '-ts2 '] or [' +tc5 ', '-tc3 '].
'''
Flag = 0
For I in Opt_list:
i = I.strip ()
If I.startswith (' + '):
Tlist.append (i[1:])
Elif i.startswith ('-'):
If i[1:] in tlist:
Tlist.remove (i[1:])
Else
print ' bad argument:%s isn't in%s '% (i[1:], tlist)
Flag = 1
Else
print ' bad argument:%s '% i
Flag = 1
If flag:
Return flag
Else
return tlist
if __name__ = = ' __main__ ':
args = docopt (__doc__)
Ts_arg = Args.get ('--testsuite ')
Tc_arg = Args.get ('--testcase ')
If Ts_arg:
Ts_opt_list = Ts_arg.strip (). Split (', ')
Testsuites = Add_remove (testsuites, Ts_opt_list)
If Tc_arg:
Tc_opt_list = Tc_arg.strip (). Split (', ')
testcases = Add_remove (testcases, Tc_opt_list)
If Testsuites!= 1 and testcases!= 1:
print ' TS:%s '% testsuites
print ' TC:%s '% testcases

jay-ali:py2016 jay$ python try_docopt.py-s +ts5,+ts8,-ts1--testcase-tc3,+tc6
TS: [' ts2 ', ' ts3 ', ' ts4 ', ' ts5 ', ' ts8 ']
TC: [' TC1 ', ' tc2 ', ' tc4 ', ' tc6 ']
jay-ali:py2016 jay$
jay-ali:py2016 jay$ python try_docopt.py-s +ts5,+ts8,-ts1--testcase-tc3,+tc6,-tc7
Bad argument:tc7 isn't in [' TC1 ', ' tc2 ', ' tc4 ', ' tc6 ']

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.