mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 22:50:38 +03:00
Added a style parameter to defaultGetoptFormatter.
This commit is contained in:
parent
30c72c0479
commit
f14cc0bcdd
1 changed files with 33 additions and 3 deletions
36
std/getopt.d
36
std/getopt.d
|
@ -1635,14 +1635,15 @@ void defaultGetoptPrinter(string text, Option[] opt)
|
||||||
|
|
||||||
/** This function writes the passed text and `Option` into an output range
|
/** This function writes the passed text and `Option` into an output range
|
||||||
in the manner described in the documentation of function
|
in the manner described in the documentation of function
|
||||||
`defaultGetoptPrinter`.
|
`defaultGetoptPrinter`, unless the style option is used.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
output = The output range used to write the help information.
|
output = The output range used to write the help information.
|
||||||
text = The text to print at the beginning of the help output.
|
text = The text to print at the beginning of the help output.
|
||||||
opt = The `Option` extracted from the `getopt` parameter.
|
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.algorithm.comparison : min, max;
|
||||||
import std.format : formattedWrite;
|
import std.format : formattedWrite;
|
||||||
|
@ -1663,7 +1664,7 @@ void defaultGetoptFormatter(Output)(Output output, string text, Option[] opt)
|
||||||
|
|
||||||
foreach (it; 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);
|
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(c == '-');
|
||||||
assert(f == "-");
|
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);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue