@anycli/example-plugin-ts
Version:
example dxcli plugin in typescript
79 lines (78 loc) • 2.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("@anycli/command");
const cli_ux_1 = require("cli-ux");
class Hello extends command_1.Command {
constructor() {
super(...arguments);
// this makes the parser not fail when it receives invalid arguments
// set it to off if you need to accept variable arguments
// static strict = false
// runs the parser and stores the results in this.options
// you should run this even if you have no flags/args so it properly errors out
// (see strict above for variable argument commands)
//
// stores the parsed flags in options.flags[name]
//
// stores the parsed args in options.args[name] as an object
// but also in options.argv as an array
// you can get the raw args passed to the command with this.argv
// or from this.options.argv which will remove any args that were actually flags
this.options = command_1.parse(this.argv, Hello);
}
// entry point of command
async run() {
const name = this.options.flags.name || 'world';
cli_ux_1.default.log(`hello ${name} from hello!`);
// this.options.flags.force is a boolean
// this.options.args.file and this.options.argv[0] is a string or undefined
}
}
Hello.title = 'scaffolded command that says hello';
// hide the command from help
// can also set hidden on args and flags
// static hidden = true
// usage is set by default
// add your own by setting this variable
// can be a string or array
// static usage = 'title of command'
Hello.description = `
Add a longer description here
...
...
`;
Hello.examples = [
`$ example-plugin-ts hello
hello world from hello!
`,
`$ example-plugin-ts hello --name myname
hello myname from hello!
`,
'$ example-plugin-ts hello file outputs "hello world!" to file',
'$ example-plugin-ts hello --force',
'$ example-plugin-ts hello --help',
];
// allow running this command by running `$ example-plugin-ts foobar`
// static aliases = ['foobar']
Hello.flags = {
// flag with a value (-n, --name=VALUE)
name: command_1.flags.string({
char: 'n',
description: 'name to print',
hidden: false,
required: false,
multiple: false,
}),
// flag with no value (-f, --force)
force: command_1.flags.boolean({
char: 'f',
}),
};
Hello.args = [
{
name: 'file',
required: false,
description: 'file to output',
},
];
exports.default = Hello;