command line options in R: "optparse" package
5 stars based on
Thus, the following command lines are optparse iterate over options trading 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.
Options are used to provide extra information to tune 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 optparse iterate over options trading 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 just 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 optparse iterate over options trading one file to another, or several files to another directory.
Positional arguments are optparse iterate over options trading 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 optparse iterate over options trading of information that users are absolutely required to supply—use sensible defaults whenever possible.
Of course, you also want 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 optparse iterate over options trading. 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 "--file"and several option attributes that tell optparse what to expect and what to do when it encounters that option on the command line.
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 an option on the command line. There is a fixed set of actions hard-coded into optparse ; adding new actions is an advanced topic covered in section Extending optparse. Most actions 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 take the next argument or the remainder of the current argumentensure that it is of the correct type, and store it to your chosen destination.
When optparse sees the option string "-f"it 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 optparse iterate over options trading string: Adding types is covered in section 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 rather than to any particular option, and these two options happen to have the same destination, this is exactly equivalent:. Again, the default value for verbose will be True: As before, the last value specified for a given option destination is optparse iterate over options trading 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 useful 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. Continuing with the parser defined above, adding an OptionGroup to a parser is easy:.
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:. There 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 optparse iterate over options trading 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 optparse iterate over options trading.
Also, you optparse iterate over options trading call OptionParser. In either case, optparse handles the error the same way: Consider the first optparse iterate over options trading above, where the user passes "4x" to an option that takes an integer:.
The first step in using optparse is to create an OptionParser instance. The OptionParser constructor has no required arguments, but a number of optional keyword arguments.
You should always pass them optparse iterate over options trading 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 Optparse iterate over options trading instances to the OptionParser constructor, as in:.
Optparse iterate over options trading not instantiate Option directly. Each Option instance represents a set of synonymous command-line option strings, e. You can specify any number of short or long option strings, but you must specify at least one overall option string.
The keyword arguments define attributes of the new Option object. The most important option attribute is actionand it largely determines which other attributes are relevant or required. If you pass irrelevant option attributes, or fail to pass required ones, optparse raises an OptionError exception explaining your mistake. The standard option actions hard-coded into optparse are:. For this action, you may also supply type and dest option attributes; see Standard option actions.
As you can see, most actions involve storing or updating a value somewhere. Option arguments and optparse iterate over options trading other values are stored as attributes of this object, according to the dest destination option attribute. The type and dest option attributes are almost as important as actionbut action is the only one that makes sense for all options.