www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - getopt & single-letter options with parameters

reply Adrian Matoga <epi atari8.info> writes:
Hi,

Is it by design that single-letter option needs to be glued to its 
argument, like "-ofilename", or is it a bug in implementation?

Source:

import std.stdio;
import std.getopt;

void main(string[] args)
{
	string outputFile;
	getopt(args,
		config.passThrough,
		"o|output-filename", &outputFile);
	writeln(args);
	writeln("'" ~ outputFile ~ "'");
}



Results:
test.exe -o somename

''
test.exe -osomename

'somename' Regards, Adrian Matoga
Aug 07 2010
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 08/07/2010 05:55 PM, Adrian Matoga wrote:
 Hi,

 Is it by design that single-letter option needs to be glued to its
 argument, like "-ofilename", or is it a bug in implementation?

 Source:

 import std.stdio;
 import std.getopt;

 void main(string[] args)
 {
 string outputFile;
 getopt(args,
 config.passThrough,
 "o|output-filename", &outputFile);
 writeln(args);
 writeln("'" ~ outputFile ~ "'");
 }



 Results:
  >test.exe -o somename
 test.exe somename
 ''

  >test.exe -osomename
 test.exe
 'somename'

 Regards,
 Adrian Matoga

It's by design in order to avoid confusion with parameterless options. Your example works with either of these invocations: ./prog -ofilename ./prog -o=filename ./prog --o=filename but not others. Andrei
Aug 07 2010
next sibling parent reply Adrian Matoga <epi atari8.info> writes:
On 2010-08-08 01:22, Andrei Alexandrescu wrote:
 On 08/07/2010 05:55 PM, Adrian Matoga wrote:
 Hi,

 Is it by design that single-letter option needs to be glued to its
 argument, like "-ofilename", or is it a bug in implementation?

 Source:

 import std.stdio;
 import std.getopt;

 void main(string[] args)
 {
 string outputFile;
 getopt(args,
 config.passThrough,
 "o|output-filename", &outputFile);
 writeln(args);
 writeln("'" ~ outputFile ~ "'");
 }



 Results:
  >test.exe -o somename
 test.exe somename
 ''

  >test.exe -osomename
 test.exe
 'somename'

 Regards,
 Adrian Matoga

It's by design in order to avoid confusion with parameterless options. Your example works with either of these invocations: ./prog -ofilename ./prog -o=filename ./prog --o=filename but not others. Andrei

