【发布时间】:2016-01-01 13:08:21
【问题描述】:
我正在使用TCLAP 库进行一些命令行参数解析。它非常棒:除了它打印的帮助信息。这些有点丑。
例如,这是输出:
USAGE:
./a.out [-r] -n <string> [--] [--version] [-h]
Where:
-r, --reverse
Print name backwards
-n <string>, --name <string>
(required) Name to print
--, --ignore_rest
Ignores the rest of the labeled arguments following this flag.
--version
Displays version information and exits.
-h, --help
Displays usage information and exits.
Command description message
这个节目:
#include <string>
#include <iostream>
#include <algorithm>
#include <tclap/CmdLine.h>
int main(int argc, char** argv){
try {
TCLAP::CmdLine cmd("Command description message", ' ', "0.9");
TCLAP::ValueArg<std::string> nameArg("n","name","Name to print",true,"homer","string");
cmd.add( nameArg );
TCLAP::SwitchArg reverseSwitch("r","reverse","Print name backwards", cmd, false);
cmd.parse( argc, argv );
std::string name = nameArg.getValue();
bool reverseName = reverseSwitch.getValue();
if ( reverseName ){
std::reverse(name.begin(),name.end());
std::cout << "My name (spelled backwards) is: " << name << std::endl;
} else{
std::cout << "My name is: " << name << std::endl;
}
} catch (TCLAP::ArgException &e) {
std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
}
}
当使用./a.out -h 运行时。
我想要更多的创意控制:我想要制作自己的帮助信息!
我怎样才能做到这一点?
【问题讨论】:
-
你想要什么?我一直认为 automagic 帮助创作是核心力量——但有时我将输出子类化以使其更紧凑,即发烧空行。
-
我想打印一大段我自己编写和格式化的文本。
-
完全可行。将 TCLAP 子类化为 myTCLAP,将默认帮助方法替换为自定义方法,该方法将您的格式化文本作为附加 ctor 参数。
标签: c++ command-line-arguments