Thread overview
Allowing string value with getopt without requiring it
Dec 13, 2013
Ithy
Dec 13, 2013
John Colvin
Dec 13, 2013
H. S. Teoh
December 13, 2013
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?
December 13, 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.
December 13, 2013
On Fri, Dec 13, 2013 at 10:46:57AM +0100, John Colvin wrote:
> 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.

I would file an enhancement request in the bugtracker. This is a pretty common idiom in command-line parsing.

	http://d.puremagic.com/issues/


T

-- 
We are in class, we are supposed to be learning, we have a teacher... Is it too much that I expect him to teach me??? -- RL