[Issue 14725] New: std.getopt: improve error message for malformed arguments

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Jun 23 09:33:35 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14725

          Issue ID: 14725
           Summary: std.getopt: improve error message for malformed
                    arguments
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: initrd.gz at gmail.com

When parsing an argument that isn't a string (ex. an int), getopt just uses
`std.conv.to`, which throws a ConvException that's not very helpful to the end
user.

For example:


import std.stdio;
import std.getopt;
import std.conv;

void main(string[] args) {
    int num;

    GetoptResult helpinfo;
    try {
        helpinfo = getopt(args,
            "num", "An integer", &num,
        );
    } catch(ConvException ex) {
        stderr.writeln("Error parsing arguments: ", ex.msg);
        return;
    }

    if(helpinfo.helpWanted) {
        defaultGetoptPrinter("I am help text", helpinfo.options);
    }
}


$ rdmd ~/test.d --num=foo
Error parsing arguments: Unexpected 'o' when converting from type string to
type int

It would be nicer if getopt caught the ConvException, then threw a
GetOptException with a more end-user-friendly message. For example:

$ rdmd ~/test.d --num=foo
Error parsing arguments: `num` was passed a non-integer value.

--


More information about the Digitalmars-d-bugs mailing list