Command
The Command is the core mechanism within the @hyperse/wizard framework that enables definition, parsing, and execution of CLI commands. This system provides type-safe command creation, argument parsing, and process execution while integrating with the broader event-driven architecture.
CommandBuilder
Name
Name | Type | Default |
---|---|---|
Name | string | typeof Root |
Options
Properties
Command Properties
Flags Configuration
Wizardβs flag parsing is powered by type-flagΒ and comes with many features:
- Array & Custom types
- Flag delimiters: βflag value, βflag=value, βflag:value, and βflag.value
- Combined aliases: -abcd 2 β -a -b -c -d 2
- End of flagsΒ : Pass in β to end flag parsing
- Unknown flags: Unexpected flags stored in unknownFlags
Read the type-flag docsΒ to learn more.
Flags can be specified in the flags
object-property, where the key is the flag name, and the value is a flag type function or an object that describes the flag.
The flag name is recommended to be in camelCase as it will be interpreted to parse kebab-case equivalents.
The flag type function can be any function that accepts a string and returns the parsed value. Default JavaScript constructors should cover most use-cases:
String
,
Number
,
Boolean
, etc.
The flag description object can be used to store additional information about the flag, such as alias
, default
, and description
. To accept multiple values for a flag, wrap the type function in an array.
All of the provided information will be used to generate better help documentation.
Example:
// @filename: @hyperse/wizard/dist/index.d.ts
import { } from '@hyperse/wizard';
const = <
'deploy',
{ : { : string; : string } }
>('deploy', {
//locale message key
: 'plugins.deploy.description',
})
.({
: {
: ,
: 'f',
//locale message key
: 'plugins.deploy.flags.fileType',
: 'js',
},
: {
: ,
: 'o',
//locale message key
: 'plugins.deploy.flags.ossType',
: 'aliyun',
},
: {
: 'i',
: 'plugins.deploy.flags.ignore',
// Array type
// To accept multiple values of a flag, wrap the type with an array:
: [],
: [],
},
: {
: [],
// Use a function to return an object or array
: () => [1, 2],
},
})
.(() => {
//do something
});
// execute command
// node ./dist/cli.js deploy --fileType=ts --ossType=tencent --ignore=a --ignore=b --manyNumbers=1 --manyNumbers=2 --manyNumbers=3
Advanced Usage
To seperate handlers from the cli definition, you can use the defineCommand
utility function:
CommandBuilder<Name, Ctx, object, Flags, { [K in Name]: Ctx & { flags: { [x: string]: never; noColor: boolean; logLevel: "Error" | "Warn" | "Info" | "Debug" | "Verbose" | undefined; hpsAppEnv: string; hpsEnvPath: string | undefined; projectCwd: string; }; }; }>
Returns a command builder object for further configuration and subcommand registration.
Example:
// @filename: @hyperse/wizard/dist/index.d.ts
import { } from '@hyperse/wizard';
const = <
'deploy',
{ : { : string; : string } }
>('deploy', {
//locale message key
: 'plugins.deploy.description',
});