1. 程式人生 > 程式設計 >第2期:argument、parameter以及option有什麼區別?

第2期:argument、parameter以及option有什麼區別?

日常交流中,我們通常用引數一詞說明函式或者命令的使用方法,比如:

  • HashMap 可以通過建構函式的 initialCapacity 引數設定初始容量,我傳的 引數 是 1000。
  • rm 命令 -r 引數用來刪除目錄。

但在看英文檔案時,經常會交替的出現argumentparameter以及option,尤其是argument和parameter,更讓我困惑,應該不只是同義詞這麼簡單吧?於是我特地查閱了一些資料。主要針對Java和Shell下的語義進行了梳理:

Java中的argument和parameter

Java語境中的argument和parameter,在官方檔案中給予了非常明確的說明。Oracle Java官方教程的

Passing Information to a Method or a Constructor 一節,提到:

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.

所以,在Java中,parameter指的是函式定義。而argument指的函式呼叫

因此,在Java Doc中,使用的是@param註解來說明引數含義:

    /**
     * @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.

維基百科也用形式引數(formal parameter)指代parameter,而argument則對應於實際引數(actual parameter)。

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”,and “parameter”? - Stack Overflow

《Java與Linux學習週刊》每週五發布,同步更新於:Github知乎掘金