Smalltalk中的未记录的getopt

时间:2011-08-06 18:50:52

标签: command-line-arguments smalltalk getopt gnu-smalltalk

我正在编写一个加密密码和解密密码哈希的命令行应用程序。 Getopt的文档没有提供任何示例,因此我不知道如何使用Getopt类。 mailing list logs中散布了一些线索。

特别是,我不知道with: pattern的格式,指定CLI参数的字符串以及值是必需的,可选的还是省略的。

ios7crypt.st:

"exec" "gst" "-f" "$0" "$0" "$@"
"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

1 个答案:

答案 0 :(得分:1)

我建议你在调试时使用#printString。它将提供面向程序员的表示(而不是#displayString)。

顺便说一句,你可以在shebang调用中使用“$ @”,这样就不会加入参数。