ia64/xen-unstable

changeset 2755:b112cdeb83fa

bitkeeper revision 1.1159.1.292 (41800989G4HjOPTJHRAf8UYug6chUA)

Parse bare words as config files making the -f before a config filename
optional.
Make parsing intermingled ``getopt'' and ``var=val'' options sane and
add support for a default option handler for bare words.
author cl349@freefall.cl.cam.ac.uk
date Wed Oct 27 20:48:09 2004 +0000 (2004-10-27)
parents 1511d2acc1a4
children 9b1dacc90715
files tools/python/xen/xm/create.py tools/python/xen/xm/opts.py
line diff
     1.1 --- a/tools/python/xen/xm/create.py	Wed Oct 27 16:59:05 2004 +0000
     1.2 +++ b/tools/python/xen/xm/create.py	Wed Oct 27 20:48:09 2004 +0000
     1.3 @@ -55,6 +55,8 @@ gopts.opt('defconfig', short='f', val='F
     1.4            After the script is loaded, option values that were not set on the
     1.5            command line are replaced by the values set in the script.""")
     1.6  
     1.7 +gopts.default('defconfig')
     1.8 +
     1.9  gopts.opt('config', short='F', val='FILE',
    1.10            fn=set_value, default=None,
    1.11            use="""Domain configuration to use (SXP).
     2.1 --- a/tools/python/xen/xm/opts.py	Wed Oct 27 16:59:05 2004 +0000
     2.2 +++ b/tools/python/xen/xm/opts.py	Wed Oct 27 20:48:09 2004 +0000
     2.3 @@ -204,6 +204,8 @@ class Opts:
     2.4          self.vals.quiet = 0
     2.5          # Variables for default scripts.
     2.6          self.vars = {}
     2.7 +        # Option to use for bare words.
     2.8 +        self.default_opt = None
     2.9  
    2.10      def __repr__(self):
    2.11          return '\n'.join(map(str, self.options))
    2.12 @@ -221,6 +223,15 @@ class Opts:
    2.13          self.options_map[name] = x
    2.14          return x
    2.15  
    2.16 +    def default(self, name):
    2.17 +        self.default_opt = name
    2.18 +
    2.19 +    def getdefault(self, val):
    2.20 +        if self.default_opt is None:
    2.21 +            return 0
    2.22 +        opt = self.option(self.default_opt)
    2.23 +        return opt.set(val)
    2.24 +
    2.25      def var(self, name, **args):
    2.26          x = OptVar(self, name, **args)
    2.27          self.options.append(x)
    2.28 @@ -284,27 +295,27 @@ class Opts:
    2.29          """
    2.30          self.argv = argv
    2.31  
    2.32 -        try:
    2.33 -            (vals, args) = getopt(argv[1:], self.short_opts(), self.long_opts())
    2.34 -        except GetoptError, err:
    2.35 -            self.err(str(err))
    2.36 -
    2.37 -	# hack to work around lack of gnu getopts parsing in python 2.2
    2.38 -	xargs = args
    2.39 -	while xargs[1:]:
    2.40 -	    (v,xargs) = getopt(xargs[1:], self.short_opts(), self.long_opts())
    2.41 -	    vals = vals + v
    2.42 +        # hack to work around lack of gnu getopts parsing in python 2.2
    2.43 +        args = argv[1:]
    2.44 +        xargs = []
    2.45 +        while args:
    2.46 +            # let getopt parse whatever it feels like -- if anything
    2.47 +            try:
    2.48 +                (xvals, args) = getopt(args[0:],
    2.49 +                                       self.short_opts(), self.long_opts())
    2.50 +            except GetoptError, err:
    2.51 +                self.err(str(err))
    2.52 +                
    2.53 +            for (k, v) in xvals:
    2.54 +                for opt in self.options:
    2.55 +                    if opt.specify(k, v): break
    2.56 +                else:
    2.57 +                    print >>sys.stderr, "Error: Unknown option:", k
    2.58 +                    self.usage()
    2.59  
    2.60 -	# back to the real work
    2.61 -        self.args = args
    2.62 -        for (k, v) in vals:
    2.63 -            for opt in self.options:
    2.64 -                if opt.specify(k, v): break
    2.65 -            else:
    2.66 -                print >>sys.stderr, "Error: Unknown option:", k
    2.67 -                self.usage()
    2.68 -        xargs = []
    2.69 -        for arg in args:
    2.70 +            # then process the 1st arg 
    2.71 +            (arg,args) = (args[0], args[1:])
    2.72 +
    2.73              isvar = 0
    2.74              if '=' in arg:
    2.75                  (k, v) = arg.split('=', 1)
    2.76 @@ -312,8 +323,11 @@ class Opts:
    2.77                      if opt.specify(k, v):
    2.78                          isvar = 1
    2.79                          break
    2.80 +            elif self.getdefault(arg):
    2.81 +                isvar = 1
    2.82              if not isvar:
    2.83                  xargs.append(arg)
    2.84 +
    2.85          return xargs
    2.86  
    2.87      def short_opts(self):