平常交流中,咱们一般用参数
一词说明函数或者命令的使用方法,好比:html
但在看英文文档时,常常会交替的出现argument
,parameter
以及option
,尤为是argument和parameter,更让我困惑,应该不仅是同义词这么简单吧?因而我特意查阅了一些资料。主要针对Java和Shell下的语义进行了梳理:java
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中,不管是命令、脚本或函数,都没法像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能够看作对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.
若是非要和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
在这个解释下,是这样区分的:
-
或--
开头。(这一点和开始的解释同样)sort -k 1
,1就是option的parameter,表示排序的列是第一列。--
或-
开头的argument,而parameter当作option的值。