Skip to content

Instantly share code, notes, and snippets.

@yuliji
Created September 29, 2020 01:39
Show Gist options
  • Save yuliji/b028abd87aee8254d4ecee07b60b3ebe to your computer and use it in GitHub Desktop.
Save yuliji/b028abd87aee8254d4ecee07b60b3ebe to your computer and use it in GitHub Desktop.
[yargs multi subcommand]
//https://github.com/yargs/yargs/issues/225
var yargs = require('yargs')
var argv = yargs
.usage('usage: $0 <command>')
.command('create', 'create a new [project|module]', function (yargs) {
argv = yargs
.usage('usage: $0 create <item> [options]')
.command('project', 'create a new project', function (yargs) {
console.log('creating project :)')
})
.command('module', 'create a new module', function (yargs) {
console.log('creating module :)')
})
.help('help')
.updateStrings({
'Commands:': 'item:'
})
.wrap(null)
.argv
checkCommands(yargs, argv, 2)
})
.command('list', 'list items in project', function (yargs) {
console.log('listing items in project :)')
})
.help('help')
.wrap(null)
.argv
checkCommands(yargs, argv, 1)
function checkCommands (yargs, argv, numRequired) {
if (argv._.length < numRequired) {
yargs.showHelp()
} else {
// check for unknown command
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment