Best Practices for Creating Filter Handlers in Windows Search
5 stars based on
Deprecated since version 2. The optparse module is deprecated and will not be developed further; development will continue with the argparse module. Thus, the following command lines are all equivalent to the above example:. To that end, it supports only the most common command-line syntax and semantics conventionally used under Unix. If you are unfamiliar with these conventions, read this section to acquaint yourself with them. In Python, arguments are elements of sys.
It is occasionally desirable to substitute an argument list other than sys. Also, traditional Unix syntax allows multiple options to be merged into a single argument, e. The GNU project introduced -- followed by a series of hyphen-separated words, e.
These are the only two option syntaxes provided by optparse. These option syntaxes are not supported by optparseand they never will be. With optparseoption arguments may either be in a separate argument from their option:. This is somewhat controversial, because it makes parsing ambiguous: Because of this ambiguity, optparse does not support this feature. Assuming that --report takes one argument, report. Options are used to provide beste handler fur optionen information to beste handler fur optionen or customize the execution of a program.
A program should be able to run just fine with no options whatsoever. Pick a random program from the Unix or GNU toolsets. Can it run without any options at all and still make sense? The main exceptions are findtarand dd —all of which are mutant oddballs that have been rightly criticized for their non-standard syntax and confusing interfaces. As an example of good command-line interface design, consider the humble cp utility, for copying files. Hence, cp fails if you run it with no arguments.
However, it has a flexible, useful syntax that does not require any options at all:. You can get pretty far with beste handler fur optionen that. Most cp implementations provide a bunch of options to tweak exactly how the files are copied: But none of this distracts from the core mission of cpwhich is to copy either one file to another, or several files to another directory.
Positional arguments are for those pieces of information that your program absolutely, positively requires to run. A good user interface should have as few absolute requirements as possible.
This applies whether the user interface is a command-line, a configuration file, or a GUI: In short, try to minimize the amount of information that users are absolutely required to supply—use sensible defaults whenever possible. Of course, you also beste handler fur optionen to make your programs reasonably flexible. Too much flexibility has drawbacks as well, of course; too many options can overwhelm users and make your code much harder to maintain. This section covers the code patterns that are common to any optparse -based program.
First, you need to import the OptionParser class; then, early in the main program, create an OptionParser instance:. Each option has one or more option strings, such as -f or --fileand several option attributes that tell optparse what to expect and what to do when it encounters that option on the command line.
The option strings passed to OptionParser. For brevity, we will frequently refer to encountering an option on the command line; in reality, optparse encounters option strings and looks up options from them. This tutorial section only covers the four most important option attributes: Of these, action is the most fundamental. Actions tell optparse what to do when it encounters beste handler fur optionen option on the command line.
There is a fixed set of actions hard-coded into optparse ; adding new actions beste handler fur optionen an advanced topic covered in section Extending optparse. Most beste handler fur optionen tell optparse to store a value in some variable—for example, take a string from the command line and store it in an attribute of options.
The most common option action is storewhich tells optparse to beste handler fur optionen the next argument or the remainder of the current argumentensure beste handler fur optionen it is of the correct type, and store it to your chosen destination. When optparse sees the option string -fit consumes the next argument, foo. Some other option types supported by optparse are int and float.
Note that this option has no long option string, which is perfectly acceptable. Combined with the fact that the default action is storethat means our first example can be a lot shorter:. If there are no long option strings, optparse looks at the first short option string: Adding types is covered in beste handler fur optionen Extending optparse. Flag options—set a variable to true or false when a particular option is seen —are quite common.
For example, you might have a verbose flag that is turned on with -v and off with -q:. Here we have two different options with the same destination, which is perfectly OK. It just means you have to be a bit careful when setting default values— see below. When optparse encounters -v on the command line, it sets options. Some other actions supported by optparse are:. What happens if those options are never seen? This is usually fine, but sometimes you want more control.
If we want optparse to set verbose to True unless -q is seen, then we can do this:. Since default values apply to the destination beste handler fur optionen than to any particular option, and these two options happen to have the same destination, this is exactly equivalent:. Beste handler fur optionen, the default value for verbose will be True: As before, the last value specified for a given option destination is the one that counts.
For clarity, try to use one method or the other of setting default values, not both. All you have to do is supply a help value for each option, and optionally a short usage message for your whole program. If optparse encounters either -h or --help on the command-line, or if you just call parser. If the help output is triggered by a help option, optparse exits after printing the help text. The expanded string is then printed before the detailed option help.
By default, optparse converts the destination variable name to uppercase and uses that for the meta-variable. This is important for more than just saving space, though: This is a simple but effective way to make your help text a lot clearer and more beste handler fur optionen for end users.
New in version 2. When dealing with many options, it is convenient to group these options for better help output. An OptionParser can contain several option groups, each of which can contain several options.
An option group is obtained using the class OptionGroup:. Continuing with the parser defined in the previous section, adding an OptionGroup to a parser is easy:.
A bit more complete example might involve using more than one group: Similar to the brief usage string, optparse can also print a version string for your program. You have to supply the string as the version argument to OptionParser:. Apart from that, version can contain anything you like. When you supply it, optparse automatically adds a --version option to your parser. The following two methods can be used to print and get the version string:.
Print the version message for the current program self. Does nothing if self. Beste handler fur optionen are two broad classes of errors that optparse has to worry about: Programmer errors are usually erroneous calls to OptionParser.
These are dealt with in the usual way: OptionError or TypeError and let the program crash. Handling user errors is much more important, since they are guaranteed to happen no matter how stable your code is.
Also, you can call OptionParser. In either case, optparse handles the error the same way: Consider the first example above, where the user passes 4x to an option that takes an integer:. The first step in using optparse is beste handler fur optionen create an OptionParser instance. The OptionParser constructor has no required arguments, but a number of optional keyword arguments.
You should always pass them as keyword arguments, i. There are several ways to populate the parser with options. The preferred way is by using OptionParser. The other alternative is to pass a list of pre-constructed Option instances to the OptionParser constructor, as in:. Do not instantiate Option directly.
Each Option instance represents a set of synonymous command-line option strings, e. You can specify any number of short beste handler fur optionen long option strings, but you must specify at least one overall option string.