Command options

From LQWiki
Jump to navigation Jump to search

Command-line options (Command options or flags) are parameters given to a command line program to alter the way it runs. One of the most familiar examples of their usage is ls -l, where -l is a flag telling ls to use the command's "long" format.

Types of Options

  • The simplest options are single-letter flags like the -l above. They can be combined into a single word, like ls -la to use the long format and list all files.
  • Some command options take arguments. Then the program knows not only that the option was given, but also gets some additional information. An example is ssh -l myname myhost.myisp.com, where the -l option is given the parameter myname to tell ssh what username to use when connecting. These options can be combined with the first type, though they must be the last option in the word, like ssh -Xl myname myhost.myisp.com, where the X tells ssh to try to forward X connections, and the l is the same as before.
  • With increasing complexity of programs, single-letter options become cryptic and even impossible. The lsof command, for example, needs to use both - and + signs to set off its single-character options, because it takes so many options. Therefore, long options are used by many programs, allowing the user to specify options with a natural name. Often, these options can be abbreviated to the point of uniqueness, conferring the advantages of both mnemonic value and brevity. Other options can be negated by prefixing the word 'no' to the option.
    • long options of the X type use a single - and consist of more than one letter. They can take arguments or not. For example, emacs -geometry +0-0 uses the geometry option to tell emacs to place itself in the corner of the screen.
    • long options of the GNU type use the double --. ls --all is the same as ls -a. Long and single-letter arguments can be mixed on the same commandline, but not in the same word. Long options of this type that take arguments sometimes use an equals sign instead of a space to separate the option name from its argument.
  • When running a program on a data file that may be mistaken for an option, the -- option can be helpful. This option shows that the option listing is over, and the next input will be a datafile. (Example: rm -- -f would delete the file "-f")
  • Many programs take a list of files to operate on. For these programs, the - option generally means to use standard input instead of opening a file. For example, the command head FILE | grep STRING - uses grep to search the output of head, without needing an intermediate file.

How to write programs that take options

  • C programs can use the getopt() function to interpret single-letter command options. getopt_long() finds long options with '--' before them, while getopt_long_only() can find long options with either '--' or '-'. All of these are described in the getopt() man page, or read the info page wich has other alternatives as well as getopt(), read info libc (see External links below).
  • Shell scripts can use the getopt program.
  • bash scripts can use the getopts builtin command.
  • Perl scripts can use the Getopt::Std or Getopt::Long module.

See also