Skip to content

Interface: CommandEnvironment<G> ​

Command environment.

Signature ​

ts
export interface CommandEnvironment<G extends GunshiParamsConstraint = DefaultGunshiParams>

Type Parameters ​

NameDescription
G extends GunshiParamsConstraint = DefaultGunshiParamsA type extending GunshiParams to specify the shape of command environments.

Properties ​

NameTypeDescription
cwdstring | undefinedCurrent working directory.
descriptionstring | undefinedCommand description.
entryCommand (optional)Command<any> | LazyCommand<any>The entry command of the CLI, marked with entry: true. It is available whether or not the CLI has sub-commands, while CommandEnvironment.subCommands includes the entry command only when the CLI has sub-commands, given with CliOptions.subCommands or with Command.subCommands of the entry command. It is not frozen, like the commands in CommandEnvironment.subCommands.
globalOptions (optional)ReadonlyMap<string, ArgSchema>The global options that are in effect for the command being executed. A plugin registers a global option with addGlobalOption, and gunshi merges it into the arguments of the command that runs. An argument that the command declares under the same name replaces it, and the option is then not in effect: the value under that name is the command's, and the plugin that registered it must not read it as its own. The schema is the one in effect, which may have given up its short name to an argument of the command that claims the same letter.
leftMarginnumberLeft margin of the command output. Default: 2
middleMarginnumberMiddle margin of the command output. Default: 10
namestring | undefinedCommand name.
onAfterCommand((ctx: Readonly<CommandContext<G>>, result: string | undefined) => Awaitable<void>) | undefinedHook that runs after successful command execution. It receives the result of the command only when it is a string. Since v0.27.0
onBeforeCommand((ctx: Readonly<CommandContext<G>>) => Awaitable<void>) | undefinedHook that runs before any command execution Since v0.27.0
onErrorCommand((ctx: Readonly<CommandContext<G>>, error: Error) => Awaitable<void>) | undefinedHook that runs when a command throws an error Since v0.27.0
renderHeader((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null | undefinedRender function the header section in the command usage.
renderUsage((ctx: Readonly<CommandContext<G>>) => Promise<string>) | null | undefinedRender function the command usage.
renderValidationErrors((ctx: Readonly<CommandContext<G>>, error: AggregateError) => Promise<string>) | null | undefinedRender function the validation errors.
strictbooleanWhether to treat undefined options as argument validation errors. Default: false
subCommandsMap<string, Command<any> | LazyCommand<any>> | undefinedSub commands.
usageOptionTypebooleanWhether to display the usage option type. Default: false
usageOptionValuebooleanWhether to display the option value. Default: true
usageSilentbooleanWhether to keep usage, header, version, and validation-error text off the terminal (they are still returned as strings) and to silence CommandContext.log. Output that must still appear — machine-readable results for an agent, warnings for the CLI author — should use console.log / console.warn, not ctx.log. A command that spreads jsonArgs and is given --json prints its result, and its errors, as JSON even with it. The validation errors, and a CommandError that the command throws, are not printed either, so cli() rejects with them, for the caller to report them. Default: false
versionstring | undefinedCommand version.

Released under the MIT License.