妥善处理 std.getopt.GetOptException
Proper handling of std.getopt.GetOptException
如何正确处理缺少必需的命令行参数时抛出的 std.getopt.GetOptException
异常。
声明需要的参数会引发以下错误,方式 过于冗长:
std.getopt.GetOptException@/Library/D/dmd/src/phobos/std/getopt.d(755): Required option file|f was not supplied
----------------
/Library/D/dmd/src/phobos/std/format.d-mixin-1127:1138 @safe std.getopt.GetoptResult std.getopt.getopt!(std.getopt.config, immutable(char)[], immutable(char)[], immutable(char)[]*, std.getopt.config, immutable(char)[], immutable(char)[], immutable(char)[]*, immutable(char)[], immutable(char)[], bool*, immutable(char)[], immutable(char)[], bool*).getopt(ref immutable(char)[][], std.getopt.config, immutable(char)[], immutable(char)[], immutable(char)[]*, std.getopt.config, immutable(char)[], immutable(char)[], immutable(char)[]*, immutable(char)[], immutable(char)[], bool*, immutable(char)[], immutable(char)[], bool*) [0xbb5a9d1]
source/app.d:11 _Dmain [0xbb58996]
std.getopt.GetOptException@/Library/D/dmd/src/phobos/std/getopt.d(755): Required option key|k was not supplied
使用以下代码重现:
import std.stdio;
import std.getopt;
int main(string[] args)
{
string key;
string inputFile;
bool encrypt;
bool decrypt;
auto result = getopt(
args,
std.getopt.config.required,
"key|k", "The key to use", &key,
std.getopt.config.required,
"file|f", "The file to encrypt/decrypt", &inputFile,
"encrypt|e", "Encrypt the file", &encrypt,
"decrypt|d", "Decrypt the file", &decrypt
);
if (result.helpWanted) {
defaultGetoptPrinter("Some information about the program.", result.options);
}
return 0;
}
一个合理的方法是将 getopt
调用包装在 try-catch
块中,捕获任何错误,并在退出前仅写入错误消息部分。这将避免写入堆栈跟踪,这对大多数用户没有帮助。修改上面的例子:
import std.stdio;
import std.getopt;
int main(string[] args)
{
string key;
string inputFile;
bool encrypt;
bool decrypt;
try {
auto result = getopt(
args,
std.getopt.config.required,
"key|k", "The key to use", &key,
std.getopt.config.required,
"file|f", "The file to encrypt/decrypt", &inputFile,
"encrypt|e", "Encrypt the file", &encrypt,
"decrypt|d", "Decrypt the file", &decrypt
);
if (result.helpWanted) {
defaultGetoptPrinter("Some information about the program.", result.options);
}
}
catch (Exception e) {
stderr.writefln("Error processing command line arguments: %s", e.msg);
return 1;
}
return 0;
}
运行时的错误信息:
Error processing command line arguments: Required option file|f was not supplied
D 语言 'Learn' 论坛上有一个与此相关的帖子可能有用:What's the proper way to use std.getopt?. I've written some open source command line tools that are a bit more extensive examples, eg. tsv-sample.d。
如何正确处理缺少必需的命令行参数时抛出的 std.getopt.GetOptException
异常。
声明需要的参数会引发以下错误,方式 过于冗长:
std.getopt.GetOptException@/Library/D/dmd/src/phobos/std/getopt.d(755): Required option file|f was not supplied
----------------
/Library/D/dmd/src/phobos/std/format.d-mixin-1127:1138 @safe std.getopt.GetoptResult std.getopt.getopt!(std.getopt.config, immutable(char)[], immutable(char)[], immutable(char)[]*, std.getopt.config, immutable(char)[], immutable(char)[], immutable(char)[]*, immutable(char)[], immutable(char)[], bool*, immutable(char)[], immutable(char)[], bool*).getopt(ref immutable(char)[][], std.getopt.config, immutable(char)[], immutable(char)[], immutable(char)[]*, std.getopt.config, immutable(char)[], immutable(char)[], immutable(char)[]*, immutable(char)[], immutable(char)[], bool*, immutable(char)[], immutable(char)[], bool*) [0xbb5a9d1]
source/app.d:11 _Dmain [0xbb58996]
std.getopt.GetOptException@/Library/D/dmd/src/phobos/std/getopt.d(755): Required option key|k was not supplied
使用以下代码重现:
import std.stdio;
import std.getopt;
int main(string[] args)
{
string key;
string inputFile;
bool encrypt;
bool decrypt;
auto result = getopt(
args,
std.getopt.config.required,
"key|k", "The key to use", &key,
std.getopt.config.required,
"file|f", "The file to encrypt/decrypt", &inputFile,
"encrypt|e", "Encrypt the file", &encrypt,
"decrypt|d", "Decrypt the file", &decrypt
);
if (result.helpWanted) {
defaultGetoptPrinter("Some information about the program.", result.options);
}
return 0;
}
一个合理的方法是将 getopt
调用包装在 try-catch
块中,捕获任何错误,并在退出前仅写入错误消息部分。这将避免写入堆栈跟踪,这对大多数用户没有帮助。修改上面的例子:
import std.stdio;
import std.getopt;
int main(string[] args)
{
string key;
string inputFile;
bool encrypt;
bool decrypt;
try {
auto result = getopt(
args,
std.getopt.config.required,
"key|k", "The key to use", &key,
std.getopt.config.required,
"file|f", "The file to encrypt/decrypt", &inputFile,
"encrypt|e", "Encrypt the file", &encrypt,
"decrypt|d", "Decrypt the file", &decrypt
);
if (result.helpWanted) {
defaultGetoptPrinter("Some information about the program.", result.options);
}
}
catch (Exception e) {
stderr.writefln("Error processing command line arguments: %s", e.msg);
return 1;
}
return 0;
}
运行时的错误信息:
Error processing command line arguments: Required option file|f was not supplied
D 语言 'Learn' 论坛上有一个与此相关的帖子可能有用:What's the proper way to use std.getopt?. I've written some open source command line tools that are a bit more extensive examples, eg. tsv-sample.d。