Added a style parameter to defaultGetoptFormatter.

This commit is contained in:
Jonathan Samson 2019-12-11 19:05:00 +01:00
parent 30c72c0479
commit f14cc0bcdd

View file

@ -1635,14 +1635,15 @@ void defaultGetoptPrinter(string text, Option[] opt)
/** This function writes the passed text and `Option` into an output range
in the manner described in the documentation of function
`defaultGetoptPrinter`.
`defaultGetoptPrinter`, unless the style option is used.
Params:
output = The output range used to write the help information.
text = The text to print at the beginning of the help output.
opt = The `Option` extracted from the `getopt` parameter.
style = The manner in which to display the output of each `Option.`
*/
void defaultGetoptFormatter(Output)(Output output, string text, Option[] opt)
void defaultGetoptFormatter(Output)(Output output, string text, Option[] opt, string style = "%*s %*s%*s%s\n")
{
import std.algorithm.comparison : min, max;
import std.format : formattedWrite;
@ -1663,7 +1664,7 @@ void defaultGetoptFormatter(Output)(Output output, string text, Option[] opt)
foreach (it; opt)
{
output.formattedWrite("%*s %*s%*s%s\n", ls, it.optShort, ll, it.optLong,
output.formattedWrite(style, ls, it.optShort, ll, it.optLong,
hasRequired ? re.length : 1, it.required ? re : " ", it.help);
}
}
@ -1864,3 +1865,32 @@ void defaultGetoptFormatter(Output)(Output output, string text, Option[] opt)
assert(c == '-');
assert(f == "-");
}
@system unittest
{
import std.conv;
import std.array;
import std.string;
bool a;
auto args = ["prog", "--foo"];
auto t = getopt(args, "foo|f", "Help", &a);
string s;
auto app = appender!string();
defaultGetoptFormatter(app, "Some Text", t.options, "\t\t%*s %*s%*s\n%s\n");
string helpMsg = app.data;
//writeln(helpMsg);
assert(helpMsg.length);
assert(helpMsg.count("\n") == 5, to!string(helpMsg.count("\n")) ~ " "
~ helpMsg);
assert(helpMsg.indexOf("--foo") != -1);
assert(helpMsg.indexOf("-f") != -1);
assert(helpMsg.indexOf("-h") != -1);
assert(helpMsg.indexOf("--help") != -1);
assert(helpMsg.indexOf("Help") != -1);
string wanted = "Some Text\n\t\t-f --foo \nHelp\n\t\t-h --help \nThis help "
~ "information.\n";
assert(wanted == helpMsg);
}