Difference between terms: “option”, “argument”, and “parameter”?

后端 未结 3 1341
半阙折子戏
半阙折子戏 2020-12-07 07:54

What are the differences between these terms: \"option\", \"argument\", and \"parameter\"? In man pages these terms often seem to be used interchangeably.

3条回答
  •  -上瘾入骨i
    2020-12-07 08:32

    The man page for a typical Unix command often uses the terms argument, option and parameter. At the lowest level, we have argument and everything is an argument, including the (filesystem path to the) command itself.

    In a shell script you access arguments using the special variables $0 .. $n. Other languages have similar ways to access them (commonly through an array with a name like argv).

    Arguments may be interpreted as options if you wish. How this is done is implementation-specific. You can either roll your own, for exampe a shell (such as bash) script can use provided getopts or getopt commands.

    These typically define an option as an argument beginning with a hyphen (-) and some options may use proceeding arguments as its parameters. More capable parsers (e.g getopt) support mixing short-form (-h) and long-form (--help) options.

    Typically, most options take zero or one parameter. Such parameters are also sometimes called values.

    The supported options are coded in the program code (e.g in the invocation of getopts within a shell script). Any remaining arguments after the options have been consumed are commonly called positional parameters when the order in which they are given is significant (this is in contrast to options which usually can be given in any order).

    Again, the script defines what the positional parameters are by how it consumes and uses them.

    So a typical command

    $ ls -I README -l foo 'bar car' baz
    

    has seven arguments: /usr/bin/ls, -I, README, -l, foo, bar car, and baz accessible as $0 thru $6. The -l and -I are interpreted as options, the latter having a parameter (or value) of README. What remains are positional parameters (foo, bar car and baz).

    Option parsing may alter the argument list by removing those it consumes (e.g using shift or set) so that only the positional parameters remain and are thereafter accessible as $1 .. $n.

提交回复
热议问题