Thanks very much. I suggest adding this info to official docs (it's not obvious, and a bit confusing, since long options work with whitespace(s)). Adrian
Aug 07 2010
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 08/07/2010 06:57 PM, Adrian Matoga wrote:
 On 2010-08-08 01:22, Andrei Alexandrescu wrote:
 On 08/07/2010 05:55 PM, Adrian Matoga wrote:
 Hi,

 Is it by design that single-letter option needs to be glued to its
 argument, like "-ofilename", or is it a bug in implementation?

 Source:

 import std.stdio;
 import std.getopt;

 void main(string[] args)
 {
 string outputFile;
 getopt(args,
 config.passThrough,
 "o|output-filename", &outputFile);
 writeln(args);
 writeln("'" ~ outputFile ~ "'");
 }



 Results:
test.exe -o somename

''
test.exe -osomename

'somename' Regards, Adrian Matoga

It's by design in order to avoid confusion with parameterless options. Your example works with either of these invocations: ./prog -ofilename ./prog -o=filename ./prog --o=filename but not others. Andrei

Thanks very much. I suggest adding this info to official docs (it's not obvious, and a bit confusing, since long options work with whitespace(s)). Adrian

http://www.dsource.org/projects/phobos/changeset/1822 Andrei
Aug 08 2010
parent Adrian Matoga <epi atari8.info> writes:
 http://www.dsource.org/projects/phobos/changeset/1822
 
 Andrei

Thank you, now the description is clear. By the way, shouldn't "--FOo" and "--bAr" in the following fragment be rejected because of "caseSensitive" set just before them?
 By default options are case-insensitive. You can change that behavior
 by passing $(D getopt) the $(D caseSensitive) directive like this:

 ---------
 bool foo, bar;
 getopt(args,
     std.getopt.config.caseSensitive,
     "foo", &foo,
     "bar", &bar);
 ---------

 In the example above, "--foo", "--bar", "--FOo", "--bAr" etc. are 

Adrian
Aug 08 2010
prev sibling next sibling parent SK <sk metrokings.com> writes:
On Aug 7, 2010, at 4:22 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org 
 wrote:


It's by design in order to avoid confusion with parameterless options. Your example works with either of these invocations: ./prog -ofilename ./prog -o=filename ./prog --o=filename but not others. Andrei

Aug 07 2010
prev sibling next sibling parent reply SK <sk metrokings.com> writes:
--Apple-Mail-3-134512537
Content-Type: text/plain;
	charset=us-ascii;
	format=flowed;
	delsp=yes
Content-Transfer-Encoding: 7bit



On Aug 7, 2010, at 4:22 PM, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org 
 wrote:

 On 08/07/2010 05:55 PM, Adrian Matoga wrote:
 Hi,

 Is it by design that single-letter option needs to be glued to its
 argument, like "-ofilename", or is it a bug in implementation?

 Source:

 import std.stdio;
 import std.getopt;

 void main(string[] args)
 {
 string outputFile;
 getopt(args,
 config.passThrough,
 "o|output-filename", &outputFile);
 writeln(args);
 writeln("'" ~ outputFile ~ "'");
 }



 Results:
test.exe -o somename

''
test.exe -osomename

'somename' Regards, Adrian Matoga

It's by design in order to avoid confusion with parameterless options. Your example works with either of these invocations: ./prog -ofilename ./prog -o=filename ./prog --o=filename but not others. Andrei

The prevailing convention is to allow whitespace in this case. Would you reconsider? Sorry for the accidental null post earlier. -steve --Apple-Mail-3-134512537 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable <html><body bgcolor=3D"#FFFFFF"><div><br><br>On Aug 7, 2010, at 4:22 PM, = Andrei Alexandrescu &lt;<a = href=3D"mailto:SeeWebsiteForEmail erdani.org">SeeWebsiteForEmail erdani.or= g</a>&gt; wrote:<br><br></div><div></div><blockquote = type=3D"cite"><div><span>On 08/07/2010 05:55 PM, Adrian Matoga = wrote:</span><br><blockquote = type=3D"cite"><span>Hi,</span><br></blockquote><blockquote = type=3D"cite"><span></span><br></blockquote><blockquote = type=3D"cite"><span>Is it by design that single-letter option needs to = be glued to its</span><br></blockquote><blockquote = type=3D"cite"><span>argument, like "-ofilename", or is it a bug in = implementation?</span><br></blockquote><blockquote = type=3D"cite"><span></span><br></blockquote><blockquote = type=3D"cite"><span>Source:</span><br></blockquote><blockquote = type=3D"cite"><span></span><br></blockquote><blockquote = type=3D"cite"><span>import std.stdio;</span><br></blockquote><blockquote = type=3D"cite"><span>import = std.getopt;</span><br></blockquote><blockquote = type=3D"cite"><span></span><br></blockquote><blockquote = type=3D"cite"><span>void main(string[] = args)</span><br></blockquote><blockquote = type=3D"cite"><span>{</span><br></blockquote><blockquote = type=3D"cite"><span>string = outputFile;</span><br></blockquote><blockquote = type=3D"cite"><span>getopt(args,</span><br></blockquote><blockquote = type=3D"cite"><span>config.passThrough,</span><br></blockquote><blockquote= type=3D"cite"><span>"o|output-filename", = &amp;outputFile);</span><br></blockquote><blockquote = type=3D"cite"><span>writeln(args);</span><br></blockquote><blockquote = type=3D"cite"><span>writeln("'" ~ outputFile ~ = "'");</span><br></blockquote><blockquote = type=3D"cite"><span>}</span><br></blockquote><blockquote = type=3D"cite"><span></span><br></blockquote><blockquote = type=3D"cite"><span></span><br></blockquote><blockquote = type=3D"cite"><span></span><br></blockquote><blockquote = type=3D"cite"><span>Results:</span><br></blockquote><blockquote = type=3D"cite"><span> &gt;test.exe -o = somename</span><br></blockquote><blockquote type=3D"cite"><span>test.exe = somename</span><br></blockquote><blockquote = type=3D"cite"><span>''</span><br></blockquote><blockquote = type=3D"cite"><span></span><br></blockquote><blockquote = type=3D"cite"><span> &gt;test.exe = -osomename</span><br></blockquote><blockquote = type=3D"cite"><span>test.exe</span><br></blockquote><blockquote = type=3D"cite"><span>'somename'</span><br></blockquote><blockquote = type=3D"cite"><span></span><br></blockquote><blockquote = type=3D"cite"><span>Regards,</span><br></blockquote><blockquote = type=3D"cite"><span>Adrian = Matoga</span><br></blockquote><span></span><br><span>It's by design in = order to avoid confusion with parameterless options. Your example works = with either of these = invocations:</span><br><span></span><br><span>./prog = -ofilename</span><br><span>./prog -o=3Dfilename</span><br><span>./prog = --o=3Dfilename</span><br><span></span><br><span>but not = others.</span><br><span></span><br><span></span><br><span>Andrei</span><br=
<span></span><br></div></blockquote><br><span class=3D"Apple-style-span" =

0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, = 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, = 0.230469); "><span>The prevailing convention is = to&nbsp;</span><span>allow whitespace in this case. &nbsp;Would you = reconsider?</span><br><span></span><br><span>Sorry for the accidental = null post = earlier.</span><br><span></span><br><span>-steve</span></span></body></htm= l>= --Apple-Mail-3-134512537--
Aug 07 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 08/07/2010 10:08 PM, SK wrote:
 It's by design in order to avoid confusion with parameterless options.
 Your example works with either of these invocations:

 ./prog -ofilename
 ./prog -o=filename
 ./prog --o=filename

 but not others.


 Andrei

The prevailing convention is to allow whitespace in this case. Would you reconsider?

Sure. A patch would actually help a lot. Andrei
Aug 07 2010
prev sibling next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Saturday 07 August 2010 20:08:26 SK wrote:
 Sorry for the accidental null post earlier.

Well, just so long as no one dereferenced it... ;) - Jonathan M Davis
Aug 07 2010
prev sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message 
news:i3kpsi$rod$1 digitalmars.com...
 ./prog -ofilename

