www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.getopt

reply Trass3r <un known.com> writes:
Why doesn't std.getopt support standard unix style like 'make -j 4'?

"uint timeout;
getopt(args, "timeout|t", &timeout);
To set timeout to 5, use either of the following: --timeout=5, --timeout  
5, --t=5, --t 5, or -t5. Forms such as -t 5 and -timeout=5 will be not  
accepted."

Also it doesn't make any sense to me that --t=5 and --t 5 is allowed in  
this case.
I expected the short alias only to be used with a single dash.
Jul 15 2011
next sibling parent Trass3r <un known.com> writes:
The following just came to my mind:
What about having getopt also generate a "Usage" string which you can  
output if any cmdline argument parsing error occured?
Of course this would require adding a third field containing the  
description. (which would also be self-documenting)
Jul 16 2011
prev sibling next sibling parent reply Jens Mueller <jens.k.mueller gmx.de> writes:
Trass3r wrote:
 Why doesn't std.getopt support standard unix style like 'make -j 4'?
 
 "uint timeout;
 getopt(args, "timeout|t", &timeout);
 To set timeout to 5, use either of the following: --timeout=5,
 --timeout 5, --t=5, --t 5, or -t5. Forms such as -t 5 and -timeout=5
 will be not accepted."
 
 Also it doesn't make any sense to me that --t=5 and --t 5 is allowed
 in this case.
 I expected the short alias only to be used with a single dash.

I agree. Not supporting -t 5 is no good. std.getopt took its inspiration from Perl's Getopt::Long (http://perldoc.perl.org/Getopt/Long.html) which supports it as well. I'm unsure about --t=5 and --t 5. I don't like them but I don't have to use them. But without them the rules would be simpler but removing them may break code. Adding -t 5 is safe I think. And maybe we enhance the documentation a bit such that more common choices are given first and less known but still supported later. If Andrei agrees maybe you or I can create a pull request for -t 5 and enhancing the documentation regarding -- for short options. BTW -timeout=5 is accepted but it shouldn't. I just wrote unittests for all combinations. Jens
Jul 16 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 7/16/11 8:58 AM, Jens Mueller wrote:
 Trass3r wrote:
 Why doesn't std.getopt support standard unix style like 'make -j 4'?

 "uint timeout;
 getopt(args, "timeout|t",&timeout);
 To set timeout to 5, use either of the following: --timeout=5,
 --timeout 5, --t=5, --t 5, or -t5. Forms such as -t 5 and -timeout=5
 will be not accepted."

 Also it doesn't make any sense to me that --t=5 and --t 5 is allowed
 in this case.
 I expected the short alias only to be used with a single dash.

I agree. Not supporting -t 5 is no good. std.getopt took its inspiration from Perl's Getopt::Long (http://perldoc.perl.org/Getopt/Long.html) which supports it as well. I'm unsure about --t=5 and --t 5. I don't like them but I don't have to use them. But without them the rules would be simpler but removing them may break code. Adding -t 5 is safe I think. And maybe we enhance the documentation a bit such that more common choices are given first and less known but still supported later. If Andrei agrees maybe you or I can create a pull request for -t 5 and enhancing the documentation regarding -- for short options. BTW -timeout=5 is accepted but it shouldn't. I just wrote unittests for all combinations. Jens

Yes please. So, this stuff should work if we have t|timeout bound to an integral: -t5, -t 5, --timeout 5, --timeout=5 This stuff should not: -t=5, -timeout 5, -timeout=5 Right? Andrei
Jul 16 2011
parent reply Mike Wey <mike-wey example.com> writes:
On 07/17/2011 11:06 AM, Jens Mueller wrote:
 Andrei Alexandrescu wrote:
 On 7/16/11 8:58 AM, Jens Mueller wrote:
 Trass3r wrote:
 Why doesn't std.getopt support standard unix style like 'make -j 4'?

 "uint timeout;
 getopt(args, "timeout|t",&timeout);
 To set timeout to 5, use either of the following: --timeout=5,
 --timeout 5, --t=5, --t 5, or -t5. Forms such as -t 5 and -timeout=5
 will be not accepted."

 Also it doesn't make any sense to me that --t=5 and --t 5 is allowed
 in this case.
 I expected the short alias only to be used with a single dash.

