www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - crash on args.getopt

reply "Suliman" <evermind live.ru> writes:
First of all it's seems bug in docs:
void main(string[] args)
{
   getopt(
     args,
     "length",  &length,    // numeric
     "file",    &data,      // string
     "verbose", &verbose,   // flag
     "color",   &color);    // enum
   ...
}

with args inside getopt I am getting:
C:\D\dmd2\windows\bin\..\..\src\phobos\std\getopt.d(547): 
Deprecation: using * o
n an array is deprecated; use *(receiver).ptr instead
C:\D\dmd2\windows\bin\..\..\src\phobos\std\getopt.d(547): Error: 
cannot modify i
mmutable expression *cast(immutable(char)*)receiver
C:\D\dmd2\windows\bin\..\..\src\phobos\std\getopt.d(548): 
Deprecation: using * o
n an array is deprecated; use *(receiver).ptr instead

http://dlang.org/phobos/std_getopt.html

But problem that I do not know how handle not existing values:

void main(string[] args)
{
	args.getopt
	(
		"help", &help
	);
}

app.exe -sss
causes crash:
std.getopt.GetOptException C:\D\dmd2\windows\bin\..\..\src\phobos\std\getopt.d(4
63): Unrecognized option -sss
----------------
0x00453F0E in  safe void std.getopt.getoptImpl!().getoptImpl(ref 
immutable(char)
[][], ref std.getopt.configuration) at 
C:\D\dmd2\windows\bin\..\..\src\phobos\st
d\getopt.d(463)
Jan 24 2015
next sibling parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
 http://dlang.org/phobos/std_getopt.html

 But problem that I do not know how handle not existing values:

 void main(string[] args)
 {
 	args.getopt
 	(
 		"help", &help
 	);
 }

 app.exe -sss
 causes crash:
 std.getopt.GetOptException C:\D\dmd2\windows\bin\..\..\src\phobos\std\getopt.d(4
 63): Unrecognized option -sss
 ----------------
 0x00453F0E in  safe void 
 std.getopt.getoptImpl!().getoptImpl(ref immutable(char)
 [][], ref std.getopt.configuration) at 
 C:\D\dmd2\windows\bin\..\..\src\phobos\st
 d\getopt.d(463)
Look for "Passing unrecognized options through" in the documentation.
Jan 24 2015
parent reply "Suliman" <evermind live.ru> writes:
 Look for "Passing unrecognized options through" in the  
 documentation.
Oh I see, but first part of question is still actual. And also what is benefits of using getopt instead of parsing args[] manually?
Jan 24 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Sat, 24 Jan 2015 19:55:10 +0000, Suliman wrote:

 Look for "Passing unrecognized options through" in the documentation.
Oh I see, but first part of question is still actual. =20 And also what is benefits of using getopt instead of parsing args[] manually?
well... you can skip writing custom parser and use `getopt` instead. the=20 documentation has alot of examples of what `getopt` can do for you.=
Jan 24 2015
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/24/2015 11:39 AM, Suliman wrote:

 First of all it's seems bug in docs:
 void main(string[] args)
 {
    getopt(
      args,
      "length",  &length,    // numeric
      "file",    &data,      // string
      "verbose", &verbose,   // flag
      "color",   &color);    // enum
    ...
 }

 with args inside getopt I am getting:
 C:\D\dmd2\windows\bin\..\..\src\phobos\std\getopt.d(547): Deprecation:
 using * o
 n an array is deprecated; use *(receiver).ptr instead
What version is your compiler? The example compiles as is with git head dmd (after removing the ellipsis): import std.getopt; string data = "file.dat"; int length = 24; bool verbose; enum Color { no, yes }; Color color; void main(string[] args) { getopt( args, "length", &length, // numeric "file", &data, // string "verbose", &verbose, // flag "color", &color); // enum } Ali
Jan 24 2015
parent reply "Suliman" <evermind live.ru> writes:
Ali, you are right it's my error:

getopt(
   args,

and I did:
args.getopt
(
args,


Am I right understand that "args" in "args.getopt" is UFCS syntax 
style?
Jan 24 2015
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 25 Jan 2015 05:34:15 +0000, Suliman wrote:

 Ali, you are right it's my error:
=20
 getopt(
    args,
=20
 and I did:
 args.getopt (
 args,
=20
=20
 Am I right understand that "args" in "args.getopt" is UFCS syntax style?
exactly.=
Jan 24 2015
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 25 Jan 2015 05:34:15 +0000, Suliman wrote:

 Ali, you are right it's my error:
=20
 getopt(
    args,
=20
 and I did:
 args.getopt (
 args,
=20
=20
 Am I right understand that "args" in "args.getopt" is UFCS syntax style?
just a minor detail: "UFCS style", as "S" un "UFCS" means "syntax". ;-)=
Jan 24 2015
parent reply "Suliman" <evermind live.ru> writes:
But is it good practice to fail with exception during passing 
unknown parameters? Maybe std.getopt.config.passThrough should be 
as default?

I really can't remember Apps that crush if pass to in unknown 
parameters.
Jan 25 2015
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
On Sunday, 25 January 2015 at 10:21:34 UTC, Suliman wrote:
 But is it good practice to fail with exception during passing 
 unknown parameters? Maybe std.getopt.config.passThrough should 
 be as default?

 I really can't remember Apps that crush if pass to in unknown 
 parameters.
Almost all programs fail with an error message, if you pass unknown parameter. Just catch that exception.
Jan 25 2015
parent FG <home fgda.pl> writes:
On 2015-01-25 at 11:42, Tobias Pankrath wrote:
 On Sunday, 25 January 2015 at 10:21:34 UTC, Suliman wrote:
 But is it good practice to fail with exception during passing unknown
parameters? Maybe std.getopt.config.passThrough should be as default?

 I really can't remember Apps that crush if pass to in unknown parameters.
Almost all programs fail with an error message, if you pass unknown parameter. Just catch that exception.
It's much better to fail up front than to create the illusion that everything is fine after the user has mistyped one of the parameters. Let's say "foo a b" copies from a to b, but "foo --reverse a b" does the opposite. Then, when someone types for example "foo --revrse a b", and it is silently accepted, the program does exactly the opposite of what the user expects! A side note: GTK applications accept extra parameters used to initialize GTK, but it is not passThrough. What really happens is that the app calls gtk_init(&argc, &argv) (or Main.init(args) in case of GtkD) before it starts parsing arguments on its own. The call to gtk_init removes the options recognized by GTK from the list of program arguments, and this filtered array can be then processed in a normal way -- which means that whichever parameters are still left unrecognised, the program should fail with an error message.
Jan 25 2015