Home | History | Annotate | Download | only in python2.7

Lines Matching full:option

1 """A powerful, extensible, and easy-to-use option parser.
26 __all__ = ['Option',
85 # Id: option.py 522 2006-06-11 16:22:03Z gward
107 Raised if an Option instance is created with invalid or
111 def __init__(self, msg, option):
113 self.option_id = str(option)
117 return "option %s: %s" % (self.option_id, self.msg)
128 Raised if an invalid option value is encountered on the command
134 Raised if an invalid option is seen on the command line.
140 return _("no such option: %s") % self.opt_str
144 Raised if an ambiguous option is seen on the command line.
151 return (_("ambiguous option: %s (%s?)")
158 Abstract base class for formatting option help. OptionParser
168 the maximum starting column for option help text
170 the calculated starting column for option help text;
180 number of columns available for option help text (calculated)
182 text to replace with each option's default value, "%default"
184 option_strings : { Option : str }
185 maps Option instances to the snippet of help text explaining
186 the syntax of that option, e.g. "-h, --help" or
279 def expand_default(self, option):
281 return option.help
283 default_value = self.parser.defaults.get(option.dest)
287 return option.help.replace(self.default_tag, str(default_value))
289 def format_option(self, option):
290 # The help for each option consists of two parts:
305 opts = self.option_strings[option]
314 if option.help:
315 help_text = self.expand_default(option)
342 def format_option_strings(self, option):
343 """Return a comma-separated list of option strings & metavariables."""
344 if option.takes_value():
345 metavar = option.metavar or option.dest.upper()
347 for sopt in option._short_opts]
349 for lopt in option._long_opts]
351 short_opts = option._short_opts
352 long_opts = option._long_opts
423 def check_builtin(option, opt, value):
424 (cvt, what) = _builtin_cvt[option.type]
429 _("option %s: invalid %s value: %r") % (opt, what, value))
431 def check_choice(option, opt, value):
432 if value in option.choices:
435 choices = ", ".join(map(repr, option.choices))
437 _("option %s: invalid choice: %r (choose from %s)")
445 class Option:
480 # The set of actions allowed by option parsers. Explicitly listed
519 # The set of known types for option parsers. Again, listed here for
524 # validate option arguments according to the option type.
527 # check(option : Option, opt : string, value : string) -> any
529 # option is the Option instance calling the checker
530 # opt is the actual option seen on the command-line
532 # value is the option argument seen on the command-line
535 # for option.type -- eg. an integer if option.type == "int".
562 # Have to be set now, in case no option strings are supplied.
581 # one short option and one long option, either of which
585 raise TypeError("at least one option string must be supplied")
592 "invalid option string %r: "
597 "invalid short option string %r: "
604 "invalid long option string %r: "
660 raise OptionError("invalid option type: %r" % self.type, self)
685 # Glean a destination from the first long option string,
686 # or from the first short option string if no long options.
726 "callback supplied (%r) for non-callback option"
730 "callback_args supplied for non-callback option", self)
733 "callback_kwargs supplied for non-callback option", self)
820 # class Option
857 Update the option values from an arbitrary dictionary, but only
870 Update the option values from an arbitrary dictionary,
906 standard_option_list : [Option]
911 option_list : [Option]
912 the list of Option objects contained by this OptionContainer
913 _short_opt : { string : Option }
914 dictionary mapping short option strings, eg. "-f" or "-X",
915 to the Option instances that implement them. If an Option
916 has multiple short option strings, it will appears in this
918 _long_opt : { string : Option }
919 dictionary mapping long option strings, eg. "--file" or
920 "--exclude", to the Option instances that implement them.
921 Again, a given Option can occur multiple times in this
924 dictionary mapping option destination names to default
933 # Initialize the option list and related data structures.
945 # option mappings used by this OptionParser and all
947 self._short_opt = {} # single letter -> Option instance
948 self._long_opt = {} # long option -> Option instance
949 self.defaults = {} # maps option dest -> default value
953 # For use by OptionGroup constructor -- use shared option
978 # -- Option-adding methods -----------------------------------------
980 def _check_conflict(self, option):
982 for opt in option._short_opts:
985 for opt in option._long_opts:
993 "conflicting option string(s): %s"
995 option)
1008 """add_option(Option)
1012 option = self.option_class(*args, **kwargs)
1014 option = args[0]
1015 if not isinstance(option, Option):
1016 raise TypeError, "not an Option instance: %r" % option
1020 self._check_conflict(option)
1022 self.option_list.append(option)
1023 option.container = self
1024 for opt in option._short_opts:
1025 self._short_opt[opt] = option
1026 for opt in option._long_opts:
1027 self._long_opt[opt] = option
1029 if option.dest is not None: # option has a dest, we need a default
1030 if option.default is not NO_DEFAULT:
1031 self.defaults[option.dest] = option.default
1032 elif option.dest not in self.defaults:
1033 self.defaults[option.dest] = None
1035 return option
1038 for option in option_list:
1039 self.add_option(option)
1041 # -- Option query/removal methods ----------------------------------
1052 option = self._short_opt.get(opt_str)
1053 if option is None:
1054 option = self._long_opt.get(opt_str)
1055 if option is None:
1056 raise ValueError("no such option %r" % opt_str)
1058 for opt in option._short_opts:
1060 for opt in option._long_opts:
1062 option.container.option_list.remove(option)
1071 for option in self.option_list:
1072 if not option.help is SUPPRESS_HELP:
1073 result.append(formatter.format_option(option))
1122 standard_option_list : [Option]
1140 paragraph of help text to print after option help
1143 list of option groups in this parser (option groups are
1156 non-option argument. (This is the tradition followed by
1161 if true, option default values are processed similarly to option
1163 type-checking function for the option's type (as long as the
1178 the set of option values currently being accumulated. Only
1193 option_class=Option,
1214 # Populate the option list; initial sources are the
1288 """Set parsing to not stop on the first non-option, allowing
1296 """Set parsing to stop on the first non-option. Use this if
1324 for option in self._get_all_options():
1325 default = defaults.get(option.dest)
1327 opt_str = option.get_opt_string()
1328 defaults[option.dest] = option.check_value(opt_str, default)
1352 option = (self._short_opt.get(opt_str) or
1354 if option and option.container is not self:
1355 return option.container
1359 # -- Option-parsing methods ----------------------------------------
1378 your option values) and 'args' is the list of arguments left
1411 Check that the supplied option values and leftover arguments are
1412 valid. Returns the option values and leftover arguments
1426 false, stop at the first non-option argument. If true, accumulate any
1427 interspersed non-option arguments in 'largs'.
1438 # process a single long option (possibly with value(s))
1460 # If it consumes 1 (eg. arg is an option that takes no arguments),
1473 Determine which long option string 'opt' matches, ie. which one
1475 'opt' doesn't unambiguously match any long option string.
1493 option = self._long_opt[opt]
1494 if option.takes_value():
1495 nargs = option.nargs
1498 self.error(_("%s option requires an argument") % opt)
1500 self.error(_("%s option requires %d arguments")
1509 self.error(_("%s option does not take a value") % opt)
1514 option.process(opt, value, values, self)
1522 option = self._short_opt.get(opt)
1525 if not option:
1527 if option.takes_value():
1534 nargs = option.nargs
1537 self.error(_("%s option requires an argument") % opt)
1539 self.error(_("%s option requires %d arguments")
1547 else: # option doesn't take a value
1550 option.process(opt, value, values, self)
1635 # Drop the last "\n", or the header if no options or option groups:
1675 """_match_abbrev(s : string, wordmap : {string : Option}) -> string
1699 # Some day, there might be many Option classes. As of Optik 1.3, the
1701 # which will become a factory function when there are many Option
1703 make_option = Option