Skip to content
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

strict() and default() cause problem in command builder #760

Closed
GongT opened this issue Jan 19, 2017 · 2 comments
Closed

strict() and default() cause problem in command builder #760

GongT opened this issue Jan 19, 2017 · 2 comments

Comments

@GongT
Copy link

GongT commented Jan 19, 2017

main.js:

const ArgumentDefine = yargs
		.strict()
		// .command(require('./commands/init'))
		.commandDir('commands')
		.demandCommand(1);
console.log(ArgumentDefine.argv);

commands/init.js:

exports.command = 'init [target]';
exports.desc = 'Create the config files. (or update if exists)';
exports.builder = function builder(yargs) {
	return yargs
		.default('target', './')
		.demandCommand(0, 1, 'no way to show this', 'Too many arguments.')
		.strict();
};

node ./main.js init /tmp/xxx:

main.js init [target]

Options:
  --target                                                       [default: "./"]

Unknown argument: target
@GongT
Copy link
Author

GongT commented Jan 19, 2017

without strict(), I can run node main.js init /tmp/xxx -x -y -z without error.

without default(), I can't run node main.js init -x -y -z, but can't set a default value.

@bcoe
Copy link
Member

bcoe commented Jan 20, 2017

@GongT from the docs:

Any command-line argument given that is not demanded, or does not have a corresponding description, will be reported as an error.

so if you describe target or demand target.

      yargs
        .default('target', 'blarg.js')
        .demand('target')
        .strict()

Things should start working for you. I can see the argument for making demand also be applied to arguments that are simply defaulted; it might be worth joining the conversation here #742, where we're discussing API improvements for version 7.x of yargs.

@bcoe bcoe closed this as completed Jan 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants