-
Notifications
You must be signed in to change notification settings - Fork 0
CLI Configuration
To map the CLI arguments with the commands, parameters and options, and to print cli help, easycli generates an internal configuration from the cli class and its methods. This configuration can be customized by passing a configuration object to the constructor or by using easycli decorators on class, methods and method parameters.
const { Program } = require("@auttam/easycli");
class HelloWorld extends Program {
main(message, $options) {
let greetMessage = message || "Hello World";
// check if underline option is set
if ($options.underline) {
greetMessage = `\u001b[4m${greetMessage}\u001b[0m`;
}
console.log(greetMessage);
}
}
// Passing Configuration Object
Program.run(
new HelloWorld({
options: [
{
name: "underline",
aliases: ["u", "U"],
},
],
})
);Note:
Almost any configuration can be changed except a few like methodName of the auto-generated commands and propName of the auto-generated parameters.
{
/** all fields are optional */
/** name of the cli tool e.g. Http Server */
"name": "string",
/** name to match "bin" key of package.json */
"binaryName": "string",
/** help text for the command line tool */
"help": "string",
/** version number to match package.json */
"version": "string",
/** array of program parameters, see param config */
"params": [],
/** array of program options, see option config */
"options": [],
/** array of program commands */
"commands": []
}{
/** name of the command */
"name": "string"
/** name of the class method mapped with the command */
"method": "string"
/** help text for the command */
"help": "string",
/** array of command parameters, see param config */
"params": [],
/** array of command options, see option config */
"options": [],
}{
/** name of the parameter */
/** required field */
"name": "string",
/** help text for the parameter */
"help": "string",
/** name of the property for code reference */
/** not: changing this field when parameter is already
auto-generated from method signature will throw a
configuration error */
"propName": "string",
/** specifies whether parameter accepts a single (default)
value or a list of values e.g. arg1 arg2 arg3 ...*/
"type": "single | list",
/** if set to true, make sure value is supplied for the
parameter */
"required": true|false,
/** string array specifies list of accepted values for the
parameter, throws error different value is supplied.
To prevent error default value must be provided to
the "value" field */
"acceptOnly": ["string"],
/** default value, must be one of accepted values */
"value": "string"
}Note:EasyCli uses camelCase property naming convention for param property names and hyphenated naming convention for param names. This means if a configuration has test-param name and the property name is not specified, the property name for accessing this parameter from code will be testParam ($params.testParam). Same way, if program method as parameter testParam2, the generated name will test-param2.
{
/** name of the option */
/** required field */
"name": "string",
/** help text for the option */
"help": "string",
/** name of the property for code reference */
"propName": "string",
/** string array specifying other possible names for the
option, can contain single characters or words */
"aliases": [ "string" ]
/** string array specifies a list of accepted values for the
option, same as parameters */
"acceptOnly": ["string"],
/** default value, must be one of accepted values */
"value": "string"
}