import { Command } from "https://deno.land/x/wmill@v1.364.1/deps.ts";
Chainable command factory class.
import { Command } from "./mod.ts";
export const cli = new Command()
.name("todo")
.description("Example command description")
.globalOption("--verbose", "Enable verbose output.")
.globalEnv("VERBOSE=<value>", "Enable verbose output.")
.command("add <todo>", "Add todo.")
.action(({ verbose }, todo: string) => {
if (verbose) {
console.log("Add todo '%s'.", todo);
}
})
.command("delete <id>", "Delete todo.")
.action(({ verbose }, id: string) => {
if (verbose) {
console.log("Delete todo with id '%s'.", id);
}
});
if (import.meta.main) {
await cli.parse();
}
Type Parameters
Properties
Methods
Execute command.
Get error handler callback function.
Get help handler method.
Get help handler method.
Register default options like --version
and --help
.
Parse command-line arguments.
Read and validate environment variables.
Parse raw command line arguments.
Parse argument type.
Check whether the command should exit after printing help or version.
Check whether the command should throw errors or exit.
Set command callback method.
Add new command alias.
Don't throw an error if the command was called without arguments.
Set command arguments.
Syntax: <requiredArg:string> [optionalArg: number] [...restArgs:string]
Add new sub-command.
Add new sub-command.
Add new sub-command.
Register global complete handler.
Register complete handler.
Set default command. The default command is executed when the program was called without any argument and if no action handler is registered.
Set the long command description.
Register a global environment variable.
Register an environment variable.
Set custom error handler.
Register command example.
Get command name aliases.
Get arguments definition. E.g: input-file:string output-file:string
Get argument by name.
Get arguments.
Get base command by name or alias.
Get base commands.
Get base completion by name.
Get base completions.
Get base environment variable by name.
Get base environment variables.
Get base option by name.
Get base options.
Get base type by name.
Get base types.
Get command by name or alias.
Get commands.
Get completion by name.
Get completions.
Get command description.
Get environment variable by name.
Get environment variables.
Get example with given name.
Get all examples.
Get global command by name or alias.
Get global commands.
Get global completions by name.
Get global completions.
Get global environment variable by name.
Get global environment variables.
Get global option from parent commands by name.
Get global options.
Get parent command from global executed command. Be sure, to call this method only inside an action handler. Unless this or any child command was executed, this method returns always undefined.
Get global type by name.
Get global types.
Get generated help.
Get all arguments defined after the double dash.
Returns command name, version and meta data.
Get main command.
Returns an object of metadata.
Get metadata value by name.
Get command name.
Get option by name.
Get options.
Get parent command.
Get full command path.
Get original command-line arguments.
Get short command description. This is the first line of the description.
Get type by name.
Get types.
Get auto generated command usage.
Get command version.
Make command globally available.
Set command callback method.
Register global complete handler.
Register a global environment variable.
Register a global option.
Enable grouping of options and set the name of the group.
All option which are added after calling the .group()
method will be
grouped in the help output. If the .group()
method can be use multiple
times to create more groups.
Check if command has arguments.
Checks whether a child command exists by given name or alias.
Checks whether the command has sub-commands or not.
Checks whether the command has an environment variable with given name or not.
Checks whether the command has environment variables or not.
Checks whether the command has an example with given name or not.
Checks whether the command has examples or not.
Checks whether the command has an option with given name or not.
Checks whether the command has options or not.
Set command help.
Disable help option.
Set global help option.
Set help option.
Set help option.
Add meta data. Will be displayed in the auto generated help and in the output of the long version.
Set command name. Used in auto generated help and shell completions
Same as .throwErrors()
but also prevents calling Deno.exit
after
printing help or version with the --help and --version option.
Disable inheriting global commands, options and environment variables from parent commands.
Add a global option.
Register an option.
Parse command line arguments and execute matched command.
Remove sub-command by name or alias.
Remove option by name.
Reset internal command reference to main command.
Set internal command pointer to child command with given name.
Output generated help without exiting.
Outputs command name, version and meta data.
Output generated help without exiting.
Enable stop early. If enabled, all arguments starting from the first non option argument will be passed as arguments with type string to the command action handler.
For example:
command --debug-level warning server --port 80
Will result in:
- options: { debugLevel: 'warning' }
- args: ['server', '--port', '80']
Handle error. If throwErrors
is enabled the error will be thrown,
otherwise a formatted error message will be printed and Deno.exit(1)
will be called. This will also trigger registered error handlers.
Throw validation errors instead of calling Deno.exit()
to handle
validation errors manually.
A validation error is thrown when the command is wrongly used by the user. For example: If the user passes some invalid options or arguments to the command.
This has no effect for parent commands. Only for the command on which this method was called and all child commands.
Example:
import { Command, ValidationError } from "./mod.ts";
const cmd = new Command();
// ...
try {
cmd.parse();
} catch(error) {
if (error instanceof ValidationError) {
cmd.showHelp();
Deno.exit(1);
}
throw error;
}
Register custom type.
Set the command usage. Defaults to arguments.
Disable parsing arguments. If enabled the raw arguments will be passed to the action handler. This has no effect for parent or child commands. Only for the command on which this method was called.
Set command version.
Disable version option.
Set global version option.
Set version option.
Set version option.