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

requires_arg example don't work #1974

Closed
salsan opened this issue Jun 23, 2021 · 1 comment · Fixed by #2019
Closed

requires_arg example don't work #1974

salsan opened this issue Jun 23, 2021 · 1 comment · Fixed by #2019

Comments

@salsan
Copy link

salsan commented Jun 23, 2021

I try learn how to work this tool and requiresArg , and I've tried this example requires_arg.js but this is the result

$ node requires_arg.js

YError: Invalid second argument. Expected string or boolean but received object.
    at c (/home/pi/Desktop/test/node_modules/yargs/build/index.cjs:1:2244)
    at /home/pi/Desktop/test/node_modules/yargs/build/index.cjs:1:2096
    at Array.forEach (<anonymous>)
    at h (/home/pi/Desktop/test/node_modules/yargs/build/index.cjs:1:1993)
    at Bt.usage (/home/pi/Desktop/test/node_modules/yargs/build/index.cjs:1:39503)
    at Object.<anonymous> (/home/pi/Desktop/test/requires_arg.js:3:18)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
.
.
.
e [YError]: .usage() description must start with $0 if being used as alias for .command()
    at Bt.usage (/home/pi/Desktop/test/node_modules/yargs/build/index.cjs:1:39703)
    at Object.<anonymous> (/home/pi/Desktop/test/requires_arg.js:3:18)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47


I wrote my ( based from this solution for other issue )version and run

var yargs = require('yargs/yargs')(process.argv.slice(2));

var argv = yargs.usage('This is my awesome program').options({
  'input': {
    type: "string",
    description: 'Input file name',
    requiresArg: true,
    alias: 'i',
  },
  'output': {
    type: "string",
    description: 'Output file name',
    requiresArg: true,
    alias: 'o'
  }
}).argv;

yargs.showHelp();

console.log('\n\nInspecting options');
console.dir(argv);

I replace short with alias, because I don't found short on the documentation

@kitesi
Copy link
Contributor

kitesi commented Jun 25, 2021

Seems like it's because usage() changed how it works, Now it asks for the actual usage command as the first parameter like so:

yargs.usage("$0 [port]"), next would be description, builder and handler.

Your example works, but it doesn't provide the actual command or the allowed options, only the description. So I think something like this would be better:

var yargs = require('yargs/yargs')(process.argv.slice(2));

var argv = yargs.usage('$0 [port]', "Load up a server").options({
	input: {
		description: 'Input file name',
		requiresArg: true,
		alias: 'i',
	},
	output: {
		description: 'Output file name',
		requiresArg: true,
		alias: 'o',
	},
}).argv;

yargs.showHelp();

console.log('\n\nInspecting options');
console.dir(argv);

And yea looks like yargs changed from short to alias.

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

Successfully merging a pull request may close this issue.

2 participants