Skip to content

Help & Diagnostics ​

Clerc treats terminal output as a user interface: --help uses an editorial layout with a single hairline rule, uppercase section headings, smart column snapping, and atomic metadata tags that never split across line wraps.

Grouping Commands & Options ​

Organize large CLIs with .groups() and assign group keys on commands or flags:

ts
import { 
Types
,
clerc
} from "clerc";
const
cli
=
clerc
({
name
: "acme",
version
: "2.0.0",
description
: "Build and deploy modern TypeScript apps",
examples
: [["$ acme build src/index.ts -p 8080", "Build with custom port"]],
notes
: ["Set NO_COLOR=1 to disable ANSI colors."],
}).
groups
({
commands
: {
core
: "Core" },
flags
: {
input
: "Input Options" },
});
cli
.
command
("build <entry>", {
aliases
: ["b"],
group
: "core",
description
: "Compile source files",
args
: {
entry
: {
description
: "Entry file path" } },
flags
: {
output
: {
type
:
String
,
short
: "o",
description
: "Output directory" },
src
: {
type
:
String
,
group
: "input",
description
: "Source directory" },
cache
: {
type
:
Boolean
,
default
: true,
group
: "input",
description
: "Use build cache",
}, },
run
() {},
});
$ acme build --help
acme build v2.0.0  alias: b
Compile source files
────────────────────────────────────────────────────────────────────
USAGE   acme build [options] <entry>

ARGUMENTS
  <entry>                  Entry file path

OPTIONS
  -o, --output <string>    Output directory

INPUT OPTIONS
      --src <string>       Source directory
      --cache, --no-cache  Use build cache  [default: true]

GLOBAL OPTIONS
  -h, --help               Show help

Customizing Help Sections ​

You can add a header / footer, override individual section renderers via help.sections, or inspect the structured AST directly with buildHelpDocument(cli.buildModel()):

ts
import { 
clerc
,
defaultHelpSections
} from "clerc";
const
cli
=
clerc
({
name
: "acme",
version
: "2.0.0",
help
: {
footer
: (
meta
) => `Docs: https://example.com/${
meta
.
name
}`,
sections
: {
header
: (
doc
,
ctx
) =>
defaultHelpSections
.
header
(
doc
,
ctx
),
notes
: () =>
undefined
, // hide NOTES section
}, }, });

Inside any command handler, await ctx.help.print() prints help for the root or a specific subcommand path (await ctx.help.print(["db", "migrate"])).

Actionable Error Diagnostics ​

Parse and validation errors exit with code 2 (usage error), while runtime/schema errors exit with code 1. Unknown commands and unknown flags automatically compute Levenshtein distances to suggest the closest match:

$ acme deploy prod --outpt dist
error  unexpected option: '--outpt'

tip    a similar option exists: '--output'

       For more information, try 'acme deploy --help'.

Throw ClercError in your own commands to render matching diagnostics with detail, hints, and usage:

ts
import { 
ClercError
,
clerc
} from "clerc";
const
cli
=
clerc
({
name
: "acme",
version
: "1.0.0" }).
command
("publish", {
run
() {
throw new
ClercError
("not logged in to registry", {
code
: "ERR_VALIDATION",
detail
: "No token found in ~/.acmerc or ACME_TOKEN",
usage
: "acme login --registry https://registry.example.com",
}).
addHint
("run 'acme login' first or set ACME_TOKEN");
}, });

Custom .onError() & JSON Errors ​

Register .onError((err, ctx) => ...) handlers to intercept errors in reverse registration (LIFO) order. Returning true marks the error as handled and suppresses default diagnostic printing.

When ctx.log.format === "json" (or logger: { format: "json" }), unhandled errors serialize as single-line structured JSON to stderr:

json
{
  "error": {
    "code": "ERR_UNKNOWN_COMMAND",
    "message": "unknown command 'buid'",
    "hints": ["a similar command exists: 'build'"],
    "exitCode": 2
  }
}

Released under the MIT License