[Issue 11737] Allow optional string value for getopt switches

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Dec 15 10:54:17 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=11737


Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei at erdani.com,
                   |                            |andrej.mitrovich at gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-12-15 10:54:11 PST ---
I'm thinking of the following, this current code will allow --log=value, but
not --log:

-----
import std.getopt;
import std.stdio;

void main(string[] args)
{
    getopt(args,
           "log",
           (string option, string value)
           {
                // store value here
                writeln(value);
           });
}
-----

$ rdmd test.d --log=foo
> foo

$ rdmd test.d --log
> object.Exception at C:\dmd-git\dmd2\windows\bin\..\..\src\phobos\std\getop
t.d(486): Missing value for argument --log.

So it's meant to catch bugs. However I think we can extend this, and make
getopt check whether the value parameter has a default initializer:

-----
import std.getopt;
import std.stdio;

void main(string[] args)
{
    getopt(args,
           "log",
           (string option, string value = "bar")  // should allow '--log'
           {
               // value is either set (e.g. --log=foo), or set to "bar" (--log)
               writeln(value);
           });
}
-----

That way we don't break code and introduce this new feature. Andrei, thoughts?

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list