opt command line parser
5 stars based on
Nutshell opt is a subroutine library which facilitates the convenient input of parameters to a C program. Parameters are parsed from a getopt multiple arguments option trading line, with further facilities for reading options from files, from environment strings, or from an interactive environment. The aim of the opt package is to permit programs to be both user- and programmer- friendly.
The package getopt multiple arguments option trading to provide a direct and relatively full-featured input interface to the ultimate user of the program, and at the same time to impose a minimal amount of work on the programmer to "add" options parsing to existing software. But opt does takes a somewhat different philosophy.
Variables are "registered" to strings, and whenever the strings are used getopt multiple arguments option trading the command line, the variables are updated accordingly. This tends to be a little more compact and in my view more convenient than the loop and case-statement approach used by getopt. Also, opt has a few more bells and whistles. Note that the space between the single-character option name and the value is optional.
Some parameters can also be set using long names, for instance: There is also an interactive menu. Set any other parameters as you see fit. Enter a blank line, and the you'll again see a list of parameters and values. You can change the parameters and run again if you like. Note that the programmer may optionally disable the menu, getopt multiple arguments option trading some applications that use opt may not allow the --menu option.
Finally, the program is somewhat self-documenting. There are several other features provided by opt that will be discussed in later sections. This is not the simplest possible, but it illustrates what I personally find to be the most commonly used features. More sophisticated options processing is also available, and will be discussed in later sections. But for short programs that will not be used a lot, "programmer friendly" is more important than "user friendly.
But in doing so, the programmer trades away the "programmer friendliness" of the original code to end up with software that exhibits "user friendliness". Suppose you want to write a program that depends on some parameters. As a programmer, it is convenient to set the parameters in the vernacular, to "hardcode" them to desired values right in the program. It is not really very convenient, though, because you have to recompile the program every time you want to run it with different values.
This is especially inconvenient when the user and the programmer are different people. It is usually more convenient for the user if the parameters can be specified at run time. There is, of course, more than one way to do this. You can specify parameter values in an input file, for instance.
Then the program has to open the file, read the values, assign them to the appropriate variables, and then compute accordingly. It's a little inconvenient for the programmer, and a little inconvenient for the user, but if there are a large number of parameters, this is often the best approach. Even for a small number of parameters, it is useful to at least have the option of saving parameter values in a file.
Another approach is to have the program "ask" the user what parameter values are desired. This style of program sometimes seems friendly at first, since the user doesn't have to know a lot about the program to run it, but just has to answer the getopt multiple arguments option trading. It can get to be pretty awkward after awhile, though, particularly if there are a lot of options, and especially if the program is to be used in shell scripts. It is not too hard to write the program to do this it is basically getopt multiple arguments option trading series alternating printf 's and scanf 'sbut for a lot of parameters, it can get pretty tedious.
One of the most popular approaches is to specify the options and parameter values directly from the command line. This is a bit of a compromise; the user has to know what all the options are, and the programmer has to do a little string parsing.
The motivation for the opt package is to bridge this gap: I know I've said this already, but First, the opt interface will be described, as it appears to the user. This comprises the advertisement for incorporating opt into your code. Then it will be shown how to write code that uses opt for its user interface. Direct command line options Options from a named file Environment strings Rudimentary builtin menu Standard usage messages 2. The power of this assignment is that options for getopt multiple arguments option trading parameters can be assigned in a flexible way: Among these are "flags" which signal whether a given option is on off.
Unlike options which assign values, flag parameters can be assigned with a single getopt multiple arguments option trading. In this case, one can write commands of the following forms: Invoking a flag is not always the same as setting the flag to ON. Depending on how the flags are defined in the program, invoking a flag may set its value to ON, to OFF, or toggle it to the opposite of its value before invocation.
The motivation for toggles and other kinds of flags will become apparent when the command line is extended to files and environment variables and interactive menus. A file permits some luxuries that are not available on the command line directly. One of these is that you are not limited to a single line, and another is that you can add comments in the file. Thus if the file looks like this: It is possible to mix direct command line options with file options.
This is particularly useful if you want to make many runs in which only getopt multiple arguments option trading few parameters are changed at a time. It is also possible to nest files, so that one might have two files: There is a useful abbreviation for files: The environment options are assigned before any command line options, so they can be over-ridden: The environment string is therefore a useful place for storing default options that you don't want to have to keep typing getopt multiple arguments option trading each time you run the program.
Of course I'm always surprised when anything works on that platform. The opt package provides a convenient way to do this, with an interactive menu. You might want to do this to avoid stuff about menus appearing in the usage message, for instance. Subsequent sections will then go into more detailed explanation.
The following program illustrates most of the features that you'd getopt multiple arguments option trading want to use and several you probably don't care about. This is done with a function call that "registers" or associates 1 a variable and a name. These functions one for each variable, in general are usually called at the beginning of the code. Positional options Options that do not have an associated switch and simply appear on the command line as bare arguments.
Flexible options Options that can appear either as a delimited or a positional argument. You can register a long string name as well as the short one character name.
The available option types are: Most of these are self-explanatory: Invoking this option sets the value to 1; and subsequent invocations have no effect. I can't think of why you would want to use it.
In my own experience, I have found absolute flags are usually preferable to toggle flags. The problem with toggles is that you can easily lose track of the current state of the toggle, especially when options can be specified in files, environment variables, and the menu, as well as the command line itself. I find this useful for "verbose" since I can not only turn verbose ON, but I can have a range from slightly verbose to extremely verbose.
There are two types of strings, variable and constant. Which of these to use depends on how the variable is declared: By contrast, to use vstringspace must be allocated dynamically. These are similar to delimited options except that they are not flagged by a delimiter switch on the command line.
The user specifies values for positional options by simply writing those values as bare arguments on the command line. The processing of positional options is determined by the order in which they appear on the command line, hence the name.
In addition, if the option is subsequently made "flexible" see section 2. Undelimited command line arguments are assigned to positional options in the order in which those positional options were registered.
If there are insufficient command line arguments to process all registered positional options, then the remaining postional options are simply never invoked. At the time of writing, such extra undelimited arguments also cause opt processing to terminate immediately, even if there are as yet getopt multiple arguments option trading delimited arguments on the command line. This is probably a mistake and will be fixed in some future release.
Positional options can be registered using any of the types getopt multiple arguments option trading under 2. Flexible getopt multiple arguments option trading are simply positional options that can also be set by the user using a delimited switch as well.
This can be handy in some cases, for instance when setting a positional option from a file. Consider the following example:. Sometimes eg, instance when there is no variable associated with the registration this integer is a more useful identifier of a given option.
The above, for instance, is equivalent to: Makes the option associated with the variable pointed to by v have the corresponding mode. Note that using optarraydelim NULL,delim resets the delimiter for all array options. As a programmer, you need to keep track not only of the array pointer but of the size of the array, so to register an array option requires an extra argument.
These are essentially equivalent to the scalar versions, except that the first argument is a pointer to the size of the array. After the options have been processed, the array x will have nx elements. In the above example, nx would be 3, and x would be 1, x would be 2, and x would be 3.