Thread overview
[Issue 5228] New: Add GetOptException (or similar) to std.getopt
Nov 17, 2010
Jonathan M Davis
Nov 17, 2010
Jonathan M Davis
May 25, 2011
Andrej Mitrovic
November 17, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5228

           Summary: Add GetOptException (or similar) to std.getopt
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: jmdavisProg@gmx.com


--- Comment #0 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-11-16 19:20:35 PST ---
Right now, if there's a problem with std.getopt, you typically either get a regular Exception or a ConvError (soon to be ConvException). This makes it a bit cumbersome to specifically grab errors pertaining to problems parsing command-line arguments. Having an Exception thrown for doing something like passing an empty array to getopt() probably makes some sense, but ideally, there would be a specific exception type for problems involving parsing command-line arguments - such as GetOptException, or something similar.

What would also be particularly useful is if GetOptException gave you specific information about the error beyond just toString(). Being able to get at the specific error message without any stack trace would be nice. In particular, knowing which option it was trying to get when it failed would be useful.

Right now, if getopt fails, you either catch any and all exceptions (and errors, thanks to ConvError) and have to somehow figure out what went wrong to somehow print a useful error message (or just print a generic error message), or you have to let the exception escape and have it be printed. And while have the exception be printed is fine for quick scripts and the like, for end-user applications, the user shouldn't be seeing stack traces or exception messages which include source file names and the like. You should be able to print intelligent errors about what they entered incorrectly. And right now, you can't do that.

So, I'd like GetOptException to be added and then used for all errors pertaining to problems parsing the command-line options and which aren't programmer errors  (such as passing an empty array). Also, I'd like it to give enough information to be able to easily print out exactly what went wrong - in particular, which option failed to be handled correctly and why.

std.getopt.getopt is extremely useful, but it needs better error reporting if it's going to be used for serious applications.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 17, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5228



--- Comment #1 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-11-16 20:55:47 PST ---
I feel like a moderate idiot. I didn't realize that you could get an exception's message by accessing its public member variable msg. So, the situation is not quite as bad as I thought that it was. However, that doesn't tell you which option failed to parse correctly (unless you parse the message), which would be nice, and being able to explicitly catch GetOptException would still be a big improvement. You can, however, wrap getopt() in a try-catch block and print the exception's message to get something better than a generic error message.

Regardless, I think that GetOptException would be a good addition to std.getopt.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5228


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2011-05-24 20:23:44 PDT ---
It can also throw a UnicodeException. Even for things like this:

main.exe -

A single slash throws: core.exception.UnicodeException@(1244708): invalid UTF-8
sequence

This should really be wrapped in some kind of a getopt exception.

I'd further add that appart from getting some information on which option failed to parse, getopt might try to figure out what went wrong in other situations and create a custom exception message based on it. For example if you just pass a single dash getopt would throw GetOptException("Failed to provide an argument after single dash."), and you could either catch this and ignore it, or display it to the user:

void main()
{
    try { // parse args }
    catch (GetOptException exc)
    {
        writeln(exc.toString);  // tell the user what went wrong,
                                // but maybe continue operating
    }
}

Anywho, +1 for the enhancement.

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