fynjs API
    Preparing search index...

    Class Command

    Represents a command in the CLI application.

    The specification type for the command.

    This class provides methods to manage and execute CLI commands, including handling sub-commands, options, and aliases. It also includes functionality to generate help messages and verify the uniqueness of options and aliases.

    const cmdSpec: CommandSpec = { /* command specification */ };
    const command = new Command('start', cmdSpec);
    command.setCommandAliases(['run', 'execute'], 'start');
    const helpMessage = command.makeHelp('myProgram');
    console.log(helpMessage.join('\n'));

    Hierarchy

    Index
    • Creates an instance of a Command.

      Parameters

      • name: string

        The name of the command.

      • cmdSpec: CommandSpec

        The specification object for the command.

      • OptionalncConfig: NixClapConfig

        The NixClap configuration.

      • Optionalparent: Command

        An optional parent command.

      Returns Command

    args: ArgInfo[]

    Array of argument information for the command or option

    execCount: number

    The count of executable sub-commands.

    expectArgs: number

    The total number of expected arguments (required + optional)

    name: string

    The name of the command or option

    ncConfig?: NixClapConfig
    needArgs: number

    The number of required arguments

    options: Options

    The options associated with the command.

    parent?: Command

    The parent command, if any.

    The original specification provided for the command or option

    subAliases: Record<string, string>

    A record of sub-command aliases.

    subCmdCount: number
    subCmdsBase: Record<string, Command>
    variadic?: { max: number; min: number }

    Information about variadic arguments, if any

    • get alias(): string[]

      Returns string[]

    • get allowUnknownOption(): boolean

      Get the allowUnknownOption flag

      Returns boolean

      The allowUnknownOption flag

    • get cliType(): string

      Returns the CLI type (e.g., "command" or "option")

      Returns string

    • get cmdSpec(): CommandSpec

      Retrieves the command specification.

      Returns CommandSpec

      The specification of the command.

    • get desc(): string

      Returns string

    • get exec(): CommandExecFunc | CommandExecAsyncFunc

      Returns CommandExecFunc | CommandExecAsyncFunc

    • get hasArgs(): boolean

      Indicates whether this command or option has any arguments

      Returns boolean

    • get isSingleArg(): boolean

      Indicates whether this command or option has a single argument

      Returns boolean

    • get isVariadicArgs(): boolean

      Indicates whether this command or option has variadic arguments

      Returns boolean

    • get usage(): string

      Returns string

    • get verbatimArgs(): string

      Get the verbatim arguments

      Returns string

      The verbatim arguments

    • Get the number of commands, including sub commands, that has exec

      Returns number

    • Generates a help message for the command and its subcommands.

      Parameters

      • OptionalprogName: string

        Optional name of the program to be included in the help message.

      Returns string[]

      An array of strings representing the formatted help message.

    • Matches a sub-command based on the provided alias.

      Parameters

      • alias: string

        The alias of the sub-command to match.

      Returns CommandMatched

      An object containing the matched command's name, alias, and the command itself.

      If the alias does not directly match a sub-command, it will attempt to match using sub-aliases. If no match is found, the alias is returned as the name with an undefined command.

      const result = matchSubCommand('start');
      console.log(result.name); // 'start'
      console.log(result.alias); // 'start'
      console.log(result.cmd); // Command object or undefined
    • Processes the argument specification string and populates the args array.

      Returns void

      If the argument specification is invalid

    • Sets aliases for a command.

      Parameters

      • alias: string[]

        An array of alias strings to be associated with the command.

      • name: string

        The name of the command to which the aliases will be assigned.

      Returns void

      Throws an error if an alias is already used by another command.

    • Verifies that there are no duplicate options in the command. Iterates through the options and checks each one to ensure it does not already exist. Throws an error if a duplicate option is found (unless allowDuplicateOption is enabled).

      Returns void

    • Verifies that an option with the given name does not already exist in the current command or any of its parent commands. If the option exists, an error is thrown (unless allowDuplicateOption is enabled).

      Parameters

      • optName: string

        The name of the option to verify.

      • _from: Command

        The command from which the verification is initiated.

      Returns void

      If the option already exists in the current command or any parent command.