1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
|
#
# COPYRIGHT
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DISCLAIMER
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""Base class for building command line applications.
The CommandLineApp class makes creating command line applications as
simple as defining callbacks to handle options when they appear in
'sys.argv'.
To do
- enhance intelligence of option handling
- boolean options should not need to be implemented as functions
- enable/disable with/without options
- type checking for option arguments
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name':'$RCSfile: CommandLineApp.py,v $',
'creator':'Doug Hellmann <doug@hellfly.net>',
'project':'Open Source',
'created':'Tue, 23-May-2000 07:11:43 EDT',
#
# Current Information
#
'author':'$Author: doughellmann $',
'version':'$Revision: 1.7 $',
'date':'$Date: 2001/12/24 19:02:54 $',
}
try:
__version__ = __rcs_info__['version'].split(' ')[1]
except:
__version__ = '0.0'
#
# Import system modules
#
import getopt
import sys
import string
import unittest
#
# Import Local modules
#
from StreamFlushTest import StreamFlushTest
#
# Module
#
class CommandLineApp:
"""Base class for building command line applications.
Define a __doc__ string for the class to explain what the
program does.
When the argumentsDescription field is not empty,
it will be printed appropriately in the help message.
When the examplesDescription field is not empty,
it will be printed last in the help message when
the user asks for help.
"""
argumentsDescription = ''
shortArgumentsDescription = ''
examplesDescription = ''
#
# Exception names
#
ReservedOptionName = 'Reserved option name'
HelpRequested='Help requested'
InvalidOptionValue='Invalid value for option'
InvalidArgument='Invalid argument to program'
#
# Globally useful configuration stuff.
#
optionHandlerPrefix = 'optionHandler_'
optTypeLong = 'optTypeLong'
optTypeShort = 'optTypeShort'
optTakesParam = 'optTakesParam'
optNoParam = 'optNoParam'
verboseLevel = 1
_app_version = '0.0'
def __init__(self, commandLineOptions=sys.argv[1:]):
"Initialize CommandLineApp."
self.commandLineOptions = commandLineOptions
self.__learnValidOpts__()
self.__init_getopt__()
self.appInit()
def appInit(self):
"""Override this method to perform application initialization.
This hook may be easier to override than the __init__ method,
since it takes no parameters.
"""
pass
def shortOptionsStringGet(self):
"""Given the information learned through self.__learnValidOpts__,
construct a string to be passed to getopt to set the valid
single character option syntax.
"""
sas = ''
for (optName, optTakesValue,
optLongOrShort, ignore, ignore) in self.shortOptions:
sas = sas + optName
if optTakesValue == self.optTakesParam:
sas = sas + ':'
return sas
def longOptionsListGet(self):
"""Given the information learned through self.__learnValidOpts__,
construct a list to be passed to getopt to set the valid
long form option syntax.
"""
lal = []
for (optName, optTakesValue,
optLongOrShort, ignore, ignore) in self.longOptions:
if optTakesValue == self.optTakesParam:
opt = '%s=' % optName
else:
opt = optName
lal.append(opt)
if opt.find('-') >= 0:
new_opt = opt.replace('-', '_')
lal.append(new_opt)
return lal
def __init_getopt__(self):
"Parse the command line options."
shortOptionsString = self.shortOptionsStringGet() + 'h'
longOptionList = self.longOptionsListGet()
longOptionList.append('help')
try:
self.parsedOptions, self.remainingOpts = getopt.getopt(
self.commandLineOptions,
shortOptionsString,
longOptionList)
except getopt.error, message:
self.showHelp(message)
raise getopt.error, message
def constructOptionInfo(self, methodName, methodRef):
"""Return an info tuple for an option handler method.
Given a method name, return the information tuple
for that option to the program. The tuple contains:
(option name,
flag showing whether the option takes a value,
flag indicating long or short form option,
help string for option)
"""
optionName = methodName[len(self.optionHandlerPrefix):]
if len(methodRef.func_code.co_varnames) > 1:
optionTakesValue = self.optTakesParam
else:
optionTakesValue = self.optNoParam
if len(optionName) > 1:
optionLongForm = self.optTypeLong
optionName = optionName.replace('_', '-')
else:
optionLongForm = self.optTypeShort
return (optionName, optionTakesValue,
optionLongForm, methodName, methodRef)
def methodNameFromOption(self, option):
"""Given the name of an option, construct
and return the name of the method to handle it.
"""
methodName = '%s%s' % (self.optionHandlerPrefix, option)
return methodName
def scanClassForOptions(self, cRef):
"Scan through the inheritence hierarchy to find option handlers."
for parentClass in cRef.__bases__:
self.scanClassForOptions(parentClass)
for componentName in cRef.__dict__.keys():
component = cRef.__dict__[componentName]
if componentName[:len(self.optionHandlerPrefix)] == self.optionHandlerPrefix and \
type(component).__name__ == 'function':
optionInfo = self.constructOptionInfo(componentName, component)
if optionInfo[0] == 'h':
raise CommandLineApp.ReservedOptionName, 'h'
self.allOptions[ optionInfo[0] ] = optionInfo
self.allMethods[ componentName ] = optionInfo
def __learnValidOpts__(self):
"""Derive the options which are valid for this application.
Examine the methods defined for this class to
learn what options the developer wants to use. Options
can be added by defining an optionHandler method with a
name like optionHandler_<option name>. If the option
handler method takes an argument, the option will require
an argument as well.
"""
self.shortOptions = []
self.longOptions = []
self.allOptions = {}
self.allMethods = {}
self.scanClassForOptions(self.__class__)
for optionName in self.allOptions.keys():
optionInfo = self.allOptions[optionName]
if optionInfo[2] == self.optTypeShort:
self.shortOptions.append( optionInfo )
else:
self.longOptions.append( optionInfo )
def handleHelpOption(self):
#
# Look for -h in self.optList.
# if found, call help function
# then raise HelpRequested
#
for option in self.parsedOptions:
if option[0] == '-h':
self.showHelp()
raise CommandLineApp.HelpRequested, 'Help message was printed.'
if option[0] == '--help':
self.showVerboseHelp()
raise CommandLineApp.HelpRequested, 'Help message was printed.'
def main(self, *args):
"""Main body of your application.
This is the main portion of the app, and is run after all of
the arguments are processed. Override this method to implment
the primary processing section of your application.
"""
pass
def infoForOption(self, option):
"Get the stored information about an option."
optionBase = option
if optionBase[:2] == '--':
optionBase = option[2:]
elif optionBase[0] == '-':
optionBase = optionBase[1:]
optionBase = optionBase.replace('_', '-')
return self.allOptions[ optionBase ]
def methodReferenceFromName(self, methodName):
"Return a reference to the method with the given name."
if len(methodName) > 1:
methodName = methodName.replace('-', '_')
return self.allMethods[methodName][4]
def run(self):
"""Entry point.
Process options and execute callback functions as needed.
This method should not need to be overridden, if the main()
method is defined.
"""
try:
self.handleHelpOption()
for option, optValue in self.parsedOptions:
cleanOption = option
while cleanOption[0] == '-':
cleanOption = cleanOption[1:]
methName = self.methodNameFromOption(cleanOption)
method = self.methodReferenceFromName(methName)
optInfo = self.infoForOption(option)
if optInfo[1] == self.optTakesParam:
optArg = string.split(optValue, ',')
if len(optArg) <= 1:
optArg = optValue
method(self, optArg)
else:
method(self)
exit_code = apply(self.main, tuple(self.remainingOpts))
except KeyboardInterrupt:
try:
self.interruptHandler()
except AttributeError:
sys.stderr.write('Cancelled by user.\n')
pass
exit_code = 1
return exit_code
def getSimpleSyntaxHelpString(self):
"""Return syntax statement.
Return a simplified form of help including only the
syntax of the command.
"""
#
# Initialize the return value
#
helpMsg = ''
#
# Initialize some lists of options to organize the
# printing.
#
shortOptionNoArgs = 'h'
shortOptionArgs = []
longOptionNoArgs = ['help']
longOptionArgs = []
allOptionNames = self.allOptions.keys()
allOptionNames.sort()
#
# Sort out each option into the correct group.
#
for option in allOptionNames:
optName, optTakesValue, optLongOrShort, ignore, ignore = \
self.infoForOption(option)
methodName = self.methodNameFromOption(option)
method = self.methodReferenceFromName(methodName)
if optTakesValue == self.optTakesParam:
if optLongOrShort == self.optTypeLong:
longOptionArgs.append(optName)
else:
shortOptionArgs.append(optName)
else:
if optLongOrShort == self.optTypeLong:
longOptionNoArgs.append(optName)
else:
shortOptionNoArgs = shortOptionNoArgs + optName
#
# Print the name of the command, and the short options
# which take no arguments.
#
helpMsg = '%s%s [-%s] ' % (helpMsg, sys.argv[0], shortOptionNoArgs)
#
# Print the short options which take arguments.
#
for option in shortOptionArgs:
methodName = self.methodNameFromOption(option)
method = self.methodReferenceFromName(methodName)
valueName = method.func_code.co_varnames[1]
helpMsg = '%s\n\t\t[-%s %s] ' % (helpMsg, option, valueName)
#
# Print the long options which take no arguments.
#
for option in longOptionNoArgs:
helpMsg = '%s\n\t\t[--%s] ' % (helpMsg, option)
#
# Print the long options which take arguments.
#
for option in longOptionArgs:
methodName = self.methodNameFromOption(option)
method = self.methodReferenceFromName(methodName)
valueName = method.func_code.co_varnames[1]
helpMsg = '%s\n\t\t[--%s=%s] ' % (helpMsg, option, valueName)
#
# Print the argument description
#
helpMsg = '%s\n\t%s' % (helpMsg, self.shortArgumentsDescription)
#
helpMsg = '%s\n\n' % helpMsg
return helpMsg
def showSimpleSyntaxHelp(self):
"Show basic syntax message."
txt = self.getSimpleSyntaxHelpString()
print txt
print '\tFor more details, use --help.'
print
return
def getOptionHelpString(self, dashInsert, option,
valueInsert, docString):
"Build the help string for an option."
indent_size = 15
baseString = '\t%s%s%s' % (dashInsert,
option,
valueInsert,
)
if len(baseString) > indent_size:
baseString='%s\n' % baseString
description_prefix = '\t%s' % (' ' * indent_size)
else:
format_str = '%%-%ds' % indent_size
baseString = format_str % baseString
description_prefix = ' '
docStringLines = string.split(docString, '\n')
optionString = '%s%s' % (baseString, description_prefix)
if docStringLines:
line = docStringLines[0]
optionString = '%s%s\n' % (optionString, line)
emit_blank = 1
for line in docStringLines[1:]:
line = string.strip(line)
if not line and emit_blank:
emit_blank = 0
elif line:
emit_blank = 1
optionString = '%s\t%s%s\n' % (optionString, ' ' * indent_size, line)
optionString = optionString + '\n'
return optionString
def showOptionHelp(self, dashInsert, option, valueInsert, docString):
'Format and print the help message for a single option.'
#
#print '\t%s%s%s' % (dashInsert, option, valueInsert)
#print ''
#print '\t\t\t%s' % docString
#print ''
print self.getOptionHelpString(dashInsert, option,
valueInsert, docString)
def showVerboseSyntaxHelp(self):
"Show a full description of all options and arguments."
txt = self.getVerboseSyntaxHelpString()
print txt
def getVerboseSyntaxHelpString(self):
"""Return the full description of the options and arguments.
Show a full description of the options and arguments to the
command in something like UNIX man page format. This includes
- a description of each option and argument, taken from the
__doc__ string for the optionHandler method for
the option
- a description of what additional arguments will be processed,
taken from the class member argumentsDescription
"""
#
# Show them how to use it.
#
helpMsg = '\nSYNTAX:\n\n\t'
#
# Show the options.
#
helpMsg = '%s%sOPTIONS:\n\n' % (helpMsg,
self.getSimpleSyntaxHelpString())
helpMsg = string.join( [helpMsg,
self.getOptionHelpString('-', 'h', '',
'Displays abbreviated help message.'),
self.getOptionHelpString('--', 'help', '',
'Displays complete usage information.'),
],
''
)
#
allOptionNames = self.allOptions.keys()
allOptionNames.sort()
for option in allOptionNames:
optName, optTakesValue, optLongOrShort, ignore, ignore = \
self.infoForOption(option)
methodName = self.methodNameFromOption(option)
method = self.methodReferenceFromName(methodName)
docString = method.__doc__
if optTakesValue == self.optTakesParam:
valueInsert = method.func_code.co_varnames[1]
else:
valueInsert = ''
if optLongOrShort == self.optTypeLong:
dashInsert = '--'
if valueInsert:
valueInsert = '=%s' % valueInsert
else:
dashInsert = '-'
if valueInsert:
valueInsert = ' %s' % valueInsert
helpMsg = string.join( [ helpMsg,
self.getOptionHelpString(dashInsert,
option,
valueInsert,
docString) ],
'')
#self.showOptionHelp(dashInsert, option, valueInsert, docString)
helpMsg = helpMsg + '\n'
if self.argumentsDescription:
helpMsg = '%sARGUMENTS:\n\n%s\n\n' % (helpMsg,
self.argumentsDescription)
#print 'ARGUMENTS:'
#print ''
#print self.argumentsDescription
#print ''
return helpMsg
def showVerboseHelp(self):
"""Show a verbose help message explaining how to use the program.
This includes:
* a verbose description of the program, taken from the __doc__
string for the class
* an explanation of each option, produced by
showVerboseSyntaxHelp()
* examples of how to use the program for specific tasks,
taken from the class member examplesDescription
"""
#
# Show the program name and
# a description of what it is for.
#
print ''
print '%s\n' % sys.argv[0]
if self.__class__.__doc__:
print ''
for line in string.split(self.__class__.__doc__, '\n'):
line = string.strip(line)
#if not line: continue
print '\t%s' % line
print ''
self.showVerboseSyntaxHelp()
if self.examplesDescription:
print 'EXAMPLES:'
print ''
print self.examplesDescription
print ''
return
def showHelp(self, errorMessage=None):
"Display help message when error occurs."
print
print '%s version %s' % (self.__class__.__name__, self._app_version)
print
#
# If they made a syntax mistake, just
# show them how to use the program. Otherwise,
# show the full help message.
#
if errorMessage:
print ''
print 'ERROR: ', errorMessage
print ''
print ''
print '%s\n' % sys.argv[0]
print ''
self.showSimpleSyntaxHelp()
return
def statusMessage(self, msg='', verboseLevel=1, error=None):
"""Print a status message to output.
Arguments
msg='' -- The status message string to be printed.
verboseLevel=1 -- The verbose level to use. The message
will only be printed if the current verbose
level is >= this number.
error=None -- If true, the message is considered an error and
printed as such.
"""
if self.verboseLevel >= verboseLevel:
if error:
output = sys.stderr
else:
output = sys.stdout
output.write('%s\n' % msg)
output.flush()
return
def debugMethodEntry(self, methodName, debugLevel=3, **nargs):
"Print a method entry debug statement with named arguments."
arg_str = string.join( map( lambda pair: '%s=%s' % pair, nargs.items() ), ', ')
return self.statusMessage('%s(%s)' % (methodName, arg_str), debugLevel)
def errorMessage(self, msg=''):
'Print a message as an error.'
self.statusMessage('ERROR: %s\n' % msg, 0)
def optionHandler_v(self):
"""Increment the verbose level.
Higher levels are more verbose.
The default is 1.
"""
self.verboseLevel = self.verboseLevel + 1
self.statusMessage('New verbose level is %d' % self.verboseLevel,
3)
return
def optionHandler_q(self):
'Turn on quiet mode.'
self.verboseLevel = 0
class TestApp(CommandLineApp):
""" This is a simple test application.
It defines several optionHandler methods to handle
some example options. One option of each type is
handled by an example.
The __doc__ string for the class should contain
the info about how to use the application. """
examplesDescription = \
""" a description of how to use the program
in various ways goes here.
"""
argumentsDescription = \
""" a description of other arguments goes here
"""
defaultLongFormOption='<default value>'
def optionHandler_a(self, optValue):
'get a value for a'
print '%s: handling a: %s' % (self.__class__.__name__, optValue)
def optionHandler_b(self):
'toggle the value of b'
print '%s: handling b' % (self.__class__.__name__,)
def optionHandler_long_form_option(self):
'boolean option'
print '%s: handling long-form-option' % (self.__class__.__name__,)
def optionHandler_long_form_with_value(self, optValue):
"""First line of help.
get a value for long form option
Default:<manually inserted>"""
print '%s: handling long-form-with-value: %s' % (self.__class__.__name__,
optValue)
def main(self, *args):
'main loop'
print '%s: LEFT OVERS: ' % (self.__class__.__name__,), self.remainingOpts
class SubClassTestApp(TestApp):
'new doc string'
def optionHandler_a(self, newA):
'Doc string for SubClassTestApp'
print 'New A:', newA
TestApp.optionHandler_a(self, newA)
def optionHandler_z(self):
'Doc string for SubClassTestApp'
print '%s -z' % self.__class__.__name__
def optionHandler_option_list(self, optionList):
'Doc string for SubClassTestApp'
if type(optionList) == type([]):
print '%s -z list: ' % self.__class__.__name__, optionList
else:
print '%s -z string: %s' % (self.__class__.__name__, optionList)
def main(self, *args):
apply(TestApp.main, (self,) + args)
raise 'not an error'
class CLATestCase(StreamFlushTest):
def runAndCatchOutput(self, args):
output_text = ''
try:
old_stdout = sys.stdout
old_stderr = sys.stderr
from cStringIO import StringIO
sys.stdout = sys.stderr = StringIO()
SubClassTestApp( args ).run()
except SubClassTestApp.HelpRequested:
output_text = sys.stdout.getvalue()
sys.stdout = old_stdout
sys.stderr = old_stderr
else:
sys.stdout = old_stdout
sys.stderr = old_stderr
self.fail('Help was not requested.')
return output_text
def testShortHelpGeneration(self):
help_text = self.runAndCatchOutput(['-h'])
expected_help_text = """
SubClassTestApp version 0.0
./test_happydoc.py [-hbqvz]
[-a newA]
[--help]
[--long-form-option]
[--long-form-with-value=optValue]
[--option-list=optionList]
For more details, use --help.
"""
assert help_text == expected_help_text, \
'Unexpected help text "%s" does not match "%s"' % (help_text, expected_help_text)
return
def testLongHelpGeneratoion(self):
help_text = self.runAndCatchOutput(['--help'])
expected_help_text = """
./test_happydoc.py
new doc string
SYNTAX:
./test_happydoc.py [-hbqvz]
[-a newA]
[--help]
[--long-form-option]
[--long-form-with-value=optValue]
[--option-list=optionList]
OPTIONS:
-h Displays abbreviated help message.
--help Displays complete usage information.
-a newA Doc string for SubClassTestApp
-b toggle the value of b
--long-form-option
boolean option
--long-form-with-value=optValue
First line of help.
get a value for long form option
Default:<manually inserted>
--option-list=optionList
Doc string for SubClassTestApp
-q Turn on quiet mode.
-v Increment the verbose level.
Higher levels are more verbose.
The default is 1.
-z Doc string for SubClassTestApp
ARGUMENTS:
a description of other arguments goes here
EXAMPLES:
a description of how to use the program
in various ways goes here.
"""
assert help_text == expected_help_text, 'Unexpected help text "%s"' % help_text
return
def testOptionList(self):
class CLAOptionListTest(CommandLineApp):
expected_options = ['a', 'b', 'c']
def optionHandler_t(self, options):
"Expects multiple arguments."
assert options == self.expected_options, \
"Option value does not match expected (%s)" % str(options)
optionHandler_option_list = optionHandler_t
CLAOptionListTest( [ '-t', 'a,b,c' ] ).run()
CLAOptionListTest( [ '--option-list', 'a,b,c' ] ).run()
CLAOptionListTest( [ '--option-list=a,b,c' ] ).run()
return
def testLongOptions(self):
class CLALongOptionTest(CommandLineApp):
def optionHandler_test(self):
"Expects no arguments."
return
def optionHandler_test_args(self, args):
"Expects some arguments"
return
CLALongOptionTest( [ '--test' ] ).run()
CLALongOptionTest( [ '--test-args', 'foo' ] ).run()
CLALongOptionTest( [ '--test-args=foo' ] ).run()
CLALongOptionTest( [ '--test_args', 'foo' ] ).run()
CLALongOptionTest( [ '--test_args=foo' ] ).run()
return
def testArgsToMain(self):
class CLALongOptionTest(CommandLineApp):
expected_args = ( 'a', 'b', 'c' )
def optionHandler_t(self):
pass
def main(self, *args):
assert args == self.expected_args, \
'Got %s instead of expected values.' % str(args)
CLALongOptionTest( [ 'a', 'b', 'c' ] ).run()
CLALongOptionTest( [ '-t', 'a', 'b', 'c' ] ).run()
CLALongOptionTest( [ '-t', '--', 'a', 'b', 'c' ] ).run()
CLALongOptionTest( [ '--', 'a', 'b', 'c' ] ).run()
new_test = CLALongOptionTest( [ '--', '-t', 'a', 'b', 'c' ] )
new_test.expected_args = ('-t',) + new_test.expected_args
new_test.run()
return
if __name__ == '__main__':
unittest.main()
|