| 1 | n/a | """ CommandLine - Get and parse command line options |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | NOTE: This still is very much work in progress !!! |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | Different version are likely to be incompatible. |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | TODO: |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | * Incorporate the changes made by (see Inbox) |
|---|
| 10 | n/a | * Add number range option using srange() |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | """ |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | from __future__ import print_function |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | __copyright__ = """\ |
|---|
| 17 | n/a | Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com) |
|---|
| 18 | n/a | Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com) |
|---|
| 19 | n/a | See the documentation for further information on copyrights, |
|---|
| 20 | n/a | or contact the author. All Rights Reserved. |
|---|
| 21 | n/a | """ |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | __version__ = '1.2' |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | import sys, getopt, glob, os, re, traceback |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | ### Helpers |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | def _getopt_flags(options): |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | """ Convert the option list to a getopt flag string and long opt |
|---|
| 32 | n/a | list |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | """ |
|---|
| 35 | n/a | s = [] |
|---|
| 36 | n/a | l = [] |
|---|
| 37 | n/a | for o in options: |
|---|
| 38 | n/a | if o.prefix == '-': |
|---|
| 39 | n/a | # short option |
|---|
| 40 | n/a | s.append(o.name) |
|---|
| 41 | n/a | if o.takes_argument: |
|---|
| 42 | n/a | s.append(':') |
|---|
| 43 | n/a | else: |
|---|
| 44 | n/a | # long option |
|---|
| 45 | n/a | if o.takes_argument: |
|---|
| 46 | n/a | l.append(o.name+'=') |
|---|
| 47 | n/a | else: |
|---|
| 48 | n/a | l.append(o.name) |
|---|
| 49 | n/a | return ''.join(s), l |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | def invisible_input(prompt='>>> '): |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | """ Get raw input from a terminal without echoing the characters to |
|---|
| 54 | n/a | the terminal, e.g. for password queries. |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | """ |
|---|
| 57 | n/a | import getpass |
|---|
| 58 | n/a | entry = getpass.getpass(prompt) |
|---|
| 59 | n/a | if entry is None: |
|---|
| 60 | n/a | raise KeyboardInterrupt |
|---|
| 61 | n/a | return entry |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | def fileopen(name, mode='wb', encoding=None): |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | """ Open a file using mode. |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | Default mode is 'wb' meaning to open the file for writing in |
|---|
| 68 | n/a | binary mode. If encoding is given, I/O to and from the file is |
|---|
| 69 | n/a | transparently encoded using the given encoding. |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | Files opened for writing are chmod()ed to 0600. |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | """ |
|---|
| 74 | n/a | if name == 'stdout': |
|---|
| 75 | n/a | return sys.stdout |
|---|
| 76 | n/a | elif name == 'stderr': |
|---|
| 77 | n/a | return sys.stderr |
|---|
| 78 | n/a | elif name == 'stdin': |
|---|
| 79 | n/a | return sys.stdin |
|---|
| 80 | n/a | else: |
|---|
| 81 | n/a | if encoding is not None: |
|---|
| 82 | n/a | import codecs |
|---|
| 83 | n/a | f = codecs.open(name, mode, encoding) |
|---|
| 84 | n/a | else: |
|---|
| 85 | n/a | f = open(name, mode) |
|---|
| 86 | n/a | if 'w' in mode: |
|---|
| 87 | n/a | os.chmod(name, 0o600) |
|---|
| 88 | n/a | return f |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | def option_dict(options): |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | """ Return a dictionary mapping option names to Option instances. |
|---|
| 93 | n/a | """ |
|---|
| 94 | n/a | d = {} |
|---|
| 95 | n/a | for option in options: |
|---|
| 96 | n/a | d[option.name] = option |
|---|
| 97 | n/a | return d |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | # Alias |
|---|
| 100 | n/a | getpasswd = invisible_input |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | _integerRE = re.compile(r'\s*(-?\d+)\s*$') |
|---|
| 103 | n/a | _integerRangeRE = re.compile(r'\s*(-?\d+)\s*-\s*(-?\d+)\s*$') |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | def srange(s, |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | integer=_integerRE, |
|---|
| 108 | n/a | integerRange=_integerRangeRE): |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | """ Converts a textual representation of integer numbers and ranges |
|---|
| 111 | n/a | to a Python list. |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | Supported formats: 2,3,4,2-10,-1 - -3, 5 - -2 |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | Values are appended to the created list in the order specified |
|---|
| 116 | n/a | in the string. |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | """ |
|---|
| 119 | n/a | l = [] |
|---|
| 120 | n/a | append = l.append |
|---|
| 121 | n/a | for entry in s.split(','): |
|---|
| 122 | n/a | m = integer.match(entry) |
|---|
| 123 | n/a | if m: |
|---|
| 124 | n/a | append(int(m.groups()[0])) |
|---|
| 125 | n/a | continue |
|---|
| 126 | n/a | m = integerRange.match(entry) |
|---|
| 127 | n/a | if m: |
|---|
| 128 | n/a | start,end = map(int,m.groups()) |
|---|
| 129 | n/a | l[len(l):] = range(start,end+1) |
|---|
| 130 | n/a | return l |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | def abspath(path, |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | expandvars=os.path.expandvars,expanduser=os.path.expanduser, |
|---|
| 135 | n/a | join=os.path.join,getcwd=os.getcwd): |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | """ Return the corresponding absolute path for path. |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | path is expanded in the usual shell ways before |
|---|
| 140 | n/a | joining it with the current working directory. |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | """ |
|---|
| 143 | n/a | try: |
|---|
| 144 | n/a | path = expandvars(path) |
|---|
| 145 | n/a | except AttributeError: |
|---|
| 146 | n/a | pass |
|---|
| 147 | n/a | try: |
|---|
| 148 | n/a | path = expanduser(path) |
|---|
| 149 | n/a | except AttributeError: |
|---|
| 150 | n/a | pass |
|---|
| 151 | n/a | return join(getcwd(), path) |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | ### Option classes |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | class Option: |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | """ Option base class. Takes no argument. |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | """ |
|---|
| 160 | n/a | default = None |
|---|
| 161 | n/a | helptext = '' |
|---|
| 162 | n/a | prefix = '-' |
|---|
| 163 | n/a | takes_argument = 0 |
|---|
| 164 | n/a | has_default = 0 |
|---|
| 165 | n/a | tab = 15 |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | def __init__(self,name,help=None): |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | if not name[:1] == '-': |
|---|
| 170 | n/a | raise TypeError('option names must start with "-"') |
|---|
| 171 | n/a | if name[1:2] == '-': |
|---|
| 172 | n/a | self.prefix = '--' |
|---|
| 173 | n/a | self.name = name[2:] |
|---|
| 174 | n/a | else: |
|---|
| 175 | n/a | self.name = name[1:] |
|---|
| 176 | n/a | if help: |
|---|
| 177 | n/a | self.help = help |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | def __str__(self): |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | o = self |
|---|
| 182 | n/a | name = o.prefix + o.name |
|---|
| 183 | n/a | if o.takes_argument: |
|---|
| 184 | n/a | name = name + ' arg' |
|---|
| 185 | n/a | if len(name) > self.tab: |
|---|
| 186 | n/a | name = name + '\n' + ' ' * (self.tab + 1 + len(o.prefix)) |
|---|
| 187 | n/a | else: |
|---|
| 188 | n/a | name = '%-*s ' % (self.tab, name) |
|---|
| 189 | n/a | description = o.help |
|---|
| 190 | n/a | if o.has_default: |
|---|
| 191 | n/a | description = description + ' (%s)' % o.default |
|---|
| 192 | n/a | return '%s %s' % (name, description) |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | class ArgumentOption(Option): |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | """ Option that takes an argument. |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | An optional default argument can be given. |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | """ |
|---|
| 201 | n/a | def __init__(self,name,help=None,default=None): |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | # Basemethod |
|---|
| 204 | n/a | Option.__init__(self,name,help) |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | if default is not None: |
|---|
| 207 | n/a | self.default = default |
|---|
| 208 | n/a | self.has_default = 1 |
|---|
| 209 | n/a | self.takes_argument = 1 |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | class SwitchOption(Option): |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | """ Options that can be on or off. Has an optional default value. |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | """ |
|---|
| 216 | n/a | def __init__(self,name,help=None,default=None): |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | # Basemethod |
|---|
| 219 | n/a | Option.__init__(self,name,help) |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | if default is not None: |
|---|
| 222 | n/a | self.default = default |
|---|
| 223 | n/a | self.has_default = 1 |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | ### Application baseclass |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | class Application: |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | """ Command line application interface with builtin argument |
|---|
| 230 | n/a | parsing. |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | """ |
|---|
| 233 | n/a | # Options the program accepts (Option instances) |
|---|
| 234 | n/a | options = [] |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | # Standard settings; these are appended to options in __init__ |
|---|
| 237 | n/a | preset_options = [SwitchOption('-v', |
|---|
| 238 | n/a | 'generate verbose output'), |
|---|
| 239 | n/a | SwitchOption('-h', |
|---|
| 240 | n/a | 'show this help text'), |
|---|
| 241 | n/a | SwitchOption('--help', |
|---|
| 242 | n/a | 'show this help text'), |
|---|
| 243 | n/a | SwitchOption('--debug', |
|---|
| 244 | n/a | 'enable debugging'), |
|---|
| 245 | n/a | SwitchOption('--copyright', |
|---|
| 246 | n/a | 'show copyright'), |
|---|
| 247 | n/a | SwitchOption('--examples', |
|---|
| 248 | n/a | 'show examples of usage')] |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | # The help layout looks like this: |
|---|
| 251 | n/a | # [header] - defaults to '' |
|---|
| 252 | n/a | # |
|---|
| 253 | n/a | # [synopsis] - formatted as '<self.name> %s' % self.synopsis |
|---|
| 254 | n/a | # |
|---|
| 255 | n/a | # options: |
|---|
| 256 | n/a | # [options] - formatted from self.options |
|---|
| 257 | n/a | # |
|---|
| 258 | n/a | # [version] - formatted as 'Version:\n %s' % self.version, if given |
|---|
| 259 | n/a | # |
|---|
| 260 | n/a | # [about] - defaults to '' |
|---|
| 261 | n/a | # |
|---|
| 262 | n/a | # Note: all fields that do not behave as template are formatted |
|---|
| 263 | n/a | # using the instances dictionary as substitution namespace, |
|---|
| 264 | n/a | # e.g. %(name)s will be replaced by the applications name. |
|---|
| 265 | n/a | # |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | # Header (default to program name) |
|---|
| 268 | n/a | header = '' |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | # Name (defaults to program name) |
|---|
| 271 | n/a | name = '' |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | # Synopsis (%(name)s is replaced by the program name) |
|---|
| 274 | n/a | synopsis = '%(name)s [option] files...' |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | # Version (optional) |
|---|
| 277 | n/a | version = '' |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | # General information printed after the possible options (optional) |
|---|
| 280 | n/a | about = '' |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | # Examples of usage to show when the --examples option is given (optional) |
|---|
| 283 | n/a | examples = '' |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | # Copyright to show |
|---|
| 286 | n/a | copyright = __copyright__ |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | # Apply file globbing ? |
|---|
| 289 | n/a | globbing = 1 |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | # Generate debug output ? |
|---|
| 292 | n/a | debug = 0 |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | # Generate verbose output ? |
|---|
| 295 | n/a | verbose = 0 |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | # Internal errors to catch |
|---|
| 298 | n/a | InternalError = BaseException |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | # Instance variables: |
|---|
| 301 | n/a | values = None # Dictionary of passed options (or default values) |
|---|
| 302 | n/a | # indexed by the options name, e.g. '-h' |
|---|
| 303 | n/a | files = None # List of passed filenames |
|---|
| 304 | n/a | optionlist = None # List of passed options |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | def __init__(self,argv=None): |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | # Setup application specs |
|---|
| 309 | n/a | if argv is None: |
|---|
| 310 | n/a | argv = sys.argv |
|---|
| 311 | n/a | self.filename = os.path.split(argv[0])[1] |
|---|
| 312 | n/a | if not self.name: |
|---|
| 313 | n/a | self.name = os.path.split(self.filename)[1] |
|---|
| 314 | n/a | else: |
|---|
| 315 | n/a | self.name = self.name |
|---|
| 316 | n/a | if not self.header: |
|---|
| 317 | n/a | self.header = self.name |
|---|
| 318 | n/a | else: |
|---|
| 319 | n/a | self.header = self.header |
|---|
| 320 | n/a | |
|---|
| 321 | n/a | # Init .arguments list |
|---|
| 322 | n/a | self.arguments = argv[1:] |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | # Setup Option mapping |
|---|
| 325 | n/a | self.option_map = option_dict(self.options) |
|---|
| 326 | n/a | |
|---|
| 327 | n/a | # Append preset options |
|---|
| 328 | n/a | for option in self.preset_options: |
|---|
| 329 | n/a | if not option.name in self.option_map: |
|---|
| 330 | n/a | self.add_option(option) |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | # Init .files list |
|---|
| 333 | n/a | self.files = [] |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | # Start Application |
|---|
| 336 | n/a | rc = 0 |
|---|
| 337 | n/a | try: |
|---|
| 338 | n/a | # Process startup |
|---|
| 339 | n/a | rc = self.startup() |
|---|
| 340 | n/a | if rc is not None: |
|---|
| 341 | n/a | raise SystemExit(rc) |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | # Parse command line |
|---|
| 344 | n/a | rc = self.parse() |
|---|
| 345 | n/a | if rc is not None: |
|---|
| 346 | n/a | raise SystemExit(rc) |
|---|
| 347 | n/a | |
|---|
| 348 | n/a | # Start application |
|---|
| 349 | n/a | rc = self.main() |
|---|
| 350 | n/a | if rc is None: |
|---|
| 351 | n/a | rc = 0 |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | except SystemExit as rcException: |
|---|
| 354 | n/a | rc = rcException |
|---|
| 355 | n/a | pass |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | except KeyboardInterrupt: |
|---|
| 358 | n/a | print() |
|---|
| 359 | n/a | print('* User Break') |
|---|
| 360 | n/a | print() |
|---|
| 361 | n/a | rc = 1 |
|---|
| 362 | n/a | |
|---|
| 363 | n/a | except self.InternalError: |
|---|
| 364 | n/a | print() |
|---|
| 365 | n/a | print('* Internal Error (use --debug to display the traceback)') |
|---|
| 366 | n/a | if self.debug: |
|---|
| 367 | n/a | print() |
|---|
| 368 | n/a | traceback.print_exc(20, sys.stdout) |
|---|
| 369 | n/a | elif self.verbose: |
|---|
| 370 | n/a | print(' %s: %s' % sys.exc_info()[:2]) |
|---|
| 371 | n/a | print() |
|---|
| 372 | n/a | rc = 1 |
|---|
| 373 | n/a | |
|---|
| 374 | n/a | raise SystemExit(rc) |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | def add_option(self, option): |
|---|
| 377 | n/a | |
|---|
| 378 | n/a | """ Add a new Option instance to the Application dynamically. |
|---|
| 379 | n/a | |
|---|
| 380 | n/a | Note that this has to be done *before* .parse() is being |
|---|
| 381 | n/a | executed. |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | """ |
|---|
| 384 | n/a | self.options.append(option) |
|---|
| 385 | n/a | self.option_map[option.name] = option |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | def startup(self): |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | """ Set user defined instance variables. |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | If this method returns anything other than None, the |
|---|
| 392 | n/a | process is terminated with the return value as exit code. |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | """ |
|---|
| 395 | n/a | return None |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | def exit(self, rc=0): |
|---|
| 398 | n/a | |
|---|
| 399 | n/a | """ Exit the program. |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | rc is used as exit code and passed back to the calling |
|---|
| 402 | n/a | program. It defaults to 0 which usually means: OK. |
|---|
| 403 | n/a | |
|---|
| 404 | n/a | """ |
|---|
| 405 | n/a | raise SystemExit(rc) |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | def parse(self): |
|---|
| 408 | n/a | |
|---|
| 409 | n/a | """ Parse the command line and fill in self.values and self.files. |
|---|
| 410 | n/a | |
|---|
| 411 | n/a | After having parsed the options, the remaining command line |
|---|
| 412 | n/a | arguments are interpreted as files and passed to .handle_files() |
|---|
| 413 | n/a | for processing. |
|---|
| 414 | n/a | |
|---|
| 415 | n/a | As final step the option handlers are called in the order |
|---|
| 416 | n/a | of the options given on the command line. |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | """ |
|---|
| 419 | n/a | # Parse arguments |
|---|
| 420 | n/a | self.values = values = {} |
|---|
| 421 | n/a | for o in self.options: |
|---|
| 422 | n/a | if o.has_default: |
|---|
| 423 | n/a | values[o.prefix+o.name] = o.default |
|---|
| 424 | n/a | else: |
|---|
| 425 | n/a | values[o.prefix+o.name] = 0 |
|---|
| 426 | n/a | flags,lflags = _getopt_flags(self.options) |
|---|
| 427 | n/a | try: |
|---|
| 428 | n/a | optlist,files = getopt.getopt(self.arguments,flags,lflags) |
|---|
| 429 | n/a | if self.globbing: |
|---|
| 430 | n/a | l = [] |
|---|
| 431 | n/a | for f in files: |
|---|
| 432 | n/a | gf = glob.glob(f) |
|---|
| 433 | n/a | if not gf: |
|---|
| 434 | n/a | l.append(f) |
|---|
| 435 | n/a | else: |
|---|
| 436 | n/a | l[len(l):] = gf |
|---|
| 437 | n/a | files = l |
|---|
| 438 | n/a | self.optionlist = optlist |
|---|
| 439 | n/a | self.files = files + self.files |
|---|
| 440 | n/a | except getopt.error as why: |
|---|
| 441 | n/a | self.help(why) |
|---|
| 442 | n/a | sys.exit(1) |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | # Call file handler |
|---|
| 445 | n/a | rc = self.handle_files(self.files) |
|---|
| 446 | n/a | if rc is not None: |
|---|
| 447 | n/a | sys.exit(rc) |
|---|
| 448 | n/a | |
|---|
| 449 | n/a | # Call option handlers |
|---|
| 450 | n/a | for optionname, value in optlist: |
|---|
| 451 | n/a | |
|---|
| 452 | n/a | # Try to convert value to integer |
|---|
| 453 | n/a | try: |
|---|
| 454 | n/a | value = int(value) |
|---|
| 455 | n/a | except ValueError: |
|---|
| 456 | n/a | pass |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | # Find handler and call it (or count the number of option |
|---|
| 459 | n/a | # instances on the command line) |
|---|
| 460 | n/a | handlername = 'handle' + optionname.replace('-', '_') |
|---|
| 461 | n/a | try: |
|---|
| 462 | n/a | handler = getattr(self, handlername) |
|---|
| 463 | n/a | except AttributeError: |
|---|
| 464 | n/a | if value == '': |
|---|
| 465 | n/a | # count the number of occurrences |
|---|
| 466 | n/a | if optionname in values: |
|---|
| 467 | n/a | values[optionname] = values[optionname] + 1 |
|---|
| 468 | n/a | else: |
|---|
| 469 | n/a | values[optionname] = 1 |
|---|
| 470 | n/a | else: |
|---|
| 471 | n/a | values[optionname] = value |
|---|
| 472 | n/a | else: |
|---|
| 473 | n/a | rc = handler(value) |
|---|
| 474 | n/a | if rc is not None: |
|---|
| 475 | n/a | raise SystemExit(rc) |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | # Apply final file check (for backward compatibility) |
|---|
| 478 | n/a | rc = self.check_files(self.files) |
|---|
| 479 | n/a | if rc is not None: |
|---|
| 480 | n/a | sys.exit(rc) |
|---|
| 481 | n/a | |
|---|
| 482 | n/a | def check_files(self,filelist): |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | """ Apply some user defined checks on the files given in filelist. |
|---|
| 485 | n/a | |
|---|
| 486 | n/a | This may modify filelist in place. A typical application |
|---|
| 487 | n/a | is checking that at least n files are given. |
|---|
| 488 | n/a | |
|---|
| 489 | n/a | If this method returns anything other than None, the |
|---|
| 490 | n/a | process is terminated with the return value as exit code. |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | """ |
|---|
| 493 | n/a | return None |
|---|
| 494 | n/a | |
|---|
| 495 | n/a | def help(self,note=''): |
|---|
| 496 | n/a | |
|---|
| 497 | n/a | self.print_header() |
|---|
| 498 | n/a | if self.synopsis: |
|---|
| 499 | n/a | print('Synopsis:') |
|---|
| 500 | n/a | # To remain backward compatible: |
|---|
| 501 | n/a | try: |
|---|
| 502 | n/a | synopsis = self.synopsis % self.name |
|---|
| 503 | n/a | except (NameError, KeyError, TypeError): |
|---|
| 504 | n/a | synopsis = self.synopsis % self.__dict__ |
|---|
| 505 | n/a | print(' ' + synopsis) |
|---|
| 506 | n/a | print() |
|---|
| 507 | n/a | self.print_options() |
|---|
| 508 | n/a | if self.version: |
|---|
| 509 | n/a | print('Version:') |
|---|
| 510 | n/a | print(' %s' % self.version) |
|---|
| 511 | n/a | print() |
|---|
| 512 | n/a | if self.about: |
|---|
| 513 | n/a | about = self.about % self.__dict__ |
|---|
| 514 | n/a | print(about.strip()) |
|---|
| 515 | n/a | print() |
|---|
| 516 | n/a | if note: |
|---|
| 517 | n/a | print('-'*72) |
|---|
| 518 | n/a | print('Note:',note) |
|---|
| 519 | n/a | print() |
|---|
| 520 | n/a | |
|---|
| 521 | n/a | def notice(self,note): |
|---|
| 522 | n/a | |
|---|
| 523 | n/a | print('-'*72) |
|---|
| 524 | n/a | print('Note:',note) |
|---|
| 525 | n/a | print('-'*72) |
|---|
| 526 | n/a | print() |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | def print_header(self): |
|---|
| 529 | n/a | |
|---|
| 530 | n/a | print('-'*72) |
|---|
| 531 | n/a | print(self.header % self.__dict__) |
|---|
| 532 | n/a | print('-'*72) |
|---|
| 533 | n/a | print() |
|---|
| 534 | n/a | |
|---|
| 535 | n/a | def print_options(self): |
|---|
| 536 | n/a | |
|---|
| 537 | n/a | options = self.options |
|---|
| 538 | n/a | print('Options and default settings:') |
|---|
| 539 | n/a | if not options: |
|---|
| 540 | n/a | print(' None') |
|---|
| 541 | n/a | return |
|---|
| 542 | n/a | int = [x for x in options if x.prefix == '--'] |
|---|
| 543 | n/a | short = [x for x in options if x.prefix == '-'] |
|---|
| 544 | n/a | items = short + int |
|---|
| 545 | n/a | for o in options: |
|---|
| 546 | n/a | print(' ',o) |
|---|
| 547 | n/a | print() |
|---|
| 548 | n/a | |
|---|
| 549 | n/a | # |
|---|
| 550 | n/a | # Example handlers: |
|---|
| 551 | n/a | # |
|---|
| 552 | n/a | # If a handler returns anything other than None, processing stops |
|---|
| 553 | n/a | # and the return value is passed to sys.exit() as argument. |
|---|
| 554 | n/a | # |
|---|
| 555 | n/a | |
|---|
| 556 | n/a | # File handler |
|---|
| 557 | n/a | def handle_files(self,files): |
|---|
| 558 | n/a | |
|---|
| 559 | n/a | """ This may process the files list in place. |
|---|
| 560 | n/a | """ |
|---|
| 561 | n/a | return None |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | # Short option handler |
|---|
| 564 | n/a | def handle_h(self,arg): |
|---|
| 565 | n/a | |
|---|
| 566 | n/a | self.help() |
|---|
| 567 | n/a | return 0 |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | def handle_v(self, value): |
|---|
| 570 | n/a | |
|---|
| 571 | n/a | """ Turn on verbose output. |
|---|
| 572 | n/a | """ |
|---|
| 573 | n/a | self.verbose = 1 |
|---|
| 574 | n/a | |
|---|
| 575 | n/a | # Handlers for long options have two underscores in their name |
|---|
| 576 | n/a | def handle__help(self,arg): |
|---|
| 577 | n/a | |
|---|
| 578 | n/a | self.help() |
|---|
| 579 | n/a | return 0 |
|---|
| 580 | n/a | |
|---|
| 581 | n/a | def handle__debug(self,arg): |
|---|
| 582 | n/a | |
|---|
| 583 | n/a | self.debug = 1 |
|---|
| 584 | n/a | # We don't want to catch internal errors: |
|---|
| 585 | n/a | class NoErrorToCatch(Exception): pass |
|---|
| 586 | n/a | self.InternalError = NoErrorToCatch |
|---|
| 587 | n/a | |
|---|
| 588 | n/a | def handle__copyright(self,arg): |
|---|
| 589 | n/a | |
|---|
| 590 | n/a | self.print_header() |
|---|
| 591 | n/a | copyright = self.copyright % self.__dict__ |
|---|
| 592 | n/a | print(copyright.strip()) |
|---|
| 593 | n/a | print() |
|---|
| 594 | n/a | return 0 |
|---|
| 595 | n/a | |
|---|
| 596 | n/a | def handle__examples(self,arg): |
|---|
| 597 | n/a | |
|---|
| 598 | n/a | self.print_header() |
|---|
| 599 | n/a | if self.examples: |
|---|
| 600 | n/a | print('Examples:') |
|---|
| 601 | n/a | print() |
|---|
| 602 | n/a | examples = self.examples % self.__dict__ |
|---|
| 603 | n/a | print(examples.strip()) |
|---|
| 604 | n/a | print() |
|---|
| 605 | n/a | else: |
|---|
| 606 | n/a | print('No examples available.') |
|---|
| 607 | n/a | print() |
|---|
| 608 | n/a | return 0 |
|---|
| 609 | n/a | |
|---|
| 610 | n/a | def main(self): |
|---|
| 611 | n/a | |
|---|
| 612 | n/a | """ Override this method as program entry point. |
|---|
| 613 | n/a | |
|---|
| 614 | n/a | The return value is passed to sys.exit() as argument. If |
|---|
| 615 | n/a | it is None, 0 is assumed (meaning OK). Unhandled |
|---|
| 616 | n/a | exceptions are reported with exit status code 1 (see |
|---|
| 617 | n/a | __init__ for further details). |
|---|
| 618 | n/a | |
|---|
| 619 | n/a | """ |
|---|
| 620 | n/a | return None |
|---|
| 621 | n/a | |
|---|
| 622 | n/a | # Alias |
|---|
| 623 | n/a | CommandLine = Application |
|---|
| 624 | n/a | |
|---|
| 625 | n/a | def _test(): |
|---|
| 626 | n/a | |
|---|
| 627 | n/a | class MyApplication(Application): |
|---|
| 628 | n/a | header = 'Test Application' |
|---|
| 629 | n/a | version = __version__ |
|---|
| 630 | n/a | options = [Option('-v','verbose')] |
|---|
| 631 | n/a | |
|---|
| 632 | n/a | def handle_v(self,arg): |
|---|
| 633 | n/a | print('VERBOSE, Yeah !') |
|---|
| 634 | n/a | |
|---|
| 635 | n/a | cmd = MyApplication() |
|---|
| 636 | n/a | if not cmd.values['-h']: |
|---|
| 637 | n/a | cmd.help() |
|---|
| 638 | n/a | print('files:',cmd.files) |
|---|
| 639 | n/a | print('Bye...') |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | if __name__ == '__main__': |
|---|
| 642 | n/a | _test() |
|---|