I am implementing a console where different plugins can provide their own commands and command help. For this use case I need to have a new instance of yargs for each plugin. At the moment it seems there is only one global instance sharing all the options.
Is this possible?
Hey @DaelDe 馃憢 sorry for the slow reply.
You should be able to do this by using require('yargs/yargs'), rather than using the singleton.
const Yargs = require('yargs/yargs')
const argv = Yargs(process.argv.slice(2)).argv
Please let me know if this does the trick for you.
_Edit: and feel free to re-open if it doesn't._
Grrreat, thanks :)
Actually it does not work as expected. I am able to create many instances but in the help the name of the script is printed. I create one yargs instance for one sub-command. How can I tell xargs to use another script name?
At the moment I get the following from showHelp:
Main.js [command]
Commands:
Main.js channels Lists channels and statistics [aliases: ch]
...
But I want to set Main.js to something else. Just setting the first value in argv array does not work. How can I achieve that. I found nothing in the docs...
@DaelDe seems a bit hacky but this works:
function createYargs() {
const y = yargs(process.argv.slice(2));
y.$0 = 'npm';
return y;
}
createYargs()
.command('install <package>', 'install stuff')
.argv
The script name or node command is available at argv.$0 similarly to how $0 works in bash or perl.
https://github.com/yargs/yargs/blob/master/docs/api.md
@gusvargas grrreat, this was the last missing piece. Works like a charm now 馃憤
Most helpful comment
@DaelDe seems a bit hacky but this works: