Skip to content

Function: lazy() ​

Define a lazy command with or without definition.

Call Signature ​

ts
export function lazy<A extends Args>(loader: CommandLoader<{ args: A; extensions: {} }>): LazyCommand<{ args: A; extensions: {} }, {}>

Define a lazy command.

Type Parameters ​

NameDescription
A extends ArgsAn Args type

Parameters ​

NameTypeDescription
loaderCommandLoader<{ args: A; extensions:>A command loader

Returns ​

LazyCommand<{ args: A; extensions: {} }, {}> — A lazy command with loader

Examples ​

ts
// load command with dynamic importing
const test = lazy(() => import('./commands/test'))

Call Signature ​

ts
export function lazy<
  D extends { args: Args } & Partial<Command<{ args: D['args']; extensions: {} }>>
>(loader: CommandLoader<{ args: D['args']; extensions: {} }>, definition: D &
    UnknownKeys<D, LazyDefinitionShape<{ args: D['args']; extensions: {} }>> &
    UnknownArgKeysOf<D> &
    WithoutRun<D>): LazyCommand<{ args: D['args']; extensions: {} }, D>

Define a lazy command with definition.

Type Parameters ​

NameDescription
D extends { args: Args } & Partial<Command<{ args: D['args']; extensions:>>A partial Command definition type with required args

Parameters ​

NameTypeDescription
loaderCommandLoader<{ args: D['args']; extensions:>A command loader function that returns a command definition
definitionD & UnknownKeys<D, LazyDefinitionShape<{ args: D['args']; extensions: {} }>> & UnknownArgKeysOf<D> & WithoutRun<D>A command definition without run, which the loader returns. Its name may be omitted, including when the lazy command is used as the CLI entry command.

Returns ​

LazyCommand<{ args: D['args']; extensions: {} }, D> — A lazy command that can be executed later

Examples ​

ts
// define command without command runner
const testDefinition = define({
  name: 'test',
  description: 'Test command',
  args: {
    debug: {
      type: 'boolean',
      description: 'Enable debug mode',
      default: false
    }
  },
})

// define load command with command runner and defined command
const test = lazy((): CommandRunner<{ args: typeof testDefinition.args; extensions: {} }> => {
  return ctx => {
    if (ctx.values.debug) {
      console.debug('Debug mode is enabled');
    }
  }
}, testDefinition)

Call Signature ​

ts
export function lazy<
  G extends GunshiParamsConstraint = DefaultGunshiParams,
  D extends Partial<Command<G>> = Omit<Partial<Command<G>>, 'run'>
>(loader: CommandLoader<G>, definition?: D &
    WithoutJsonArgs<D> &
    UnknownKeys<D, LazyDefinitionShape<G>> &
    UnknownArgKeysOf<D> &
    WithoutRun<D>): LazyCommand<G, D>

Define a lazy command with explicit Gunshi parameters and optional definition.

Type Parameters ​

NameDescription
G extends GunshiParamsConstraint = DefaultGunshiParamsA GunshiParamsConstraint
D extends Partial<Command<G>> = Omit<Partial<Command<G>>, 'run'>A partial Command definition type

Parameters ​

NameTypeDescription
loaderCommandLoader<G>A command loader function that returns a command definition
definitionD & WithoutJsonArgs<D> & UnknownKeys<D, LazyDefinitionShape<G>> & UnknownArgKeysOf<D> & WithoutRun<D>An optional command definition without run, which the loader returns. Its name may be omitted, including when the lazy command is used as the CLI entry command. (optional)

Returns ​

LazyCommand<G, D> — A lazy command that can be executed later

Released under the MIT License.