Show
Ignore:
Timestamp:
03/26/08 00:21:15 (16 years ago)
Author:
jerome
Message:

All new-style command line tools now have a working -A|--arguments command
line option to load options and arguments from a text file, one per line.
This option is now documented, and can be used at the same time
as other command line options : in old-style command line tools,
you weren't allowed to do this.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/commandline.py

    r3341 r3355  
    2828 
    2929from pykota import version 
    30 from pykota.utils import loginvalidparam 
     30from pykota.utils import loginvalidparam, getdefaultcharset 
    3131 
    3232def checkandset_pagesize(option, opt, value, optionparser) : 
     
    7474    else :     
    7575        setattr(optionparser.values, option.dest, value) 
     76 
     77def load_arguments_file(option, opt, value, optionparser) :     
     78    """Checks if an option argument is comprised between 0.0 included and 100.0 not included, and validates the option if it is the case.""" 
     79    setattr(optionparser.values, option.dest, value) 
     80    try : 
     81        argsfile = open(value.encode(getdefaultcharset(), "replace"), "r") 
     82    except IOError : 
     83        loginvalidparam(opt, value, additionalinfo=_("this option will be ignored")) 
     84    else : 
     85        arguments = [ l.strip().decode(getdefaultcharset(), "replace") for l in argsfile.readlines() ] 
     86        argsfile.close() 
     87        for i in range(len(arguments)) : 
     88            argi = arguments[i] 
     89            if argi.startswith('"') and argi.endswith('"') : 
     90                arguments[i] = argi[1:-1] 
     91        arguments.reverse()         
     92        for arg in arguments : 
     93            optionparser.rargs.insert(0, arg)         
    7694 
    7795class PyKotaOptionParser(optparse.OptionParser) : 
     
    197215                              action="version", 
    198216                              help=_("show the version number and exit")) 
     217        self.add_option("-A", "--arguments", 
     218                              action="callback", 
     219                              type="string", 
     220                              dest="argumentsfile", 
     221                              callback=load_arguments_file, 
     222                              help=_("loads additional options and arguments from a file, one per line"))