• Home
  • Raw
  • Download

Lines Matching +full:- +full:- +full:option

1 """A powerful, extensible, and easy-to-use option parser.
7 For support, use the optik-users@lists.sourceforge.net mailing list
8 (http://lists.sourceforge.net/lists/listinfo/optik-users).
15 parser.add_option("-f", "--file", dest="filename",
17 parser.add_option("-q", "--quiet",
26 __all__ = ['Option',
45 Copyright (c) 2001-2006 Gregory P. Ward. All rights reserved.
46 Copyright (c) 2002-2006 Python Software Foundation. All rights reserved.
84 # Id: option_parser.py 527 2006-07-23 15:21:30Z greg
85 # Id: option.py 522 2006-06-11 16:22:03Z gward
86 # Id: help.py 527 2006-07-23 15:21:30Z greg
87 # Id: errors.py 509 2006-04-20 00:58:24Z gward
113 Raised if an Option instance is created with invalid or
117 def __init__(self, msg, option): argument
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"
190 option_strings : { Option : str }
191 maps Option instances to the snippet of help text explaining
192 the syntax of that option, e.g. "-h, --help" or
193 "-fFILE, --file=FILE"
196 printed in help text. Must be either "%s%s" ("-fFILE") or
197 "%s %s" ("-f FILE"), because those are the two syntaxes that
200 similar but for long options; must be either "%s %s" ("--file FILE")
201 or "%s=%s" ("--file=FILE").
218 width -= 2
221 min(max_help_position, max(width - 20, indent_increment * 2))
251 self.current_indent -= self.indent_increment
253 self.level -= 1
263 Format a paragraph of free-form text for inclusion in the
266 text_width = max(self.width - self.current_indent, 11)
286 def expand_default(self, option): argument
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): argument
297 # The help for each option consists of two parts:
299 # eg. ("-x", or "-fFILENAME, --file=FILENAME")
300 # * the user-supplied help string
304 # -x turn on expert mode
309 # -fFILENAME, --file=FILENAME
312 opts = self.option_strings[option]
313 opt_width = self.help_position - self.current_indent - 2
318 opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
321 if option.help:
322 help_text = self.expand_default(option)
327 elif opts[-1] != "\n":
347 self.help_width = max(self.width - self.help_position, 11)
349 def format_option_strings(self, option): argument
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
403 return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading))
424 "float" : (float, _("floating-point")),
427 def check_builtin(option, opt, value): argument
428 (cvt, what) = _builtin_cvt[option.type]
433 _("option %s: invalid %s value: %r") % (opt, what, value))
435 def check_choice(option, opt, value): argument
436 if value in option.choices:
439 choices = ", ".join(map(repr, option.choices))
441 _("option %s: invalid choice: %r (choose from %s)")
449 class Option: class
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
535 # (eg. "-a", "--file")
536 # value is the option argument seen on the command-line
539 # for option.type -- eg. an integer if option.type == "int".
562 # -- Constructor/initialization methods ----------------------------
566 # Have to be set now, in case no option strings are supplied.
577 # out to the _check_*() methods listed in CHECK_METHODS -- which
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: "
599 if not (opt[0] == "-" and opt[1] != "-"):
601 "invalid short option string %r: "
602 "must be of the form -x, (x any non-dash char)" % opt,
606 if not (opt[0:2] == "--" and opt[2] != "-"):
608 "invalid long option string %r: "
609 "must start with --, followed by non-dash" % opt,
630 # -- Constructor validation methods --------------------------------
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.
685 # eg. "--foo-bar" -> "foo_bar"
686 self.dest = self._long_opts[0][2:].replace('-', '_')
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)
742 # -- Miscellaneous methods -----------------------------------------
759 # -- Processing methods --------------------------------------------
817 # class Option
845 Update the option values from an arbitrary dictionary, but only
858 Update the option values from an arbitrary dictionary,
894 standard_option_list : [Option]
899 option_list : [Option]
900 the list of Option objects contained by this OptionContainer
901 _short_opt : { string : Option }
902 dictionary mapping short option strings, eg. "-f" or "-X",
903 to the Option instances that implement them. If an Option
904 has multiple short option strings, it will appear in this
906 _long_opt : { string : Option }
907 dictionary mapping long option strings, eg. "--file" or
908 "--exclude", to the Option instances that implement them.
909 Again, a given Option can occur multiple times in this
912 dictionary mapping option destination names to default
921 # Initialize the option list and related data structures.
932 # For use by OptionParser constructor -- create the main
933 # option mappings used by this OptionParser and all
935 self._short_opt = {} # single letter -> Option instance
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
966 # -- Option-adding methods -----------------------------------------
968 def _check_conflict(self, option): argument
970 for opt in option._short_opts:
973 for opt in option._long_opts:
981 "conflicting option string(s): %s"
983 option)
986 if opt.startswith("--"):
996 """add_option(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)
1029 # -- Option query/removal methods ----------------------------------
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)
1053 # -- Help-formatting methods ---------------------------------------
1059 for option in self.option_list:
1060 if not option.help is SUPPRESS_HELP:
1061 result.append(formatter.format_option(option))
1096 # -- Help-formatting methods ---------------------------------------
1110 standard_option_list : [Option]
1128 paragraph of help text to print after option help
1131 list of option groups in this parser (option groups are
1132 irrelevant for parsing the command-line, but very useful
1137 Assuming -a and -b each take a single argument, the command-line
1138 -ablah foo bar -bboo baz
1140 -ablah -bboo -- foo bar baz
1142 -ablah -- foo bar -bboo baz
1143 -- ie. we stop processing options as soon as we see the first
1144 non-option argument. (This is the tradition followed by
1145 Python's getopt module, Perl's Getopt::Std, and other argument-
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
1170 OptionParser is not thread-safe. If, for some perverse reason, you
1171 need to parse command-line arguments simultaneously in different
1181 option_class=Option,
1202 # Populate the option list; initial sources are the
1216 it) can be garbage-collected promptly. After calling destroy(), the
1227 # -- Private methods -----------------------------------------------
1236 self.add_option("-h", "--help",
1241 self.add_option("--version",
1262 # -- Simple modifier methods ---------------------------------------
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
1308 # Old, pre-Optik 1.5 behaviour.
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)
1321 # -- OptionGroup methods -------------------------------------------
1340 option = (self._short_opt.get(opt_str) or
1342 if option and option.container is not self:
1343 return option.container
1347 # -- Option-parsing methods ----------------------------------------
1359 -> (values : Values, args : [string])
1361 Parse the command-line options found in 'args' (default:
1366 your option values) and 'args' is the list of arguments left
1376 # the rest of the command-line (the "r" stands for
1377 # "remaining" or "right-hand")
1379 # the leftover arguments -- ie. what's left after removing
1381 # or "left-hand")
1397 -> (values : Values, args : [string])
1399 Check that the supplied option values and leftover arguments are
1400 valid. Returns the option values and leftover arguments
1401 (possibly adjusted, possibly completely new -- whatever you
1402 like). Default implementation just returns the passed-in
1412 Process command-line arguments and populate 'values', consuming
1414 false, stop at the first non-option argument. If true, accumulate any
1415 interspersed non-option arguments in 'largs'.
1419 # We handle bare "--" explicitly, and bare "-" is handled by the
1422 if arg == "--":
1425 elif arg[0:2] == "--":
1426 # process a single long option (possibly with value(s))
1428 elif arg[:1] == "-" and len(arg) > 1:
1439 # [arg0, arg1, ..., arg(i-1), arg(i), arg(i+1), ..., arg(N-1)]
1443 # Then rargs is [arg(i), ..., arg(N-1)] and largs is a *subset* of
1444 # [arg0, ..., arg(i-1)] (any options and their arguments will have
1448 # If it consumes 1 (eg. arg is an option that takes no arguments),
1452 # rargs = [arg(i+1), ..., arg(N-1)]
1455 # *empty* -- still a subset of [arg0, ..., arg(i-1)], but
1459 """_match_long_opt(opt : string) -> string
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)
1508 opt = "-" + ch
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)
1542 # -- Feedback methods ----------------------------------------------
1565 If you override this in a subclass, it should not return -- it
1621 # Drop the last "\n", or the header if no options or option groups:
1622 return "".join(result[:-1])
1653 """_match_abbrev(s : string, wordmap : {string : Option}) -> string
1677 # Some day, there might be many Option classes. As of Optik 1.3, the
1679 # which will become a factory function when there are many Option
1681 make_option = Option