第2期:argument、parameter以及option有什么区别?

平常交流中,咱们一般用参数一词说明函数或者命令的使用方法,好比:html

  • HashMap 能够经过构造函数的 initialCapacity 参数设置初始容量,我传的 参数 是 1000。
  • rm 命令 -r 参数用来删除目录。

但在看英文文档时,常常会交替的出现argumentparameter以及option,尤为是argument和parameter,更让我困惑,应该不仅是同义词这么简单吧?因而我特意查阅了一些资料。主要针对Java和Shell下的语义进行了梳理:java

Java中的argument和parameter

Java语境中的argument和parameter,在官方文档中给予了很是明确的说明。Oracle Java官方教程的 Passing Information to a Method or a Constructor 一节,提到:linux

Note: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.git

因此,在Java中,parameter指的是函数定义。而argument指的函数调用github

所以,在Java Doc中,使用的是@param注解来讲明参数含义:shell

/** * @param initialCapacity the initial capacity. * @throws IllegalArgumentException if the initial capacity is negative. */
    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
复制代码

而用IllegalArgumentException表示调用时传递的参数不合法。编程

/** * Thrown to indicate that a method has been passed an illegal or inappropriate argument. * * @author unascribed * @since JDK1.0 */
public class IllegalArgumentException extends RuntimeException {
复制代码

另外,Java语境中的含义,也是大多数编程语言中的含义,这一点在维基百科 Parameter (computer programming) - Wikipedia 中有说明:数组

The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition, while argument (sometimes called actual parameter) refers to the actual input supplied at function call.bash

维基百科也用形式参数(formal parameter)指代parameter,而argument则对应于实际参数(actual parameter)。oracle

Shell中的argument、option和parameter

argument

Shell中,不管是命令、脚本或函数,都没法像Java那样定义参数,因此也就不存在Java中严格意义的parameter了。事实上,命令行的功能太复杂,组合太多,根本没法像单一功能的Java函数那样明确的parameter。虽然如此,argument仍是相似的,指运行期传入的值。好比bash的man文档里有这样的说明:

SHELL GRAMMAR Simple Commands A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

具体到执行阶段,除了第一个单词是执行的命令或函数,后面用空格分隔的单词都称做argument。好比ls -l /tmp,一共有2个argument:-l/tmp

不过管道符或重定向都不能算做argument,本质上它们不属于前面的命令。因此不能说ls -l /tmp >files.txt里的>files.txt是第3个argument。

option

option,是具体程序本身定义和识别,一个程序接受那些option是程序里写死的,因此option指在具体的命令或函数下有意义。option能够看作对argument的细分,它们通常是带---的argument。这样便于程序去解析。因此ls -l /tmp的第2个argument是一个option。

维基百科的Command-line interface,对option是这样描述的

A command-line option or simply option (also known as a flag or switch) modifies the operation of a command; the effect is determined by the command's program.

parameter

若是非要和Java中的parameter对应,能够认为Shell中是位置参数(Positional Parameters),第1个参数,第2个参数等等,很是简单粗暴。至于它们是什么含义,那是程序实现的问题了,这也形成了命令行或shell函数须要本身解析位置参数。简单的好处就是包容性强。

不过,bash文档里,仍是有专门对parameter说明的一节:

A parameter is an entity that stores values. It can be a name, a number, or one of the special characters listed below under Special Parameters. A variable is a parameter denoted by a name.

而在parameter下又细分为:位置参数(Positional Parameters)、特殊参数(Special Parameters,如$#$?)、shell变量(Shell Variables,即又shell自动设置或使用的变量,好比PATH)以及数组(Arrays)。这里的parameter更像是特殊的变量。

更实用的区分

Shell上面的解释,有点太晦涩,并且不实用。却是Stack Overflow上有个解释:bash - Difference between terms: “option”, “argument”, and “parameter”? - Stack Overflow,虽然不许确,但更实用。他认为:

A parameter is an argument that provides information to either the command or one of its options

在这个解释下,是这样区分的:

  • argument是命令后传递的全部东西的统称。(这一点和开始的解释同样)
  • argument中有一种是option,它们以---开头。(这一点和开始的解释同样)
  • option接受的值称做parameter(或者称做value也罢)。好比sort -k 1,1就是option的parameter,表示排序的列是第一列。

总结

  • 在不严格的状况下,parameter和argument是能够混用的。
  • Java或一般的编程语言中,parameter指代函数声明中的变量;而argument指代函数被调用时传递的实际输入。
  • 在Shell或命令行中,argument的含义和Java是相似的,指代调用或运行时的输入值。但parameter的含义有点晦涩和不实用。若干结合option,一个简单而实用的区分是:argument是统称,option是---开头的argument,而parameter当作option的值。

参考

  1. Passing Information to a Method or a Constructor (The Java™ Tutorials _ Learning the Java Language _ Classes and Objects)
  2. Parameter (computer programming) - Wikipedia
  3. Command-line interface - Wikipedia
  4. bash - Difference between terms: “option”, “argument”, and “parameter”? - Stack Overflow

《Java与Linux学习周刊》每周五发布,同步更新于:Github知乎掘金

相关文章
相关标签/搜索