I agree. Not supporting -t 5 is no good. std.getopt took its inspiration from Perl's Getopt::Long (http://perldoc.perl.org/Getopt/Long.html) which supports it as well. I'm unsure about --t=5 and --t 5. I don't like them but I don't have to use them. But without them the rules would be simpler but removing them may break code. Adding -t 5 is safe I think. And maybe we enhance the documentation a bit such that more common choices are given first and less known but still supported later. If Andrei agrees maybe you or I can create a pull request for -t 5 and enhancing the documentation regarding -- for short options. BTW -timeout=5 is accepted but it shouldn't. I just wrote unittests for all combinations. Jens

Yes please. So, this stuff should work if we have t|timeout bound to an integral: -t5, -t 5, --timeout 5, --timeout=5 This stuff should not: -t=5, -timeout 5, -timeout=5 Right?

--timeout 5 and --timeout=5 work as expected. --timeout5 fails as expected. -t5 works as expected. But -t 5 fails (I will try to fix). And you don't want -t=5, -timeout 5, and -timeout=5 to work? Currently they all pass as unexpected. Should I make them fail? -timeout5 fails as expected. What about --t 5 (works currently) --t=5 (works currently) --t5 (fails currently) These are all 12 combinations of "--" or "-" and "t" or "timeout" and "=5", " 5", or "5". Jens

I think you generally want long arguments like "timeout" to start with "--" and the one character ones like "t" to start with "-". Also with "--timeout5" the 5 might be seen as part of the flag and shouldn't be allowed. -- Mike Wey
Jul 17 2011
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2011-07-17 12:02, Mike Wey wrote:
 I think you generally want long arguments like "timeout" to start with
 "--" and the one character ones like "t" to start with "-".

 Also with "--timeout5" the 5 might be seen as part of the flag and
 shouldn't be allowed.

Don't know why anyone would want to be able to the option and then the argument without a space. -- /Jacob Carlborg
Jul 17 2011
prev sibling parent Mike Wey <mike-wey example.com> writes:
On 07/17/2011 03:49 PM, Jens Mueller wrote:
 That's true. It's only that changing it means breaking code. Though I
 don't believe that there are many scripts out there. But for example dmd
 uses long options without double dashes.
 I'm up to change this hoping it won't break too much code.

One problem with the long options with a single dash is that getopt supports bundling. So when enabled does "-timeout" equal "-t -i -m -e -o -u -t" or "--timeout" ? -- Mike Wey
Jul 17 2011
prev sibling next sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
Trass3r wrote:
 The following just came to my mind:
 What about having getopt also generate a "Usage" string which you
 can output if any cmdline argument parsing error occured?
 Of course this would require adding a third field containing the
 description. (which would also be self-documenting)

We have some support for this. Maybe you can comment on my pull request because I got stuck a little bit. https://github.com/D-Programming-Language/phobos/pull/106 Because the extension should not break building something like gflags on top of getopt. Jens
Jul 16 2011
prev sibling next sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
Andrei Alexandrescu wrote:
 On 7/16/11 8:58 AM, Jens Mueller wrote:
Trass3r wrote:
Why doesn't std.getopt support standard unix style like 'make -j 4'?

"uint timeout;
getopt(args, "timeout|t",&timeout);
To set timeout to 5, use either of the following: --timeout=5,
--timeout 5, --t=5, --t 5, or -t5. Forms such as -t 5 and -timeout=5
will be not accepted."

Also it doesn't make any sense to me that --t=5 and --t 5 is allowed
in this case.
I expected the short alias only to be used with a single dash.

