X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=bup.git;a=blobdiff_plain;f=lib%2Fbup%2Foptions.py;h=a7fd284b719cbf51a61ccc7f706ccbe61fa6495c;hp=9be71110622e8aa92e57aed67bbe69fc2fedae93;hb=bf67f94dd4f4096de4eee07a7dc377d6c889a016;hpb=c078168c16a21eb6cabf02f75a918a0874793976 diff --git a/lib/bup/options.py b/lib/bup/options.py index 9be7111..a7fd284 100644 --- a/lib/bup/options.py +++ b/lib/bup/options.py @@ -1,3 +1,34 @@ +# Copyright 2010-2012 Avery Pennarun and options.py contributors. +# All rights reserved. +# +# (This license applies to this file but not necessarily the other files in +# this package.) +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# +# THIS SOFTWARE IS PROVIDED BY AVERY PENNARUN AND CONTRIBUTORS ``AS +# IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED +# OF THE POSSIBILITY OF SUCH DAMAGE. +# """Command-line options parser. With the help of an options spec string, easily parse command-line options. @@ -15,10 +46,11 @@ longer one, but the short one can be omitted. Long option flags are used as the option's key for the OptDict produced when parsing options. -When the flag definition is ended with an equal sign, the option takes one -string as an argument. Otherwise, the option does not take an argument and -corresponds to a boolean flag that is true when the option is given on the -command line. +When the flag definition is ended with an equal sign, the option takes +one string as an argument, and that string will be converted to an +integer when possible. Otherwise, the option does not take an argument +and corresponds to a boolean flag that is true when the option is +given on the command line. The option's description is found at the right of its flags definition, after one or more spaces. The description ends at the end of the line. If the @@ -29,27 +61,55 @@ Options can be put in different groups. Options in the same group must be on consecutive lines. Groups are formed by inserting a line that begins with a space. The text on that line will be output after an empty line. """ + +from __future__ import absolute_import import sys, os, textwrap, getopt, re, struct -class OptDict: +try: + import fcntl +except ImportError: + fcntl = None + +try: + import termios +except ImportError: + termios = None + + +def _invert(v, invert): + if invert: + return not v + return v + + +def _remove_negative_kv(k, v): + if k.startswith('no-') or k.startswith('no_'): + return k[3:], not v + return k,v + + +class OptDict(object): """Dictionary that exposes keys as attributes. - Keys can bet set or accessed with a "no-" or "no_" prefix to negate the + Keys can be set or accessed with a "no-" or "no_" prefix to negate the value. """ - def __init__(self): + def __init__(self, aliases): self._opts = {} + self._aliases = aliases + + def _unalias(self, k): + k, reinvert = _remove_negative_kv(k, False) + k, invert = self._aliases[k] + return k, invert ^ reinvert def __setitem__(self, k, v): - if k.startswith('no-') or k.startswith('no_'): - k = k[3:] - v = not v - self._opts[k] = v + k, invert = self._unalias(k) + self._opts[k] = _invert(v, invert) def __getitem__(self, k): - if k.startswith('no-') or k.startswith('no_'): - return not self._opts[k[3:]] - return self._opts[k] + k, invert = self._unalias(k) + return _invert(self._opts[k], invert) def __getattr__(self, k): return self[k] @@ -69,31 +129,18 @@ def _intify(v): return v -def _atoi(v): - try: - return int(v or 0) - except ValueError: - return 0 - - -def _remove_negative_kv(k, v): - if k.startswith('no-') or k.startswith('no_'): - return k[3:], not v - return k,v - -def _remove_negative_k(k): - return _remove_negative_kv(k, None)[0] - - -def _tty_width(): - s = struct.pack("HHHH", 0, 0, 0, 0) - try: - import fcntl, termios - s = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, s) - except (IOError, ImportError): - return _atoi(os.environ.get('WIDTH')) or 70 - (ysize,xsize,ypix,xpix) = struct.unpack('HHHH', s) - return xsize or 70 +if not fcntl and termios: + def _tty_width(): + return 70 +else: + def _tty_width(): + s = struct.pack("HHHH", 0, 0, 0, 0) + try: + s = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, s) + except IOError: + return 70 + ysize, xsize, ypix, xpix = struct.unpack('HHHH', s) + return xsize or 70 class Options: @@ -118,7 +165,7 @@ class Options: self._longopts = ['help', 'usage'] self._hasparms = {} self._defaults = {} - self._usagestr = self._gen_usage() + self._usagestr = self._gen_usage() # this also parses the optspec def _gen_usage(self): out = [] @@ -139,7 +186,7 @@ class Options: l.lstrip())) last_was_option = False elif l: - (flags, extra) = l.split(' ', 1) + (flags,extra) = (l + ' ').split(' ', 1) extra = extra.strip() if flags.endswith('='): flags = flags[:-1] @@ -148,22 +195,27 @@ class Options: has_parm = 0 g = re.search(r'\[([^\]]*)\]$', extra) if g: - defval = g.group(1) + defval = _intify(g.group(1)) else: defval = None flagl = flags.split(',') flagl_nice = [] + flag_main, invert_main = _remove_negative_kv(flagl[0], False) + self._defaults[flag_main] = _invert(defval, invert_main) for _f in flagl: - f,dvi = _remove_negative_kv(_f, _intify(defval)) - self._aliases[f] = _remove_negative_k(flagl[0]) + f,invert = _remove_negative_kv(_f, 0) + self._aliases[f] = (flag_main, invert_main ^ invert) self._hasparms[f] = has_parm - self._defaults[f] = dvi - if len(f) == 1: + if f == '#': + self._shortopts += '0123456789' + flagl_nice.append('-#') + elif len(f) == 1: self._shortopts += f + (has_parm and ':' or '') flagl_nice.append('-' + f) else: f_nice = re.sub(r'\W', '_', f) - self._aliases[f_nice] = _remove_negative_k(flagl[0]) + self._aliases[f_nice] = (flag_main, + invert_main ^ invert) self._longopts.append(f + (has_parm and '=' or '')) self._longopts.append('no-' + f) flagl_nice.append('--' + _f) @@ -184,14 +236,15 @@ class Options: def usage(self, msg=""): """Print usage string to stderr and abort.""" sys.stderr.write(self._usagestr) + if msg: + sys.stderr.write(msg) e = self._onabort and self._onabort(msg) or None if e: raise e - def fatal(self, s): + def fatal(self, msg): """Print an error message to stderr and abort with usage string.""" - msg = 'error: %s\n' % s - sys.stderr.write(msg) + msg = '\nerror: %s\n' % msg return self.usage(msg) def parse(self, args): @@ -203,30 +256,29 @@ class Options: """ try: (flags,extra) = self.optfunc(args, self._shortopts, self._longopts) - except getopt.GetoptError, e: + except getopt.GetoptError as e: self.fatal(e) - opt = OptDict() + opt = OptDict(aliases=self._aliases) - for k,v in self._defaults.iteritems(): - k = self._aliases[k] + for k,v in self._defaults.items(): opt[k] = v for (k,v) in flags: k = k.lstrip('-') if k in ('h', '?', 'help', 'usage'): self.usage() - if k.startswith('no-'): - k = self._aliases[k[3:]] - v = 0 + if (self._aliases.get('#') and + k in ('0','1','2','3','4','5','6','7','8','9')): + v = int(k) # guaranteed to be exactly one digit + k, invert = self._aliases['#'] + opt['#'] = v else: - k = self._aliases[k] + k, invert = opt._unalias(k) if not self._hasparms[k]: assert(v == '') v = (opt._opts.get(k) or 0) + 1 else: v = _intify(v) - opt[k] = v - for (f1,f2) in self._aliases.iteritems(): - opt[f1] = opt._opts.get(f2) + opt[k] = _invert(v, invert) return (opt,flags,extra)