shellkitshellkit
Guide
API reference
Examples
Changelog
Releases
llms.txt
GitHub
  • 简体中文
  • English
Guide
API reference
Examples
Changelog
Releases
llms.txt
GitHub
  • 简体中文
  • English
  • Getting started

    • Guide
    • Install and upgrade
    • Thirty seconds
  • Core concepts

    • Five hard rules
    • Bash 3.2 compatibility
    • Module map
    • POSIX subset
  • Common tasks

    • Cookbook
    • CLI framework
    • Machine-readable output
    • Security boundaries
    • Internationalisation
    • Testing and gates
    • Working with AI
  • Going further

    • Example projects
    • Man pages
    • Migration guide
    • Compatibility policy
    • Releasing
    • Contributing
    • Changelog
  • Troubleshooting

    • Troubleshooting
    • FAQ

CLI framework

lib/core/cli.sh does five things only: registration, dispatch, help generation, option parsing, exit codes.

The key design decision: a new command never touches the dispatcher. cli_run finds cmd_<name> dynamically with declare -F, and hyphens in the command name become underscores (install-self → cmd_install_self).

Register

cli_group "ops"
cli_cmd deploy "deploy the service" "deploy [--force] <target>"
cli_cmd status "show status"

cli_group sets the heading a command appears under in help output; without it the command goes into the ungrouped section. The third argument of cli_cmd is a usage line -- omit it and help shows the bare command name.

Registration happens at source time, so cli_cmd belongs at the end of the command file. Put it earlier and the function does not exist yet; you only find out during dispatch (command has no handler).

Dispatch

cli_run "$@"

That single line is all your entry script needs. It consumes the global options first, then takes the first positional argument as the command name:

Global optionEffect
-h, --helpprint global usage (commands grouped, five options)
-V, --versionprint SK_VERSION (or dev when unset)
--verboseSHELLKIT_DEBUG=1, debug logging on
--no-colordisable colour
-y, --yesSHELLKIT_NONINTERACTIVE=1, prompts take their defaults

When the command name does not exist it suggests a prefix match and returns SK_USAGE:

[ERROR] no such command: deply
[Did you mean]: deploy

Options inside a subcommand

Three calls: clear the registry, register rules, parse.

cmd_deploy() {
  local __target="" __force="" __retries="3"

  cli_opt_init
  cli_opt __force --force -f
  cli_opt __target --target -t 1
  cli_opt __retries --retries -r 1
  cli_opts "$@" || return $?

  log_info "target=$__target force=$__force retries=$__retries"
  return "$SK_OK"
}

The rules:

  • cli_opt VAR LONG [SHORT] [TAKES_ARG]. A fourth argument of 1 means the option consumes a value.
  • Assign your defaults before registering -- cli_opt_init clears the registry, it does not touch your variables.
  • After parsing, the remaining positional arguments are in SK_OPTS_LEFT (an array starting at index 0).
  • Both --opt=value and --opt value work; a boolean option given = is rejected outright (stuffing garbage into a variable is harder to debug than an error).

Errors are reported once

log_error prints once and records the last message in SK_ERRMSG; cli_run does not print it again. So call log_error where you detect the problem, and do not combine log_error with die.

A complete minimal CLI

#!/usr/bin/env bash
set -euo pipefail

. ./vendor/shellkit/lib/bootstrap.sh

cli_group "main"
cli_cmd hello "say hello" "hello [--name NAME]"

cmd_hello() {
  local __name="world"
  cli_opt_init
  cli_opt __name --name -n 1
  cli_opts "$@" || return $?
  printf '%s\n' "hello $__name"
  return "$SK_OK"
}

SK_PROG=hello
cli_run "$@"

Note that printf writes to stdout (that is data) while logs go to stderr, so a pipeline such as ./hello --name x | jq is never polluted by log output.

Shell completion

You invented the subcommand names and the option names, so people will not remember them. The completion script is generated from the registry -- there is no second list to maintain:

shellkit completion bash > /etc/bash_completion.d/shellkit
shellkit completion zsh  > "${fpath[1]}/_shellkit"

Your own CLI can use the same function (the program name comes from SK_PROG):

cli_completion_script bash > mytool-completion.bash

A project generated by shellkit new ships this already: make completion prints the script, preferring the project's own vendor/shellkit/shellkit, so machines without shellkit installed can still generate it.

Last updated: 11/22/26, 7:50 PM
Prev
Cookbook
Next
Machine-readable output
给 AI 直读:llms.txt 一个文件读完整个站点的地图