I agree. Not supporting -t 5 is no good. std.getopt took its inspiration from Perl's Getopt::Long (http://perldoc.perl.org/Getopt/Long.html) which supports it as well. I'm unsure about --t=5 and --t 5. I don't like them but I don't have to use them. But without them the rules would be simpler but removing them may break code. Adding -t 5 is safe I think. And maybe we enhance the documentation a bit such that more common choices are given first and less known but still supported later. If Andrei agrees maybe you or I can create a pull request for -t 5 and enhancing the documentation regarding -- for short options. BTW -timeout=5 is accepted but it shouldn't. I just wrote unittests for all combinations. Jens

Yes please. So, this stuff should work if we have t|timeout bound to an integral: -t5, -t 5, --timeout 5, --timeout=5 This stuff should not: -t=5, -timeout 5, -timeout=5 Right?

--timeout 5 and --timeout=5 work as expected. --timeout5 fails as expected. -t5 works as expected. But -t 5 fails (I will try to fix). And you don't want -t=5, -timeout 5, and -timeout=5 to work? Currently they all pass as unexpected. Should I make them fail? -timeout5 fails as expected. What about --t 5 (works currently) --t=5 (works currently) --t5 (fails currently) These are all 12 combinations of "--" or "-" and "t" or "timeout" and "=5", " 5", or "5". Jens
Jul 17 2011
prev sibling next sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
Mike Wey wrote:
 On 07/17/2011 11:06 AM, Jens Mueller wrote:
Andrei Alexandrescu wrote:
On 7/16/11 8:58 AM, Jens Mueller wrote:
Trass3r wrote:
Why doesn't std.getopt support standard unix style like 'make -j 4'?

"uint timeout;
getopt(args, "timeout|t",&timeout);
To set timeout to 5, use either of the following: --timeout=5,
--timeout 5, --t=5, --t 5, or -t5. Forms such as -t 5 and -timeout=5
will be not accepted."

Also it doesn't make any sense to me that --t=5 and --t 5 is allowed
in this case.
I expected the short alias only to be used with a single dash.

I agree. Not supporting -t 5 is no good. std.getopt took its inspiration from Perl's Getopt::Long (http://perldoc.perl.org/Getopt/Long.html) which supports it as well. I'm unsure about --t=5 and --t 5. I don't like them but I don't have to use them. But without them the rules would be simpler but removing them may break code. Adding -t 5 is safe I think. And maybe we enhance the documentation a bit such that more common choices are given first and less known but still supported later. If Andrei agrees maybe you or I can create a pull request for -t 5 and enhancing the documentation regarding -- for short options. BTW -timeout=5 is accepted but it shouldn't. I just wrote unittests for all combinations. Jens

Yes please. So, this stuff should work if we have t|timeout bound to an integral: -t5, -t 5, --timeout 5, --timeout=5 This stuff should not: -t=5, -timeout 5, -timeout=5 Right?

--timeout 5 and --timeout=5 work as expected. --timeout5 fails as expected. -t5 works as expected. But -t 5 fails (I will try to fix). And you don't want -t=5, -timeout 5, and -timeout=5 to work? Currently they all pass as unexpected. Should I make them fail? -timeout5 fails as expected. What about --t 5 (works currently) --t=5 (works currently) --t5 (fails currently) These are all 12 combinations of "--" or "-" and "t" or "timeout" and "=5", " 5", or "5". Jens

I think you generally want long arguments like "timeout" to start with "--" and the one character ones like "t" to start with "-".

That's true. It's only that changing it means breaking code. Though I don't believe that there are many scripts out there. But for example dmd uses long options without double dashes. I'm up to change this hoping it won't break too much code.
 Also with "--timeout5" the 5 might be seen as part of the flag and
 shouldn't be allowed.

Very true. It's correct that --timeout5 fails. Jens
Jul 17 2011
prev sibling parent Jens Mueller <jens.k.mueller gmx.de> writes:
Jacob Carlborg wrote:
 On 2011-07-17 12:02, Mike Wey wrote:
I think you generally want long arguments like "timeout" to start with
"--" and the one character ones like "t" to start with "-".

Also with "--timeout5" the 5 might be seen as part of the flag and
shouldn't be allowed.

Don't know why anyone would want to be able to the option and then the argument without a space.

Yeah. It's only useful for short options. Jens
Jul 17 2011