google-code-export / v8cgi

Automatically exported from code.google.com/p/v8cgi
BSD 3-Clause "New" or "Revised" License
2 stars 1 forks source link

getOpt OPTIONAL_ARGUMENT fails when followed by other options #105

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Assuming the following code:

var GetOpt = require("getopt").GetOpt;

var o = new GetOpt();
o.add('name', 'Process name', '', 'n', 'name', GetOpt.REQUIRED_ARGUMENT);
o.add('trace', 'Trace client url', null, 't', 'trace', 
GetOpt.REQUIRED_ARGUMENT);
o.add('enabletrace', 'Enable tracing', false, 'e', 'enabletrace', 
GetOpt.OPTIONAL_ARGUMENT);
o.add('api', 'API server url', null, 'a', 'api', GetOpt.REQUIRED_ARGUMENT);
o.add('jstcache', 'Jst cache enable', false, 'j', 'jstcache', 
GetOpt.OPTIONAL_ARGUMENT);
o.parse(system.args);

with the following command line arguments:
--trace http://localhost:8080/trace --enabletrace

o.get('enabletrace') works as expected and returns a boolean

with these command line arguments:
--trace http://localhost:8080/trace --enabletrace fred --api bill
o.get('enabletrace') returns "fred" and behaves as though configured as 
GetOpt.REQUIRED_ARGUMENT

the presence of the following --api option is what seems to break it

Original issue reported on code.google.com by andy.bis...@gmail.com on 25 Nov 2011 at 10:36

GoogleCodeExporter commented 9 years ago
I believe this is a correct behavior.

OPTIONAL_ARGUMENT means that an option does not need a value, but will accept 
it when specified.

This means:

--enabletrace => default value (false); no option specified; correct.

--enabletrace xyz => default value (false); the "xyz" string is considered to 
be program's main argument and not an option.

--enabletrace xyz --api ABC => "xyz" is the (optional) value.

--enabletrace --api ABC => enabletrace=true; the (optional) value was not 
specified but the option was present.

Original comment by ondrej.zara on 25 Nov 2011 at 8:32

GoogleCodeExporter commented 9 years ago

Original comment by ondrej.zara on 12 Dec 2011 at 8:24