Generate formatted help text for Node.js CLIs.
help
doesn't attempt to parse arguments as some more comprehensive CLI frameworks do.
Instead, it aims to be an unopinionated primitive that can be used as a stand-alone utility or as a
building block within a more comprehensive CLI framework
- A simple and intuitive functional API
- Flexibility: Makes no assumptions about your CLI's argument scheme
- Dynamic line wrapping based on terminal width
- Extensible built-in functions
- Full TypeScript type support
pnpm install @theurgi/help
import { help, heading, paragraph, space, table } from '@theurgi/help'
console.log(
help({
display: [
paragraph('My awesome CLI'),
space(),
heading('Usage'),
paragraph('$ cli <command> [options]', { indentLevel: 1 }),
space(),
heading('Commands'),
table([
['create', 'Create something'],
['update', 'Update something'],
]),
space(),
heading('Options'),
table([
['-h, --help', 'Show help'],
['-v, --version', 'Show version'],
]),
],
})
)
- Generator
- HeadingOptions
- HelpConfig
- HelpOptions
- IndentLevel
- IndentSpaces
- ParagraphOptions
- RenderFunction
- TableAlignment
- TableOptions
▸ help(helpConfig
): string
Generate formatted help text for Node.js CLIs.
Name | Type | Description |
---|---|---|
helpConfig |
HelpConfig |
The help configuration. |
string
▸ heading(text
, options?
): RenderFunction
Generate a heading.
Name | Type | Description |
---|---|---|
text |
string |
The heading text. |
options? |
Partial <HeadingOptions> |
The heading options. |
▸ paragraph(text
, options?
): RenderFunction
Generate a paragraph.
Name | Type | Description |
---|---|---|
text |
string |
The paragraph text. |
options? |
Partial <ParagraphOptions> |
The paragraph options. |
▸ space(newlines?
): RenderFunction
Generate blank newlines.
Name | Type | Description | Default |
---|---|---|---|
newlines? |
number |
The number of newlines to render. | 1 |
▸ table(data
, options?
): RenderFunction
Generate a 2 column table.
Name | Type | Description |
---|---|---|
data |
[string, string][] |
An array of string tuples where data[n][0] is typically a CLI command or option and data[n][1] is typically a description of data[n][0] . |
options? |
Partial <TableOptions> |
The table options. |
Ƭ Generator<T
>: (...parameters
: Parameters
<T
>) => RenderFunction
NOTE: A
Generator
in the context ofhelp
has no relation to JavaScript Generators.
A Generator
is a function that is called inside the HelpConfig.display
array
of the main help
function and returns a RenderFunction
.
Name | Type |
---|---|
T |
extends (...parameters : any ) => RenderFunction |
▸ (...parameters
): RenderFunction
Name | Type |
---|---|
...parameters |
Parameters <T > |
Ƭ HeadingOptions: Object
Name | Type | Description | Default |
---|---|---|---|
indentLevel |
IndentLevel |
The level of indentation for the heading. | 0 |
Ƭ HelpConfig: Object
Name | Type | Description |
---|---|---|
display |
RenderFunction [] |
An array in which to call Generator functions to render portions of the help text. |
options? |
Partial <HelpOptions > |
Global options for the help text. |
Ƭ HelpOptions: Object
Name | Type | Description | Default |
---|---|---|---|
indentSpaces |
IndentSpaces |
The number of spaces used for each indentation level. | 2 |
maxWidth |
number |
The maximum width of the help text in characters. | The terminal width. |
Ƭ IndentLevel: number
The number of times to left pad a string with IndentSpaces
spaces.
Ƭ IndentSpaces: number
The number of spaces per IndentLevel
.
Ƭ ParagraphOptions: Object
Name | Type | Description | Default |
---|---|---|---|
indentLevel |
IndentLevel |
The level of indentation for the paragraph. | 0 |
Ƭ RenderFunction: (helpOptions
: HelpOptions
) => string
▸ (helpOptions
): string
The type of function that must be returned by a Generator
. This function will be called with
global HelpOptions
by the main help
function to render a formatted string.
Name | Type |
---|---|
helpOptions |
HelpOptions |
string
Ƭ TableOptions: 'center' | 'justify' | 'left' | 'right'
The horizontal alignment of a table
column.
Ƭ TableOptions: Object
Name | Type | Description | Default |
---|---|---|---|
columnGap |
number |
The number of spaces between columns. | 2 |
indentLevel |
IndentLevel |
The level of indentation for the table. | 1 |
leftColAlign |
TableAlignment |
The alignment of the left column. | 'left' |
rightColAlign |
TableAlignment |
The alignment of the right column | 'left' |