Issue 14725 - std.getopt: improve error message for malformed arguments
Summary: std.getopt: improve error message for malformed arguments
Status: ASSIGNED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-06-23 16:33 UTC by Alex Parrill
Modified: 2024-12-01 16:24 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Alex Parrill 2015-06-23 16:33:35 UTC
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.
Comment 1 Robert Schadek 2015-07-15 09:15:27 UTC
I will work on that
Comment 3 dlangBugzillaToGithub 2024-12-01 16:24:42 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9662

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB