2011-08-06 59 views
1

我正在編寫一個命令行應用程序,用於加密密碼並解密密碼散列。 Getopt的文檔沒有提供任何示例,所以我不知道如何使用Getopt類。一些線索分散在mailing list logsSmalltalk中沒有文檔的getopt

特別是,我不知道格式with: pattern,指定CLI參數的字符串以及值是必需的,可選的還是省略的。

ios7crypt.st:

"exec" "gst" "-f" "$0" "$0" "[email protected]" 
"exit" 

| args password hash | 

"Drop the program name." 
"The shebang joins the arguments; we must split them." 
args := (Smalltalk getArgv: 2) substrings: $ . 

args do: [ :arg | Transcript show: 'Raw arg: ', arg; cr. ]. 

Getopt parse: args with: '-e: -d: -t' do: [ :opt :arg | 
    Transcript show: 'Opt: ', (opt asString), ' Arg: ', arg; cr. 

    "..." 
]. 

實例運行:

$ ./ios7crypt.st -e monkey 
Raw arg: -e 
Raw arg: monkey 
Opt: e Arg: monkey 

此前,雙方-emonkey被傳遞到腳本,但的Getopt的do:默默地下降-e,使輸出看起來像:

$ ./ios7crypt.st -e monkey 
Raw arg: -e 
Raw arg: monkey 
Opt: Arg: monkey 
+0

啊,這是因爲:opt是一個字符,而不是一個字符串,因此不能在'Transcript show:'後面加'''(逗號)「。 – mcandre

回答

1

我建議你在調試時使用#printString。它將提供面向程序員的表示(與#displayString相反)。

順便說一句,你可以在shebang調用中使用「$ @」,這樣參數就不會被加入。