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 ']