Interface Hooks

The hooks that can be registered and called to modify the behavior of the CLI, keyed by event name, listed in the order they are called.

interface Hooks {
    afterExecute: ((payload: {
        result: unknown;
        setResult: ((result: unknown) => void);
        state: State<unknown, OptionsConfig>;
    }) => MaybePromise<void>);
    afterParse: ((payload: {
        context: Context<OptionsConfig>;
        parsedOptions: {};
        setParsedOptions: ((optionValues: {}) => void);
    }) => MaybePromise<void>);
    afterResolve: ((payload: {
        addResolvedCommands: ((result: ResolvedCommand[]) => void);
        context: Context<OptionsConfig>;
        resolvedCommands: ResolvedCommand[];
    }) => MaybePromise<void>);
    afterStateChange: ((payload: {
        changed: Partial<NextState>;
        state: State<unknown, OptionsConfig>;
    }) => MaybePromise<void>);
    beforeEnd: ((payload: {
        data: unknown;
        setData: ((data: unknown) => void);
        state: State<unknown, OptionsConfig>;
    }) => MaybePromise<void>);
    beforeExecute: ((payload: {
        initialData: unknown;
        setInitialData: ((data: unknown) => void);
        setResultAndSkip: ((result: unknown) => void);
        skip: (() => void);
        state: State<unknown, OptionsConfig>;
    }) => MaybePromise<void>);
    beforeNext: ((payload: {
        data: unknown;
        nextCommand: undefined | ResolvedCommand;
        setData: ((data: unknown) => void);
        setNextCommand: ((command: ResolvedCommand) => void);
        state: State<unknown, OptionsConfig>;
    }) => MaybePromise<void>);
    beforeParse: ((payload: {
        commandString: string | string[];
        context: Context<OptionsConfig>;
        optionsConfig: OptionsConfig;
        setParsedOptionsAndSkip: ((optionValues: {}) => void);
        setParseFn: ((parseFn: ParseCommandFn) => void);
        skip: (() => void);
    }) => MaybePromise<void>);
    beforeResolve: ((payload: {
        addResolvedCommands: ((result: ResolvedCommand[]) => void);
        commandsDir: string;
        commandString: string;
        context: Context<OptionsConfig>;
        setParseFn: ((parseFn: ParseCommandFn) => void);
        setResolveFn: ((resolveFn: ResolveCommandFn) => void);
        skip: (() => void);
    }) => MaybePromise<void>);
    beforeResolveNext: ((payload: {
        addResolvedCommands: ((resolvedCommands: ResolvedCommand[]) => void);
        commandsDir: string;
        commandString: string;
        context: Context<OptionsConfig>;
        lastResolved: ResolvedCommand;
        setParseFn: ((parseFn: ParseCommandFn) => void);
        setResolveFn: ((resolveFn: ResolveCommandFn) => void);
        skip: (() => void);
    }) => MaybePromise<void>);
    beforeStateChange: ((payload: {
        changes: Partial<NextState>;
        setChanges: ((state: Partial<NextState>) => void);
        skip: (() => void);
        state: State<unknown, OptionsConfig>;
    }) => MaybePromise<void>);
    error: ((payload: {
        context: Context<OptionsConfig>;
        error: unknown;
        ignore: (() => void);
        setError: ((error: unknown) => void);
    }) => MaybePromise<void>);
    exit: ((payload: {
        cancel: (() => void);
        code: number;
        context: Context<OptionsConfig>;
        message?: any;
        setCode: ((code: number) => void);
        setMessage: ((message: any) => void);
    }) => MaybePromise<void>);
}

Properties

afterExecute: ((payload: {
    result: unknown;
    setResult: ((result: unknown) => void);
    state: State<unknown, OptionsConfig>;
}) => MaybePromise<void>)
  1. Called once for every execution.
afterParse: ((payload: {
    context: Context<OptionsConfig>;
    parsedOptions: {};
    setParsedOptions: ((optionValues: {}) => void);
}) => MaybePromise<void>)
  1. Called once during preparation.
afterResolve: ((payload: {
    addResolvedCommands: ((result: ResolvedCommand[]) => void);
    context: Context<OptionsConfig>;
    resolvedCommands: ResolvedCommand[];
}) => MaybePromise<void>)
  1. Called once during preparation.
afterStateChange: ((payload: {
    changed: Partial<NextState>;
    state: State<unknown, OptionsConfig>;
}) => MaybePromise<void>)
  1. Called every time the state changes during execution.
beforeEnd: ((payload: {
    data: unknown;
    setData: ((data: unknown) => void);
    state: State<unknown, OptionsConfig>;
}) => MaybePromise<void>)
  1. Called once per execution, before the final state change, if state.end() is called.
beforeExecute: ((payload: {
    initialData: unknown;
    setInitialData: ((data: unknown) => void);
    setResultAndSkip: ((result: unknown) => void);
    skip: (() => void);
    state: State<unknown, OptionsConfig>;
}) => MaybePromise<void>)
  1. Called once for every execution.
beforeNext: ((payload: {
    data: unknown;
    nextCommand: undefined | ResolvedCommand;
    setData: ((data: unknown) => void);
    setNextCommand: ((command: ResolvedCommand) => void);
    state: State<unknown, OptionsConfig>;
}) => MaybePromise<void>)
  1. Called for every call of state.next().

state.start() calls state.next() internally, so this hook is called before the first command is executed.

beforeParse: ((payload: {
    commandString: string | string[];
    context: Context<OptionsConfig>;
    optionsConfig: OptionsConfig;
    setParsedOptionsAndSkip: ((optionValues: {}) => void);
    setParseFn: ((parseFn: ParseCommandFn) => void);
    skip: (() => void);
}) => MaybePromise<void>)
  1. Called once during preparation.
beforeResolve: ((payload: {
    addResolvedCommands: ((result: ResolvedCommand[]) => void);
    commandsDir: string;
    commandString: string;
    context: Context<OptionsConfig>;
    setParseFn: ((parseFn: ParseCommandFn) => void);
    setResolveFn: ((resolveFn: ResolveCommandFn) => void);
    skip: (() => void);
}) => MaybePromise<void>)
  1. Called once during preparation.
beforeResolveNext: ((payload: {
    addResolvedCommands: ((resolvedCommands: ResolvedCommand[]) => void);
    commandsDir: string;
    commandString: string;
    context: Context<OptionsConfig>;
    lastResolved: ResolvedCommand;
    setParseFn: ((parseFn: ParseCommandFn) => void);
    setResolveFn: ((resolveFn: ResolveCommandFn) => void);
    skip: (() => void);
}) => MaybePromise<void>)
  1. Called once for each subcommand during preparation.
beforeStateChange: ((payload: {
    changes: Partial<NextState>;
    setChanges: ((state: Partial<NextState>) => void);
    skip: (() => void);
    state: State<unknown, OptionsConfig>;
}) => MaybePromise<void>)
  1. Called every time the state changes during execution.
error: ((payload: {
    context: Context<OptionsConfig>;
    error: unknown;
    ignore: (() => void);
    setError: ((error: unknown) => void);
}) => MaybePromise<void>)

Called whenever an error is thrown.

exit: ((payload: {
    cancel: (() => void);
    code: number;
    context: Context<OptionsConfig>;
    message?: any;
    setCode: ((code: number) => void);
    setMessage: ((message: any) => void);
}) => MaybePromise<void>)

Called whenever a plugin or command intend to exit the process.