Allowing string value with getopt without requiring it

John Colvin john.loughran.colvin at gmail.com
Fri Dec 13 01:46:57 PST 2013


On Friday, 13 December 2013 at 01:51:25 UTC, Ithy wrote:
> Hello. I'm just getting into D (literally writing my first 
> program), and I can't figure out how to do this with getopt:
>
> I want to have the option --log, which will enable logging (set 
> a bool variable to true). However, I want the program to also 
> accept --log=filename to allow the user to define custom log 
> file. If --log is used without filename, a default file will be 
> used.
>
> I tried this:
>
> getopt(args,
>     "log", &log,
>     "log", &logFile);
>
> Passing just --log to the program works fine, the boolean is 
> set to true. However, using --log=test.log throws an exception, 
> and seemingly getopt expects either true or false.
>
> How would I go about implementing this without creating another 
> option like --log-file for custom file name?

That doesn't seem possible with getopt.

I would use std.getopt.config.passThrough and then deal with the 
"log" option manually afterwards.


More information about the Digitalmars-d-learn mailing list