-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
argument 'once' (and probably others) cause naming collision #404
Comments
There's a handful of these since when you create and parse options, they are placed directly on the A while ago I submitted #262 that added the As far as the docs, I'm sure if someone submits an accurate PR I'm sure it would be considered for a caveats section or something similar. |
So I hacked together a script that will list every possible collision (I think). Note that these are possible collisions. For example, Got the function getAllPropertyNames( obj ) {
var props = [];
do {
props= props.concat(Object.getOwnPropertyNames( obj ));
} while ( obj = Object.getPrototypeOf( obj ) );
return props;
}
function unique(val, index, array) {
return array.indexOf(val) === index;
}
var program = require('commander');
program.parse(['node','test']);
getAllPropertyNames(program.Command).concat(getAllPropertyNames(program)).sort().filter(unique); And right now, you'll come out with a list like this: [ 'Command',
'Option',
'__defineGetter__',
'__defineSetter__',
'__lookupGetter__',
'__lookupSetter__',
'__proto__',
'_allowUnknownOption',
'_args',
'_events',
'_execs',
'_maxListeners',
'_name',
'action',
'addImplicitHelpCommand',
'addListener',
'alias',
'allowUnknownOption',
'apply',
'args',
'arguments',
'bind',
'call',
'caller',
'command',
'commandHelp',
'commands',
'constructor',
'description',
'domain',
'emit',
'executeSubCommand',
'hasOwnProperty',
'help',
'helpInformation',
'isPrototypeOf',
'largestOptionLength',
'length',
'listeners',
'missingArgument',
'name',
'normalize',
'on',
'once',
'option',
'optionFor',
'optionHelp',
'optionMissingArgument',
'options',
'opts',
'outputHelp',
'parse',
'parseArgs',
'parseExpectedArgs',
'parseOptions',
'propertyIsEnumerable',
'prototype',
'rawArgs',
'removeAllListeners',
'removeListener',
'setMaxListeners',
'toLocaleString',
'toString',
'unknownOption',
'usage',
'valueOf',
'variadicArgNotLast',
'version' ] Perhaps a @zhiyelee could weigh in on how we might want to document this. |
@tonylukasavage thank you! So long a list, I really think a v3.0 version is needed to fix this problem. |
Thank you very much for the prompt and detailed response, it's really great to see active projects like this. |
I also ran into this problem. @tonylukasavage's fix is very helpful (thank you!) But +1 to @hotaru355's design change proposal. |
+1 for a breaking change in v3. The list of conflicts is just ridiculously long. IMHO it's always a mistake to throw together API/methods and business data in one namespace, and even if you do that, you should not allow user data with arbitrary names on your API objects at any rate. |
While I was reading the examples in the doc, the first question jumped into my head was this collision problem, considering the parsed results are all added to the commander object itself. so 👍 to the breaking change in the new version. |
My pull request #515 seems to fix this behavior as the user defined options are added to Commander._data instead Unfortunately this will make it a major release as reverse compatibility would be tricky |
Same issue in #584 Do we need an |
I got bitten too! :) #648 I created a workaround module for this problem. Tomorrow will be published. |
Published module which solve |
…der.js The --domain command line option was colliding with a property on the commander object. See: tj/commander.js#404 This fixes the app crashing when --domain is not the last option passed on the command line.
Hey, in my use case, while using mocha to test the cli app, mocha options are being given precedence over the arguments supplied by the command line leading to name collisions and lost data. program
.option('-d, --data <path>', 'Data')
.parse(args) where args is either Is there a way to give precedence to the actual options in case both the actual ones and the ones supplied through mocha are present. Thanks. |
I have opened a Pull Request which allows storing option values separately rather than as command properties (access using See #1102 |
Just want to give some feedback here to help others avoid a little pitfall: you cannot use
--once
as an argument, probably because the name collides with some internals of commander. Interestingly, theonce
argument will revert the expected behavior:myprog --once
will setcommander.once=undefined
, so a falsy value, while runningmyprog
will set it to a function as expected. Maybe you guys can fix the documentation to mention these reserved arguments, so others can save their time. Thanks!The text was updated successfully, but these errors were encountered: