www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Help Required on Getopt

reply Vino.B <vino.bheeman hotmail.com> writes:
Hi All,

   When i run the below program without any arguments "D1.d -r" it 
is throwing error, but i need it to show the help menu

Program:
import std.stdio;
import std.getopt;
string r;
void main (string[] args)
{
  getopt(args, std.getopt.config.caseInsensitive, 
std.getopt.config.stopOnFirstNonOption, "r" , &r);
  switch (r) {
  case "Test1" : Test1; break;
  case "Test2" : Test2; break;
  default : writeln("Unknown operation"); Help;
		    }
}
void Test1 ()
{ writeln("This is Test 1");  }
void Test2 ()
{ writeln("This is Test 2"); }
void Help ()
{ writeln("This is Help Menu"); }

Execution:
Works fine if i execute the program with below options
rdmd D1.d
rdmd D1.d -r Test1
rdmd D1.d -r Test2
rdmd D1.d -r Help

but fails with below error when i execute the program as rdmd 
D1.d -r

Requirement :
If i execute the program as "rdmd D1.d -r" is should show me the 
"Help" Menu rather than the below error/

Output:
C:\Admin\Desktop\Current\Script\D>rdmd D1.d -r

object.Exception C:\D\dmd2\windows\bin\..\..\src\phobos\std\getopt.d(880):
Missing value for argument -r.
----------------
0x00402493
0x004043AC
0x00402999
0x004025FE
0x004025C5
0x0040239B
0x00402236
0x0040B47F
0x0040B443
0x0040B344
0x00405E07
0x76CB336A in BaseThreadInitThunk
0x77CF9902 in RtlInitializeExceptionChain
0x77CF98D5 in RtlInitializeExceptionChain

From,
Vino.B
Sep 01 2017
parent reply Jon Degenhardt <jond noreply.com> writes:
On Friday, 1 September 2017 at 13:13:39 UTC, Vino.B wrote:
 Hi All,

   When i run the below program without any arguments "D1.d -r" 
 it is throwing error, but i need it to show the help menu

 [snip...]
Hi Vino, To get good error message behavior you need to put the construct in a try-catch block. Then you can choose how to respond. An example here: https://github.com/eBay/tsv-utils-dlang/blob/master/tsv-append/src/tsv append.d#L138-L194. This code prints outs the error message from the exception. In your case: "Missing value for argument -r.". But, you could also print out the help text as well. There is an example of that as well in the above code block, look for the 'if (r.helpWanted)' test. --Jon
Sep 01 2017
parent reply Vino.B <vino.bheeman hotmail.com> writes:
On Friday, 1 September 2017 at 17:23:01 UTC, Jon Degenhardt wrote:
 On Friday, 1 September 2017 at 13:13:39 UTC, Vino.B wrote:
 Hi All,

   When i run the below program without any arguments "D1.d -r" 
 it is throwing error, but i need it to show the help menu

 [snip...]
Hi Vino, To get good error message behavior you need to put the construct in a try-catch block. Then you can choose how to respond. An example here: https://github.com/eBay/tsv-utils-dlang/blob/master/tsv-append/src/tsv append.d#L138-L194. This code prints outs the error message from the exception. In your case: "Missing value for argument -r.". But, you could also print out the help text as well. There is an example of that as well in the above code block, look for the 'if (r.helpWanted)' test. --Jon
Hi, Thank you very much, that helped me to resolve the issue.
Sep 01 2017
parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
I have same issue.  How this help you?  Catching exception does not help.
How do I catch exception and still print help message?

Dne 1. 9. 2017 8:10 odpoledne napsal u=C5=BEivatel "Vino.B via
Digitalmars-d-learn" <digitalmars-d-learn puremagic.com>:

On Friday, 1 September 2017 at 17:23:01 UTC, Jon Degenhardt wrote:

 On Friday, 1 September 2017 at 13:13:39 UTC, Vino.B wrote:

 Hi All,

   When i run the below program without any arguments "D1.d -r" it is
 throwing error, but i need it to show the help menu

 [snip...]
Hi Vino, To get good error message behavior you need to put the construct in a try-catch block. Then you can choose how to respond. An example here: https://github.com/eBay/tsv-utils-dlang/blob/master/tsv-appe nd/src/tsv-append.d#L138-L194. This code prints outs the error message from the exception. In your case: "Missing value for argument -r.". But, you could also print out the help text as well. There is an example of th=
at
 as well in the above code block, look for the 'if (r.helpWanted)' test.

 --Jon
Hi, Thank you very much, that helped me to resolve the issue.
Sep 01 2017
parent Jon Degenhardt <jond noreply.com> writes:
On Friday, 1 September 2017 at 19:04:39 UTC, Daniel Kozak wrote:
 I have same issue.  How this help you?  Catching exception does 
 not help. How do I catch exception and still print help message?
Your are correct, sorry about that. What my response showed is how to avoid printing the full stack trace and instead printing a more nicely formatted error message. And separately, how to print formatted help. But, you are correct in that you can't directly print the formatted help text from the catch block as shown. In particular, the GetoptResult returned by getopt is not available. I don't have any examples that try to work around this. Presumably one could call getopt again to get the options list, then generate the formatted help. It'd be an annoyance, though perhaps judicious use of AliasSeq might make the code structure reasonable. --Jon
Sep 01 2017