Below is a paste of the page at http://www.curoles.com/j/getoptex.html. The code should be added to std.getopt, perhaps as an overload of the current function. Thanks Igor! I will add your name to the authors list when I make the change.
getoptex
One way to extend Phobos library std.getopt.getopt function is make a wrapper around it that could be feeded with usage information about every option and let it automatically gather all usage strings into one usage/help message block, similarly to Boost program_options library
Links:
http://en.wikipedia.org/wiki/Tuplehttp://www.digitalmars.com/d/2.0/phobos/std_getopt.htmlhttp://www.digitalmars.com/d/2.0/phobos/std_typetuple.html
Below is an example how getoptEx could be used:
import getoptex;
import std.stdio;
void main(string[] args)
{
string inputFile, outputFile;
bool helpPrinted = getoptEx(
"Test program to demonstrate getoptEx\n"~
"written by Igor Lesik on Feb 2010\n"~
"Usage: test1 { --switch }\n",
args,
std.getopt.config.caseInsensitive,
"input",
"\tinput file name,\n"~
"\tmust be html file",
&inputFile,
"output",
"\toutput file name",
&outputFile,
"author",
"\tprint name of\n"~
"\tthe author",
delegate() {writeln("Igor Lesik");}
);
if (helpPrinted)
return;
writeln("Input file name:", inputFile);
writeln("Output file name:", outputFile);
}
If you call the program with --help option, then output is:
>test1.exe --help
Test program to demonstrate getoptEx
written by Igor Lesik on Feb 2010
Usage: test1 { --switch }
--input
input file name,
must be html file
--output
output file name
--author
print name of
the author
--help
produce help message
getopEx implementation:
module getoptex;
import std.stdio;
import std.getopt;
private import std.contracts;
private import std.typetuple;
private import std.conv;
bool getoptEx(T...)(string helphdr, ref string[] args, T opts)
{
enforce(args.length,
"Invalid arguments string passed: program name missing");
string helpMsg = GetoptHelp(opts); // extract all help strings
bool helpPrinted = false; // state tells if called with "--help"
void printHelp()
{
writeln("\n", helphdr, "\n", helpMsg,
"--help", "\n\tproduce help message");
helpPrinted = true;
}
getopt(args, GetoptEx!(opts), "help", &printHelp);
return helpPrinted;
}
private template GetoptEx(TList...)
{
static if (TList.length)
{
static if (is(typeof(TList[0]) : config))
{
// it's a configuration flag, lets move on
alias TypeTuple!(TList[0],GetoptEx!(TList[1 .. $])) GetoptEx;
}
else
{
// it's an option string, eat help string
alias TypeTuple!(TList[0],TList[2],GetoptEx!(TList[3 .. $])) GetoptEx;
}
}
else
{
alias TList GetoptEx;
}
}
private string GetoptHelp(T...)(T opts)
{
static if (opts.length)
{
static if (is(typeof(opts[0]) : config))
{
// it's a configuration flag, skip it
return GetoptHelp(opts[1 .. $]);
}
else
{
// it's an option string
string option = to!(string)(opts[0]);
string help = to!(string)(opts[1]);
return( "--"~option~"\n"~help~"\n"~GetoptHelp(opts[3 .. $]) );
}
}
else
{
return to!(string)("\n");
}
}
Page generated by Ddoc.
Comment #1 by andrei — 2011-06-05T20:12:47Z
I can't find the code for download. Does anyone have Igor Lesik's email address?
Comment #2 by curoles — 2011-06-14T20:49:23Z
I will see how I can work it into github, meanwhile I paste the original page as comment here. Igor
getoptex
One way to extend Phobos library std.getopt.getopt function is make a wrapper around it that could be feeded with usage information about every option and let it automatically gather all usage strings into one usage/help message block, similarly to Boost program_options library
Links:
http://en.wikipedia.org/wiki/Tuplehttp://www.digitalmars.com/d/2.0/phobos/std_getopt.htmlhttp://www.digitalmars.com/d/2.0/phobos/std_typetuple.html
Below is an example how getoptEx could be used:
import getoptex;
import std.stdio;
void main(string[] args)
{
string inputFile, outputFile;
bool helpPrinted = getoptEx(
"Test program to demonstrate getoptEx\n"~
"written by Igor Lesik on Feb 2010\n"~
"Usage: test1 { --switch }\n",
args,
std.getopt.config.caseInsensitive,
"input",
"\tinput file name,\n"~
"\tmust be html file",
&inputFile,
"output",
"\toutput file name",
&outputFile,
"author",
"\tprint name of\n"~
"\tthe author",
delegate() {writeln("Igor Lesik");}
);
if (helpPrinted)
return;
writeln("Input file name:", inputFile);
writeln("Output file name:", outputFile);
}
If you call the program with --help option, then output is:
>test1.exe --help
Test program to demonstrate getoptEx
written by Igor Lesik on Feb 2010
Usage: test1 { --switch }
--input
input file name,
must be html file
--output
output file name
--author
print name of
the author
--help
produce help message
getopEx implementation:
module getoptex;
import std.stdio;
import std.getopt;
private import std.contracts;
private import std.typetuple;
private import std.conv;
bool getoptEx(T...)(string helphdr, ref string[] args, T opts)
{
enforce(args.length,
"Invalid arguments string passed: program name missing");
string helpMsg = GetoptHelp(opts); // extract all help strings
bool helpPrinted = false; // state tells if called with "--help"
void printHelp()
{
writeln("\n", helphdr, "\n", helpMsg,
"--help", "\n\tproduce help message");
helpPrinted = true;
}
getopt(args, GetoptEx!(opts), "help", &printHelp);
return helpPrinted;
}
private template GetoptEx(TList...)
{
static if (TList.length)
{
static if (is(typeof(TList[0]) : config))
{
// it's a configuration flag, lets move on
alias TypeTuple!(TList[0],GetoptEx!(TList[1 .. $])) GetoptEx;
}
else
{
// it's an option string, eat help string
alias TypeTuple!(TList[0],TList[2],GetoptEx!(TList[3 .. $])) GetoptEx;
}
}
else
{
alias TList GetoptEx;
}
}
private string GetoptHelp(T...)(T opts)
{
static if (opts.length)
{
static if (is(typeof(opts[0]) : config))
{
// it's a configuration flag, skip it
return GetoptHelp(opts[1 .. $]);
}
else
{
// it's an option string
string option = to!(string)(opts[0]);
string help = to!(string)(opts[1]);
return( "--"~option~"\n"~help~"\n"~GetoptHelp(opts[3 .. $]) );
}
}
else
{
return to!(string)("\n");
}
}
Comment #3 by jens.k.mueller — 2011-06-15T10:30:26Z
Igor, I already have the code in my local repository. I will create pull request if you don't mind and make you the author of that commit.
Comment #4 by hoganmeier — 2012-01-03T06:04:23Z
Where is that pull request?
Comment #5 by jens.k.mueller — 2012-01-03T07:15:19Z
It's still in my local repository. The point is that getting the other changes in is a bit problematic due to changing of defaults and breaking code. And the other getopt changes may break code. That's why I wanted to have all changes (including generating usage information) in one pull request.
At the moment there is no decision whether breaking old code is worth it. That got me distracted. But I should finish the code. I will provide a new function called getOptions to avoid breaking any code since there is some controversy about the proper defaults and similar.
Comment #6 by golovanov_alexey — 2012-05-20T14:05:16Z
Any news?
Comment #7 by golovanov_alexey — 2012-12-22T00:48:39Z
Any news?
Comment #8 by andrei — 2013-02-26T09:21:14Z
Didn't have time to get to this, and probably won't for a while. Could anyone take it over?
Comment #9 by jens.k.mueller — 2013-02-26T15:16:56Z
There is my pull request https://github.com/D-Programming-Language/phobos/pull/1030.
If somebody comments I'm happy to finish it. The reason why I was less willing to finish it is that I don't like implementing stuff that nobody would use. So please comment on the API from a usage point of view and I will implement it.
BTW Recently I was wondering whether it is the right way to throw exceptions because I find that this is not a rare situation. Actually it's a pretty common situation to do some usage error. But either way I want to finish it.
Comment #10 by golovanov_alexey — 2013-03-25T13:07:21Z
> If somebody comments I'm happy to finish it.
Good job! Thanx!
> I don't like implementing stuff that nobody would use.
Nobody? I have no statistics. But, IMHO, everybody, who write small command line utilities with D, today "invent the wheel" every time.
Best way - to have this features in std library.
Comment #11 by jcrapuchettes — 2013-11-18T16:48:49Z
We use something like this in my company. It would be really nice to have it part of phobos.
Comment #12 by jens.k.mueller — 2013-11-19T00:13:00Z
I don't have the time to finish this.
You can use my changes (if you want) to improve Phobos' getopt.
Comment #13 by bus_dbugzilla — 2014-03-24T18:10:46Z
This report is somewhat vague. What does seem clear is the expectation of an optional way to help automate generating a help screen. But beyond that, can we get clarification on the scope and expectations of this enhancement request?
Comment #14 by rburners — 2014-03-26T06:11:24Z
Is anyone still working on this? I would like to take over.
Comment #15 by andrei — 2014-03-26T06:17:57Z
@Rpbert ask Andrej Mitrovic
Comment #16 by andrej.mitrovich — 2014-03-26T07:16:36Z
Comment #18 by jens.k.mueller — 2014-09-22T08:41:31Z
I'm a little bit confused.
The changelog says it was fixed in 2.066 (http://dlang.org/changelog.html). I'm
using dmd.2.066.0.zip. Further its documentation does not look like it contains
any new documentation regarding this feature (http://dlang.org/phobos/std_getopt.html).
So I assume the changelog is just wrong.
Comment #19 by rburners — 2014-09-22T09:06:24Z
the page doc is out of sync
Comment #20 by jens.k.mueller — 2014-09-22T09:15:37Z
The changes are also not in dmd.2.066.0.zip which makes me think the changelog is in error. Or the 2.066 release.
Comment #21 by rburners — 2014-09-22T10:06:07Z
You're right, I just checked and it is not in 2.066. I though it got in
Comment #22 by golovanov_alexey — 2014-10-22T19:22:45Z
getopt.d contain "getoptX" strings (in ddoc comments).
something not done?