Jump to page: 1 2
Thread overview
crash on args.getopt
Jan 24, 2015
Suliman
Jan 24, 2015
Tobias Pankrath
Jan 24, 2015
Suliman
Jan 24, 2015
ketmar
Jan 24, 2015
Ali Çehreli
Jan 25, 2015
Suliman
Jan 25, 2015
ketmar
Jan 25, 2015
ketmar
Jan 25, 2015
Suliman
Jan 25, 2015
Tobias Pankrath
Jan 25, 2015
FG
January 24, 2015
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)
January 24, 2015
> 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.

January 24, 2015
> 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?

January 24, 2015
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.
> 
> 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 documentation has alot of examples of what `getopt` can do for you.

January 24, 2015
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

January 25, 2015
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?
January 25, 2015
On Sun, 25 Jan 2015 05:34:15 +0000, Suliman wrote:

> 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?

exactly.

January 25, 2015
On Sun, 25 Jan 2015 05:34:15 +0000, Suliman wrote:

> 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?

just a minor detail: "UFCS style", as "S" un "UFCS" means "syntax". ;-)

January 25, 2015
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.
January 25, 2015
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.
« First   ‹ Prev
1 2