Lines Matching refs:option
1 """A powerful, extensible, and easy-to-use option parser.
85 # Id: option.py 522 2006-06-11 16:22:03Z gward
117 def __init__(self, msg, option):
119 self.option_id = str(option)
123 return "option %s: %s" % (self.option_id, self.msg)
134 Raised if an invalid option value is encountered on the command
140 Raised if an invalid option is seen on the command line.
146 return _("no such option: %s") % self.opt_str
150 Raised if an ambiguous option is seen on the command line.
157 return (_("ambiguous option: %s (%s?)")
164 Abstract base class for formatting option help. OptionParser
174 the maximum starting column for option help text
176 the calculated starting column for option help text;
186 number of columns available for option help text (calculated)
188 text to replace with each option's default value, "%default"
192 the syntax of that option, e.g. "-h, --help" or
286 def expand_default(self, option):
288 return option.help
290 default_value = self.parser.defaults.get(option.dest)
294 return option.help.replace(self.default_tag, str(default_value))
296 def format_option(self, option):
297 # The help for each option consists of two parts:
312 opts = self.option_strings[option]
321 if option.help:
322 help_text = self.expand_default(option)
349 def format_option_strings(self, option):
350 """Return a comma-separated list of option strings & metavariables."""
351 if option.takes_value():
352 metavar = option.metavar or option.dest.upper()
354 for sopt in option._short_opts]
356 for lopt in option._long_opts]
358 short_opts = option._short_opts
359 long_opts = option._long_opts
427 def check_builtin(option, opt, value):
428 (cvt, what) = _builtin_cvt[option.type]
433 _("option %s: invalid %s value: %r") % (opt, what, value))
435 def check_choice(option, opt, value):
436 if value in option.choices:
439 choices = ", ".join(map(repr, option.choices))
441 _("option %s: invalid choice: %r (choose from %s)")
484 # The set of actions allowed by option parsers. Explicitly listed
523 # The set of known types for option parsers. Again, listed here for
528 # validate option arguments according to the option type.
531 # check(option : Option, opt : string, value : string) -> any
533 # option is the Option instance calling the checker
534 # opt is the actual option seen on the command-line
536 # value is the option argument seen on the command-line
539 # for option.type -- eg. an integer if option.type == "int".
566 # Have to be set now, in case no option strings are supplied.
585 # one short option and one long option, either of which
589 raise TypeError("at least one option string must be supplied")
596 "invalid option string %r: "
601 "invalid short option string %r: "
608 "invalid long option string %r: "
657 raise OptionError("invalid option type: %r" % self.type, self)
682 # Glean a destination from the first long option string,
683 # or from the first short option string if no long options.
723 "callback supplied (%r) for non-callback option"
727 "callback_args supplied for non-callback option", self)
730 "callback_kwargs supplied for non-callback option", self)
845 Update the option values from an arbitrary dictionary, but only
858 Update the option values from an arbitrary dictionary,
902 dictionary mapping short option strings, eg. "-f" or "-X",
904 has multiple short option strings, it will appear in this
907 dictionary mapping long option strings, eg. "--file" or
912 dictionary mapping option destination names to default
921 # Initialize the option list and related data structures.
933 # option mappings used by this OptionParser and all
936 self._long_opt = {} # long option -> Option instance
937 self.defaults = {} # maps option dest -> default value
941 # For use by OptionGroup constructor -- use shared option
968 def _check_conflict(self, option):
970 for opt in option._short_opts:
973 for opt in option._long_opts:
981 "conflicting option string(s): %s"
983 option)
1000 option = self.option_class(*args, **kwargs)
1002 option = args[0]
1003 if not isinstance(option, Option):
1004 raise TypeError("not an Option instance: %r" % option)
1008 self._check_conflict(option)
1010 self.option_list.append(option)
1011 option.container = self
1012 for opt in option._short_opts:
1013 self._short_opt[opt] = option
1014 for opt in option._long_opts:
1015 self._long_opt[opt] = option
1017 if option.dest is not None: # option has a dest, we need a default
1018 if option.default is not NO_DEFAULT:
1019 self.defaults[option.dest] = option.default
1020 elif option.dest not in self.defaults:
1021 self.defaults[option.dest] = None
1023 return option
1026 for option in option_list:
1027 self.add_option(option)
1040 option = self._short_opt.get(opt_str)
1041 if option is None:
1042 option = self._long_opt.get(opt_str)
1043 if option is None:
1044 raise ValueError("no such option %r" % opt_str)
1046 for opt in option._short_opts:
1048 for opt in option._long_opts:
1050 option.container.option_list.remove(option)
1059 for option in self.option_list:
1060 if not option.help is SUPPRESS_HELP:
1061 result.append(formatter.format_option(option))
1128 paragraph of help text to print after option help
1131 list of option groups in this parser (option groups are
1144 non-option argument. (This is the tradition followed by
1149 if true, option default values are processed similarly to option
1151 type-checking function for the option's type (as long as the
1166 the set of option values currently being accumulated. Only
1202 # Populate the option list; initial sources are the
1276 """Set parsing to not stop on the first non-option, allowing
1284 """Set parsing to stop on the first non-option. Use this if
1312 for option in self._get_all_options():
1313 default = defaults.get(option.dest)
1315 opt_str = option.get_opt_string()
1316 defaults[option.dest] = option.check_value(opt_str, default)
1340 option = (self._short_opt.get(opt_str) or
1342 if option and option.container is not self:
1343 return option.container
1366 your option values) and 'args' is the list of arguments left
1399 Check that the supplied option values and leftover arguments are
1400 valid. Returns the option values and leftover arguments
1414 false, stop at the first non-option argument. If true, accumulate any
1415 interspersed non-option arguments in 'largs'.
1426 # process a single long option (possibly with value(s))
1448 # If it consumes 1 (eg. arg is an option that takes no arguments),
1461 Determine which long option string 'opt' matches, ie. which one
1463 'opt' doesn't unambiguously match any long option string.
1481 option = self._long_opt[opt]
1482 if option.takes_value():
1483 nargs = option.nargs
1486 "%(option)s option requires %(number)d argument",
1487 "%(option)s option requires %(number)d arguments",
1488 nargs) % {"option": opt, "number": nargs})
1496 self.error(_("%s option does not take a value") % opt)
1501 option.process(opt, value, values, self)
1509 option = self._short_opt.get(opt)
1512 if not option:
1514 if option.takes_value():
1521 nargs = option.nargs
1524 "%(option)s option requires %(number)d argument",
1525 "%(option)s option requires %(number)d arguments",
1526 nargs) % {"option": opt, "number": nargs})
1533 else: # option doesn't take a value
1536 option.process(opt, value, values, self)
1621 # Drop the last "\n", or the header if no options or option groups: