How to use the command function from yargs
Find comprehensive JavaScript yargs.command code examples handpicked from public code repositorys.
yargs.command is a method in the yargs library that allows you to define a new command-line command with its own options and functionality.
54 55 56 57 58 59 60 61 62 63
rootCommands.push(command); } } } }); _.forEach(rootCommands, command => yargs.command(command.toCommand())); return yargs // eslint-disable-line .env('CF_ARG_') // this means that every process.env.CF_ARG_* will be passed to argv .option('cfconfig', {
GitHub: lando/cli
247 248 249 250 251 252 253 254 255 256
* @param {Object} task A Lando task object (@see add for definition) * @param {Object} [config={}] The landofile * @return {Object} A yargs command object * @example * // Add a task to the yargs CLI * yargs.command(lando.tasks.parseToYargs(task)); */ async parseToYargs(task) { const handler = async argv => { // if run is not a function then we need to get it
+ 11 other calls in file
How does yargs.command work?
In the yargs library, yargs.command is a method that allows you to define a new command-line command with its own options and functionality. This is useful for creating complex command-line interfaces with multiple commands, each of which may have its own arguments and behaviors. When you call yargs.command, you pass in an object that describes the command, including its name, aliases, description, options, and handler function. The options can include both positional arguments and flags with values, and can have various types, such as boolean, string, or number. The handler function is called when the command is executed, and receives the parsed command-line arguments as its first parameter. For example, here's how you might define a new command using yargs.command: javascript Copy code {{{{{{{ const yargs = require('yargs'); yargs.command({ command: 'greet ', aliases: ['hello', 'hi'], describe: 'Greet someone by name', builder: { title: { describe: 'Title to use when greeting', type: 'string', default: 'Mr.' } }, handler: (argv) => { console.log(`Hello, ${argv.title} ${argv.name}!`); } }); In this example, we're using yargs.command to define a new command called greet, which takes a required positional argument . We also define two aliases for the command, hello and hi, using the aliases option. We specify a description for the command using the describe option, and define an optional flag called title using the builder option. The title flag has a default value of 'Mr.' and is of type string. Finally, we define the handler function for the command using the handler option. The handler function is called when the greet command is executed, and receives an object with the parsed command-line arguments as its first parameter. In this case, we simply log a greeting to the console that includes the value of the title and name arguments. Overall, yargs.command is a powerful method in yargs that allows you to define complex command-line interfaces with multiple commands and options, making it easier to build robust command-line tools and utilities.
63 64 65 66 67 68 69 70 71 72
} }) yargs.command(require('./command-start')) yargs.command(require('./command-stop')) yargs.command(require('./command-env')) yargs.command('*', false, () => { yargs.showHelp(); console.error("Non-existing or no command specified.");
+ 7 other calls in file
GitHub: dockerizeme/dockerizeme
16 17 18 19 20 21 22 23 24
// Dockerize (async () => { try { yargs.command( '* [package]', 'Dockerize a package', (yargs) => {
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
const yargs = require("yargs"); yargs.command({ command: "greet ", aliases: ["hello", "hi"], describe: "Greet someone by name", builder: { title: { describe: "Title to use when greeting", type: "string", default: "Mr.", }, }, handler: (argv) => { console.log(`Hello, ${argv.title} ${argv.name}!`); }, }); // Parse the command-line arguments const argv = yargs.parse(); // Call the appropriate command based on the parsed arguments if (argv._[0] === "greet") { yargs.showHelpOnFail(false); yargs.exitProcess(false); yargs.parse(["greet", argv._[1]]); }
In this example, we're using yargs.command to define a new command called greet, which takes a required positional argument . We also define two aliases for the command, hello and hi, using the aliases option. We specify a description for the command using the describe option, and define an optional flag called title using the builder option. The title flag has a default value of 'Mr.' and is of type string. Finally, we define the handler function for the command using the handler option. The handler function is called when the greet command is executed, and receives an object with the parsed command-line arguments as its first parameter. In this case, we simply log a greeting to the console that includes the value of the title and name arguments. We then parse the command-line arguments using yargs.parse. If the first positional argument is greet, we call yargs.parse again with an array containing the greet command and the second positional argument. This ensures that the greet command is executed with the appropriate arguments. Overall, this example demonstrates how you can use yargs.command to define a new command with its own options and functionality, and then call that command based on the parsed command-line arguments.
GitHub: zandermax/sfdx
77 78 79 80 81 82 83 84 85 86
const sfdx = require('sfdx') const yargs = require('yargs') // Import all commands for (let command of Object.keys(sfdx)) { yargs.command(sfdx[command].yargs) } // Initialize yargs commands yargs.argv
17 18 19 20 21 22 23 24 25 26
builder(yargs: Yargs<any>) { cmd.builder && cmd.builder(yargs); if(!cmd.handler) yargs.demandCommand(); if(cmd.subCommands) { for(const subCommand of cmd.subCommands) { yargs.command(subCommand); } } return yargs; }
29 30 31 32 33 34 35 36 37 38
.errorsStyle('red') ; cli.usage(`Usage: $0 <command> [options]`); this.commands.forEach((command) => { cli.command(command); }); cli.demandCommand(1) .strict()
GitHub: suskind/jsfinder
22 23 24 25 26 27 28 29 30 31 32 33
(async () => { let aURLs = []; const argv = yargs.command('jsfinder', 'Parse JS files for paths and stuff. ex:\n$ jsfinder -u https//example.com -o /tmp/out --insource ', { }).option('url', { description: 'URL to be parsed. ex: -u https://example.com ', alias: 'u', type: 'string',
+ 3 other calls in file
13 14 15 16 17 18 19 20 21 22
yargs.parserConfiguration({ 'camel-case-expansion': false, // "dot-notation": false }) const { argv } = yargs.command('$0 [account] [environment]', 'Run verification').help() const { account, environment } = argv // const additionalParams = argv._ // const profile = process.env.AWS_PROFILE
24 25 26 27 28 29 30 31 32 33 34 35 36
const usageSTR = "\nUsage: todo <command> <options>"; use = yargs.usage(usageSTR); const commands = yargs.command("create", "Ajoute une todo", (y) => { y.option('t', { alias: "task", describe: "Tache à rappeler", type: "string", demandOption: true }); y.option('c', { alias: "complete", describe: "Si la tâche est terminer", type: "string", demandOption: false }); y.option('d', { alias: "due", describe: "Echeance de la tache", type: "string", demandOption: false }); return y;
+ 24 other calls in file
36 37 38 39 40 41 42 43 44
// (config) Load command with path to config JSON file // Replace './path/to/config.json' with your config JSON file var config = require('yargs-command-config')({file: './path/to/config.json'}); // (yargs) Add command to manage config file var argv = yargs.command(config).argv; ``` Display help options for `bin.js` using [node](https://nodejs.org/api/cli.html):
yargs.argv is the most popular function in yargs (1012 examples)