Command Options
/cliapp$ wooks cli --options|
Wooks CLI supports handling options in your CLI commands. Options are typically defined with a double hyphen (--) or a single hyphen (-) prefix and can have an associated value.
To define options in Wooks CLI, you can use the options property when registering your command. Here's an example:
import { useCliOption } from '@wooksjs/event-cli'
app.cli('my-command', {
options: [
// Define the "--project" option with a shortcut as "-p"
{ keys: ['project', 'p'] },
],
handler: () => {
const project = useCliOption('project');
return 'my command option project = ' + project;
},
});With the above command configuration, you can execute the command as follows:
node your-script.js my-command --project=testAlternatively, you can use the shortcut for project option:
node your-script.js my-command -p=testThis will trigger the CLI command with the project option set to "test" and log the result to the console.
Reading All Options
useCliOptions() returns the full parsed-flags object, including the positional arguments in _:
import { useCliOptions } from '@wooksjs/event-cli'
app.cli('my-command/:arg?', {
handler: () => {
const flags = useCliOptions()
// node your-script.js my-command extra --project=test -v
// flags = { _: ['my-command', 'extra'], project: 'test', v: true }
return JSON.stringify(flags)
},
});Parsing Behavior
Options are parsed with minimist. To customize parsing (string, boolean, alias, default, ...), pass minimist options as the second argument of run():
app.run(undefined, { boolean: ['verbose'], alias: { v: 'verbose' } })Gotchas
useCliOption('project')resolves synonyms (e.g.,-pfor--project) only when the option is declared in the command'soptionsmetadata (see Command Usage (Help) — Options). For undeclared options it looks up the raw flag name only.