import std.traits;
import std.getopt;
struct Options
{
@Option("threads", "t")
@Help("要用线程数")
size_t threads;
@Option("file")
@Help("输入文件")
string[] files;
}
void main(string[] args) {
Options prop;
string GenerateGetopt(alias Options)() pure {
import std.meta;
import std.typecons;
import std.format;
auto ans = `auto helpInfo = getopt(args, `;
static foreach(opt; FieldNameTuple!Options) {
ans ~= format("getUDAs!(prop.%s, Option)[0].cononical(),"
~" getUDAs!(prop.%s, Help)[0].msg, &prop.%s,", opt, opt, opt);
}
return ans ~ ");";
}
mixin(GenerateGetopt!Options);
if (helpInfo.helpWanted)
{
defaultGetoptPrinter("选项: ",helpInfo.options);
}
}
struct Help {
string msg;
}
struct Option {
string[] names;
this(string[] names...) {
this.names = names;
}
string cononical() {
import std.algorithm;
import std.conv;
return names.joiner("|").to!string;
}
}