From f14cc0bcdd2e301ef5f4c4e86be3463ef6cb5a07 Mon Sep 17 00:00:00 2001 From: Jonathan Samson Date: Wed, 11 Dec 2019 19:05:00 +0100 Subject: [PATCH] Added a style parameter to defaultGetoptFormatter. --- std/getopt.d | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/std/getopt.d b/std/getopt.d index eb29e981a..fdf1095f0 100644 --- a/std/getopt.d +++ b/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 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); +}