That being allowed at all kind of bugs me. I realize this probably isn't a particularly common problem in practice, but suppose you have: -o<filename> -of And the user uses "-o" with a file named "f": theapp -of // Do what now? Or, -o<filename> -of<filename> theapp -ofabc // "-o fabc" or "-of abc"? The whole possibility kinda makes me nervous.
Aug 08 2010
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 08/08/2010 12:17 PM, Nick Sabalausky wrote:
 "Andrei Alexandrescu"<SeeWebsiteForEmail erdani.org>  wrote in message
 news:i3kpsi$rod$1 digitalmars.com...
 ./prog -ofilename

That being allowed at all kind of bugs me. I realize this probably isn't a particularly common problem in practice, but suppose you have: -o<filename> -of And the user uses "-o" with a file named "f": theapp -of // Do what now? Or, -o<filename> -of<filename> theapp -ofabc // "-o fabc" or "-of abc"? The whole possibility kinda makes me nervous.

Short options with parameters don't accept bundling, so the problem never shows itself. Andrei
Aug 08 2010
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 08/08/2010 02:37 PM, Andrei Alexandrescu wrote:
 On 08/08/2010 12:17 PM, Nick Sabalausky wrote:
 "Andrei Alexandrescu"<SeeWebsiteForEmail erdani.org> wrote in message
 news:i3kpsi$rod$1 digitalmars.com...
 ./prog -ofilename

That being allowed at all kind of bugs me. I realize this probably isn't a particularly common problem in practice, but suppose you have: -o<filename> -of And the user uses "-o" with a file named "f": theapp -of // Do what now? Or, -o<filename> -of<filename> theapp -ofabc // "-o fabc" or "-of abc"? The whole possibility kinda makes me nervous.

Short options with parameters don't accept bundling, so the problem never shows itself. Andrei

Actually I stand corrected. The tar program does use bundled parameters... Andrei
Aug 08 2010
prev sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message 
news:i3n12a$2vfq$1 digitalmars.com...
 On 08/08/2010 12:17 PM, Nick Sabalausky wrote:
 "Andrei Alexandrescu"<SeeWebsiteForEmail erdani.org>  wrote in message
 news:i3kpsi$rod$1 digitalmars.com...
 ./prog -ofilename

That being allowed at all kind of bugs me. I realize this probably isn't a particularly common problem in practice, but suppose you have: -o<filename> -of And the user uses "-o" with a file named "f": theapp -of // Do what now? Or, -o<filename> -of<filename> theapp -ofabc // "-o fabc" or "-of abc"? The whole possibility kinda makes me nervous.

Short options with parameters don't accept bundling, so the problem never shows itself.

This then: --xy<filename> --xyz<filename> theapp --xyzabc // "--xy zabc" or "-xyz abc"?
Aug 08 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 08/08/2010 04:02 PM, Nick Sabalausky wrote:
 "Andrei Alexandrescu"<SeeWebsiteForEmail erdani.org>  wrote in message
 news:i3n12a$2vfq$1 digitalmars.com...
 On 08/08/2010 12:17 PM, Nick Sabalausky wrote:
 "Andrei Alexandrescu"<SeeWebsiteForEmail erdani.org>   wrote in message
 news:i3kpsi$rod$1 digitalmars.com...
 ./prog -ofilename

That being allowed at all kind of bugs me. I realize this probably isn't a particularly common problem in practice, but suppose you have: -o<filename> -of And the user uses "-o" with a file named "f": theapp -of // Do what now? Or, -o<filename> -of<filename> theapp -ofabc // "-o fabc" or "-of abc"? The whole possibility kinda makes me nervous.

Short options with parameters don't accept bundling, so the problem never shows itself.

This then: --xy<filename> --xyz<filename> theapp --xyzabc // "--xy zabc" or "-xyz abc"?

The long form with parameters accepts "--name=value" and "--name value" but not "--namevalue". Andrei
Aug 08 2010