Beispiel #1
0
             iarg = len(args)
         else:
             # All other arguments are treated as scalars.
             # Pass through argument class get() for validation, conversion, etc..
             try:
                 value = arg.get(args[iarg])
             except ArgumentException, e:
                 exceptions.append(e)
             iarg += 1
         if value is not None:
             setattr(verb_opts, arg.name, value)
 # Run the gauntlet of error disclosure. Abort and display usage as appropriate.
 had_errors = 0
 show_usage = False
 if exceptions:
     msg = 'Argument value %s:' % utility.pluralize('error', len(exceptions))
     utility.error(msg, [e.message for e in exceptions])
     had_errors += 1
 if iarg < len(args):
     self._abort('Extra arguments were provided:', args[iarg:])
     had_errors += 1
     show_usage = True
 if missing:
     fmt = '%%-%ds  %%s' % max([len(o) for (o, h) in missing])
     msg = 'Missing required %s:' % utility.pluralize('argument', len(missing))
     utility.error(msg, [fmt % (o.upper(), h) for (o, h) in missing])
     had_errors += 1
     show_usage = True
 if had_errors > 0:
     if show_usage:
         self._abort()
Beispiel #2
0
 def process_verb_arguments(self, verb, verb_args, verb_opts):
     """
     Validate the verb arguments. Check that required arguments are present
     and populate verb_opts attributes with scalar values or lists (for
     trailing arguments with max_count > 1).
     """
     # Add fixed arguments passed in through the decorator to the verb object.
     args = copy.copy(verb_args) + verb.command_arguments
     # Set attributes for required arguments.
     missing = []
     exceptions = []
     iarg = 0
     nargs = verb.get_argument_count()
     for arg in verb.iter_arguments():
         # It's missing if we've exhausted all the arguments before
         # exhausting all the argument specs, unless it's the last argument
         # spec and it's optional.
         if iarg > len(args) or (iarg == len(args) and arg.min_count > 0):
             if not arg.optional:
                 missing.append((arg.name, arg.help))
             else:
                 setattr(verb_opts, arg.name, None)
                 iarg += 1
         else:
             value = None
             # The last argument can have repeated arguments. If more than
             # one are allowed the values are put into a list.
             if iarg == nargs - 1 and arg.max_count > 1:
                 if len(args) - iarg < arg.min_count:
                     utility.abort(
                         'A minimum of %d %s arguments are required.' %
                         (arg.min_count, arg.name.upper()))
                 if len(args) - iarg > arg.max_count:
                     utility.abort(
                         'A maximum of %d %s arguments are allowed.' %
                         (arg.max_count, arg.name.upper()))
                 # Pass through argument class get() for validation, conversion, etc..
                 # Skip bad values and report on them at the end.
                 value = []
                 for v in args[iarg:]:
                     try:
                         value.append(arg.get(v))
                     except ArgumentException as e:
                         exceptions.append(e)
                 iarg = len(args)
             elif len(args) > 0:
                 # All other arguments are treated as scalars.
                 # Pass through argument class get() for validation, conversion, etc..
                 try:
                     value = arg.get(args[iarg])
                 except ArgumentException as e:
                     exceptions.append(e)
                 iarg += 1
             if value is not None or arg.min_count == 0:
                 setattr(verb_opts, arg.name, value)
     # Run the gauntlet of error disclosure. Abort and display usage as appropriate.
     had_errors = 0
     show_usage = False
     if exceptions:
         msg = 'Argument value %s:' % utility.pluralize(
             'error', len(exceptions))
         utility.error(msg, [str(e) for e in exceptions])
         had_errors += 1
     if iarg < len(args):
         self._abort('Extra arguments were provided:', args[iarg:])
         had_errors += 1
         show_usage = True
     if missing:
         fmt = '%%-%ds  %%s' % max([len(o) for (o, h) in missing])
         msg = 'Missing required %s:' % utility.pluralize(
             'argument', len(missing))
         utility.error(msg, [fmt % (o.upper(), h) for (o, h) in missing])
         had_errors += 1
         show_usage = True
     if had_errors > 0:
         if show_usage:
             self._abort()
         sys.exit(1)
Beispiel #3
0
             iarg = len(args)
         elif len(args) > 0:
             # All other arguments are treated as scalars.
             # Pass through argument class get() for validation, conversion, etc..
             try:
                 value = arg.get(args[iarg])
             except ArgumentException, e:
                 exceptions.append(e)
             iarg += 1
         if value is not None or arg.min_count == 0:
             setattr(verb_opts, arg.name, value)
 # Run the gauntlet of error disclosure. Abort and display usage as appropriate.
 had_errors = 0
 show_usage = False
 if exceptions:
     msg = 'Argument value %s:' % utility.pluralize(
         'error', len(exceptions))
     utility.error(msg, [str(e) for e in exceptions])
     had_errors += 1
 if iarg < len(args):
     self._abort('Extra arguments were provided:', args[iarg:])
     had_errors += 1
     show_usage = True
 if missing:
     fmt = '%%-%ds  %%s' % max([len(o) for (o, h) in missing])
     msg = 'Missing required %s:' % utility.pluralize(
         'argument', len(missing))
     utility.error(msg, [fmt % (o.upper(), h) for (o, h) in missing])
     had_errors += 1
     show_usage = True
 if had_errors > 0:
     if show_usage:
Beispiel #4
0
             iarg = len(args)
         elif len(args) > 0:
             # All other arguments are treated as scalars.
             # Pass through argument class get() for validation, conversion, etc..
             try:
                 value = arg.get(args[iarg])
             except ArgumentException, e:
                 exceptions.append(e)
             iarg += 1
         if value is not None or arg.min_count == 0:
             setattr(verb_opts, arg.name, value)
 # Run the gauntlet of error disclosure. Abort and display usage as appropriate.
 had_errors = 0
 show_usage = False
 if exceptions:
     msg = "Argument value %s:" % utility.pluralize("error", len(exceptions))
     utility.error(msg, [str(e) for e in exceptions])
     had_errors += 1
 if iarg < len(args):
     self._abort("Extra arguments were provided:", args[iarg:])
     had_errors += 1
     show_usage = True
 if missing:
     fmt = "%%-%ds  %%s" % max([len(o) for (o, h) in missing])
     msg = "Missing required %s:" % utility.pluralize("argument", len(missing))
     utility.error(msg, [fmt % (o.upper(), h) for (o, h) in missing])
     had_errors += 1
     show_usage = True
 if had_errors > 0:
     if show_usage:
         self._abort()