Skip to content

flex-development/kronk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

kronk

ci github release npm npm downloads install size codecov module type: esm license conventional commits typescript vitest yarn

✨ a command line builder ✨

Contents

run-the-command-kronk

What is this?

kronk is a utility for building command-line applications in node.js and bun.

Install

This package is ESM only.

With yarn:

yarn add @flex-development/kronk
See Git - Protocols | Yarn  for info regarding installing from Git.

With bun:

bun add @flex-development/kronk
See bun add for more details.

Use

TODO: use

Creating a program

kronk exports a global program object convenient for quick programs. This is used in the examples throughout this README for brevity.

import { program } from '@flex-development/kronk'

For larger programs using kronk in multiple ways, including unit testing, a Command should be created.

import { Command } from '@flex-development/kronk'

const program: Command = new Command()

Options

Options are defined with the .option() and .options() methods, which also serve as documentation for the options. Each option can have at most 2 flags, typically one long flag and one short or shortish (e.g. --ws) flag. Flags can be separated by commas (,), pipes (|), or spaces ( ).

An option and its option-argument can be separated by an equal sign (=) or spaces ( ). A short option (i.e. -p) and its option-argument can also be combined.

serve --port 80
serve --port=80
serve -p 80
serve -p80
serve -p=80

Options on the command line are not positional, and can be specified before or after command-arguments.

serve --port=80 ./server.mts
serve ./src/main.mts -p8080

If a non-option (i.e. -13) looks like an option because it starts with a hyphen (-), a delimiter (--) can be used to demarcate the command-argument from an option.

clamp -M3 -m-1 -- -13

Parsed options can be accessed by calling .opts<T>() on a Command object, and are passed to the command's action handler. Multi-word options such as --template-engine are camelCased, becoming program.opts().templateEngine, or can be configured to be converted using snake_case, becoming program.opts().template_engine.

There are additional, related routines for when .opts<T>() is not enough:

API

Argument(info)

A command argument (class).

Signatures

  • constructor(info: ArgumentInfo | string)
  • constructor(info: string, data?: ArgumentData | null | undefined)

Parameters

Argument#id

string

The argument syntax id.

Argument#required

boolean

Whether the argument must have a value after parsing.

Argument#syntax

ArgumentSyntax

The normalized argument syntax string.

Argument#choices([choices])

Get or set argument choices.

Overloads
  • choices(): Set<string>
  • choices(choices: List<string> | null | undefined): this
Parameters
  • choices (List<string>, optional) — list of argument choices
Returns

(Set<string> | this) List of argument choices or this argument

Argument#default([info])

Get or set the default value configuration.

Overloads
  • default<T>(): DefaultInfo<T>
  • default(info: DefaultInfo | null | undefined): this
Type Parameters
  • T (any) — default value type
Parameters
Returns

(DefaultInfo<T> | this) Default value info or this argument

Argument#description([description])

Get or set the argument description.

Pass null, undefined, or an empty string to remove the argument from the auto-generated help text.

Overloads
  • description(): string
  • description(description: URL | string | null | undefined): this
Parameters
  • description (URL | string, optional) — the argument description
Returns

(string | this) Description of this argument or this argument

Argument#parser([parser])

Get or set the handler used to parse command-arguments.

Overloads
  • parser<T, V extends string | string[] = string | string[]>(): ParseArg<T, V>
  • parser(parser: ParseArg<any, any> | null | undefined): this
Type Parameters
  • T (any) — parse result
  • V (string | string[], optional) — the argument or arguments to parse
Parameters
Returns

(ParseArg<T, V> | this) The command-argument parser or this argument

Argument#toString()

Get the human readable equivalent of the argument.

Returns

(string) String representation of this argument

Argument#variadic

boolean

Whether the argument can be specified multiple times.

Command(info)

A command (class).

Signatures

  • constructor(info: CommandInfo | string)
  • constructor(info: string, data?: CommandData | null | undefined)

Parameters

  • info (CommandInfo | string) — command info or name
  • data? (CommandData, optional) — additional command info

Command#action([action])

TODO: Command#action([action])

Command#addArgument(argument)

TODO: Command#addArgument(argument)

Command#addCommand(subcommand)

TODO: Command#addCommand(subcommand)

Command#addOption(option)

TODO: Command#addOption(option)

Command#alias([alias])

TODO: Command#alias([alias])

Command#aliases([aliases])

TODO: Command#aliases([aliases])

Command#ancestors()

TODO: Command#ancestors()

Command#args

any[]

Parsed command-line arguments.

Command#argument(info[, data])

TODO: Command#argument(info[, data])

Command#arguments([infos])

TODO: Command#arguments([infos])

Command#argv

string[]

Raw command-line arguments.

Command#command(info[, data])

TODO: Command#command(info[, data])

Command#commands([infos])

TODO: Command#commands([infos])

Command#copyInheritedSettings(parent)

TODO: Command#copyInheritedSettings(parent)

Command#createArgument(info[, data])

TODO: Command#createArgument(info[, data])

Command#createCommand(info[, data])

TODO: Command#createCommand(info[, data])

Command#createOption(info[, data])

TODO: Command#createOption(info[, data])

Command#default

boolean

Whether the command is the default subcommand of its parent.

Command#defaultCommand

Command | null | undefined

The default command.

Command#description([description])

TODO: Command#description([description])

Command#done([done])

TODO: Command#done([done])

Command#emit(event)

TODO: Command#emit(event)

Command#emitOption(option, value, source[, flags])

TODO: Command#emitOption(option, value, source[, flags])

Command#error(info)

TODO: Command#error(info)

Command#exit([e])

TODO: Command#exit([e])

Command#exiter([exit])

TODO: Command#exiter([exit])

Command#findCommand(x)

TODO: Command#findCommand(x)

Command#findOption(flag[, direction])

TODO: Command#findOption(flag[, direction])

Command#hidden

boolean

Whether the command should not be displayed in help text.

Command#hide([hidden])

TODO: Command#hide([hidden])

Command#id([name])

TODO: Command#id([name])

Command#logger

Logger

Logger instance.

Command#on<T>(event, listener[, options])

TODO: Command#on<T>(event, listener[, options])

Command#option(info[, data])

TODO: Command#option(info[, data])

Command#optionValue(key[, value][, source])

TODO: Command#optionValue(key[, value][, source])

Command#optionValueSource(key[, source])

TODO: Command#optionValueSource(key[, source])

Command#options([infos])

TODO: Command#options([infos])

Command#opts<T>()

TODO: Command#opts<T>()

Command#optsWithGlobals<T>()

TODO: Command#optsWithGlobals<T>()

Command#parent

Command | null | undefined

The parent command.

Command#parse([argv][, options])

TODO: Command#parse([argv][, options])

Command#parseAsync([argv][, options])

TODO: Command#parseAsync([argv][, options])

Command#process

Process

Information about the current process.

Command#snapshot()

TODO: Command#snapshot()

Command#summary([summary])

TODO: Command#summary([summary])

Command#toString()

TODO: Command#toString()

Command#unknown

UnknownStrategy

The strategy for handling unknown command-line arguments.

Command#unknowns(strategy)

TODO: Command#unknowns(strategy)

Command#usage([usage])

TODO: Command#usage([usage])

Command#version([version])

TODO: Command#version([version])

CommandError(info)

A command error (class).

Extends

Parameters

CommandError#command

Command | null

The command where the error originated.

CommandError#snapshot()

Get a snapshot of the error.

Returns

(CommandErrorSnapshot) Error snapshot object

KronkError(info)

A command-line error (class).

Extends

  • Error

Signatures

  • constructor(info: KronkErrorInfo)
  • constructor(info: string, id?: EmptyString | KronkErrorId | null | undefined, code?: ExitCode | null | undefined)

Parameters

  • info (KronkErrorInfo | string) — error info or human-readable description of the error
  • id (EmptyString | KronkErrorId, optional) — unique id representing the error
  • code (ExitCode, optional) — suggested exit code to use with process.exit

KronkError#additional

string[]

Additional lines to be logged with the error.

KronkError#cause

KronkErrorCause | null | undefined

Info about the cause of the error.

KronkError#code

number

The suggested exit code to use with process.exit.

KronkError#id

KronkErrorId

Unique id representing the error.

KronkError#toJSON()

Get the error as a JSON object.

Returns

(KronkErrorJson) JSON representation of this error

KronkError#toString()

Get the error as a human-readable string.

Returns

(string) String representation of this error

KronkEvent(id)

An event (class).

Parameters

KronkEvent#id

KronkEventName

The unique id representing the event.

KronkEvent#toString()

Get the event as a human-readable string.

Returns

(string) String representation of this event

Option(info)

A command option (class).

Signatures

  • constructor(info: Flags | OptionInfo)
  • constructor(info: Flags, data?: OptionData | null | undefined)

Parameters

Option#boolean

boolean

Whether the option is a boolean option. Boolean options are options that do not take any option-arguments.

Option#choices([choices])

Get or set option choices.

Overloads
  • choices(): Set<string>
  • choices(choices: List<string> | null | undefined): this
Parameters
  • choices (List<string>, optional) — list of option choices
Returns

(Set<string> | this) List of option choices or this option

Option#default([info])

Get or set the default value configuration.

Overloads
  • default<T>(): DefaultInfo<T>
  • default(info: DefaultInfo | null | undefined): this
Type Parameters
  • T (any) — default value type
Parameters
Returns

(DefaultInfo<T> | this) Default value info or this option

Option#description([description])

Get or set the option description.

Overloads
  • description(): string
  • description(description: URL | string | null | undefined): this
Parameters
  • description (URL | string) — the option description
Returns

(string | this) Description of this option or this option

Option#env([env])

Get or set the environment variables to check for the value of the option.

Overloads
  • env(): Set<string>
  • env(env: List<string> | string | null | undefined): this
Parameters
  • env (List<string> | string | null | undefined) — the name of the environment variable to check, or a list of names, in order of priority, to check
Returns

(string | this) Environment variable names or this option

Option#event

OptionEventName

The event name for the option.

Option#flags

Flags

The normalized option flags string.

Option#hidden

boolean

Whether the option should not be displayed in help text.

Option#hide([hidden])

Remove the option from help text.

Parameters
  • hidden (boolean | null | undefined, optional) — whether the option should be hidden
    • default: true
Returns

(this) this option

Option#id

string

The option id.

Option#key

string

The option id in a format that can be used an object property key.

Option#long

string | null

The long flag for the option. If null, the short flag will be a non-empty string.

Option#mandatory

boolean

Whether the option must have a value after parsing.

Option#optional

boolean

Whether a value is optional when the option is specified.

Option#parser([parser])

Get or set the handler used to parse option-arguments.

Overloads
  • parser<T, V extends string | string[] = string | string[]>(): ParseArg<T, V>
  • parser(parser: ParseArg<any, any> | null | undefined): this
Type Parameters
  • T (any) — parse result
  • V (string | string[], optional) — the argument or arguments to parse
Parameters
Returns

(ParseArg<T, V> | this) The option-argument parser or this option

Option#preset([preset])

Get or set the preset to use when the option is specified without an argument.

The option-argument parser will be called.

Overloads
  • preset(): string | null
  • preset(preset: string | null | undefined): this
Parameters
  • preset (string | null | undefined) — the option-argument preset
Returns

(string | this | null) The option-argument preset or this option

Option#required

boolean

Whether a value must be supplied when the option is specified.

Option#short

string | null

The short flag for the option. If null, the long flag will be a non-empty string.

Option#toString()

Get the option as a human-readable string.

Returns

(string) String representation of this option

Option#variadic

boolean

Whether the option can be specified multiple times.

OptionEvent<T>(option, value, source[, flag])

A parsed option event (class).

Extends

Type Parameters
  • T (Option, optional) — Parsed command option
Parameters
  • option (T) — the command option instance
  • value (RawOptionValue) — the raw option value
  • source (OptionValueSource) — the source of the raw option value
  • flag (Flags, optional) — the parsed option flag

OptionEvent#flag

Flags | null | undefined

The parsed command option flag.

OptionEvent#id

Option['event']

The option event name.

OptionEvent#option

T

The command option instance.

OptionEvent#source

OptionValueSource

The source of the raw option value.

OptionEvent#value

RawOptionValue

The raw option value.

VersionOption(info)

A command version option (class).

Extends

Parameters

VersionOption#version

string

The version of the command.

Types

This package is fully typed with TypeScript.

Action

The callback to fire when a command is executed (TypeScript type).

type Action<
  Opts extends OptionValues = OptionValues,
  Args extends any[] = any[]
> = (
  this: Command,
  opts: Opts,
  ...args: Args
) => Awaitable<null | undefined | void>

Type Parameters

  • Opts (OptionValues, optional) — command options
  • Args (any[], optional) — command arguments

Parameters

  • this (Command) — the current command or subcommand being executed
  • options (Opts) — parsed command options
  • ...args (Args) — parsed command arguments

Returns

(Awaitable<null | undefined | void>) Nothing

ArgumentData

Data transfer object for command-arguments (TypeScript interface).

Properties

  • choices? (List<string>, optional) — list of argument choices
  • default? (DefaultInfo, optional) — default value configuration
  • description? (URL | string, optional) — description of the argument
  • parser? (ParseArg<any, any>, optional) — handler used to parse command-arguments.
    the handler receives two parameters, the raw, unparsed command-argument (or command-arguments for variadic options), and the previous (default) value for the argument. it should return the new value for the argument

ArgumentInfo

Data used to create command-arguments (TypeScript interface).

Extends

Properties

ArgumentMetadata

Command-argument metadata (TypeScript interface).

Extends

Properties

  • id (string) — argument syntax id
  • required (boolean) — whether required syntax was used when defining the argument
  • variadic (boolean) — whether variadic syntax was used when defining the argument

ArgumentSyntaxMap

Registry of strings used to define command and option arguments (TypeScript interface).

interface ArgumentSyntaxMap {/* see code */}

When developing extensions that use additional syntaxes, augment ArgumentSyntaxMap to register custom syntaxes:

declare module '@flex-development/kronk' {
  interface ArgumentSyntaxMap {
    requiredMaybe: `<${string}?>`
  }
}

ArgumentSyntax

Union of registered syntaxes used to define command and option arguments (TypeScript type).

To register custom syntaxes, augment ArgumentSyntaxMap. They will be added to this union automatically.

type ArgumentSyntax = ArgumentSyntaxMap[keyof ArgumentSyntaxMap]

ArgumentsData

Union of types used to create command arguments (TypeScript type).

type ArgumentsData =
  | ArgumentInfo
  | List<ArgumentInfo | ArgumentSyntax>
  | string

ArgvSourceMap

Registry of command-line argument sources (TypeScript interface).

interface ArgvSourceMap {/* see code */}

When developing extensions that use additional sources, augment ArgvSourceMap to register custom sources:

declare module '@flex-development/kronk' {
  interface ArgvSourceMap {
    electron: 'electron'
  }
}

ArgvSource

Union of registered command-line argument sources (TypeScript type).

To register custom sources, augment ArgvSourceMap. They will be added to this union automatically.

type ArgvSource = ArgvSourceMap[keyof ArgvSourceMap]

Awaitable

Create a union of T and T as a promise (TypeScript type).

Type Parameters

  • T (any, optional)
    • the value
type Awaitable<T = unknown> = Promise<T> | T

CommandData

Data transfer object for commands (TypeScript interface).

Properties

  • action? (Action<any>, optional) — callback to fire when the command is executed
  • aliases? (List<string> | string, optional) — aliases for the command
  • arguments? (List<string>, optional) — arguments for the command
  • default? (boolean, optional) — whether this is the default command
  • description? (URL | string, optional) — description of the command
  • done? (Action<any>, optional) — callback to fire after the command action is executed
  • exit? (Exit, optional) — callback to fire when the process is exited
  • hidden? (boolean, optional) — whether the command should be not displayed in help text
  • optionPriority? (OptionPriority, optional) — the strategy to use when merging global and local options
    • default: 'local'
  • options? (OptionsData, optional) — options for the command
  • parent? (Command, optional) — the parent command
  • subcommands? (SubcommandsData, optional) — subcommands for the command
  • summary? (string, optional) — a summary of the command
  • unknown? (UnknownStrategy, optional) — the strategy to use for handling unknown command-line arguments
    • default: false
  • usage? (UsageData, optional) — an object describing how the command is used
    • default: { arguments: null, options: '[options]', subcommand: '[command]' }
  • version? (VersionData, optional) — command version configuration

CommandErrorInfo

Data used to create command errors (TypeScript interface).

Extends

Properties

  • command? (Command, optional) — the command where the error originated
  • id (KronkErrorId) — unique id representing the error

CommandErrorSnapshot

Command error overview (TypeScript interface).

Extends

Properties

CommandInfo

Data used to create commands (TypeScript interface).

Extends

Properties

  • name? (CommandName, optional) — the name of the command

CommandMetadata

Command metadata (TypeScript interface).

Extends

Properties

  • arguments (Argument[]) — list of command arguments
  • options (Map<string, Option>) — map, where each key is a long or short flag and each value is the command option instance registered for that flag
  • parent? (null | undefined) — the parent command
  • subcommands (Map<string, Command>) — map, where each key is the name of a subcommand each value is a subcommand
  • version (VersionOption | null | undefined) — command version option

CommandName

The name of a command (TypeScript type).

Parent commands do not need to have a name, but all subcommands must have a name. Valid command names are non-empty strings.

type CommandName = string | null

CommandSnapshot

Object representing a command overview (TypeScript interface).

Properties

  • ancestors (CommandName[]) — list of ancestor command names
  • args (string[]) — list of parsed command arguments
  • argv (string[]) — list of raw command arguments
  • name (CommandName) — the name of the command
  • optionValueSources (OptionValueSources) — record, where each key is an option key and each value is the source of the parsed option value
  • opts (OptionValues) — parsed command options
  • optsWithGlobals (OptionValues) — parsed command options (with globals)

DefaultInfo

Data used to configure the default value of a command argument or option (TypeScript interface).

Type Parameters

  • T (any, optional) — default value type

Properties

  • description? (URL | string, optional) — description of the default value
  • value? (T, optional) — the default value
    • default: undefined

EmptyString

An empty string (TypeScript type).

type EmptyString = ''

ExitCode

Union of exit status code types (TypeScript type).

type ExitCode = number | string

ExitProcess

Terminate the process synchronously with an exit status of code (TypeScript type).

If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set.

type ExitProcess = (code?: ExitCode | null | undefined) => undefined

Parameters

  • code? (ExitCode, optional) — exit status code

Returns

(undefined) Nothing

Exit

The callback to fire when the process is exited (TypeScript type).

type Exit = (
  this: Command,
  e?: CommandError | KronkError | null | undefined
) => undefined

Parameters

Returns

(undefined) Nothing

Flags

A string comprised of option flags (TypeScript type).

The flags string can contain at most 2 flags, typically one long flag and one short or shortish (e.g. --ws) flag. Flags are separated by commas (,), pipes (|), or spaces.

A short flag is a hyphen — specifically HYPHEN-MINUS U+002D — followed by one case-insensitive alphanumeric character. The letters themselves have conventional meanings and are worth following, if possible.

A long flag starts with two hyphens followed by one or more case-insensitive alphanumeric characters. Using two hyphens prevents long flags from being confused for grouped short options. Hyphens and full stops (FULL STOP U+002E) can be used to separate words, as well as camelCase format.

Option-arguments are marked as required using empty angle brackets (<>) or by wrapping an argument id in angle brackets: <id>. Optional arguments use empty square brackets ([]) or have their id wrapped in square brackets: [id].

Variadic arguments are specified with an ellipsis wrapped in brackets (e.g. <...>, [...]) or by appending the ellipsis to the end of the argument id (<value...>, [value...]). Option-arguments can also be marked as mandatory by appending an exclamation mark to the end of the argument id: (<!>, <id!>, <value!...>).

type Flags = string

KronkErrorCause

Info about the cause of an error (TypeScript interface).

👉 Note: Symbol.hasInstance and Symbol.unscopables are used to identify arrays and functions.

interface KronkErrorCause {
  [Symbol.hasInstance]?: never
  [Symbol.unscopables]?: never
  [key: string]: any
}

KronkErrorId

Union of registered error ids (TypeScript type).

To register custom error ids, augment KronkErrorMap. They will be added to this union automatically.

type KronkErrorId = `kronk/${keyof KronkErrorMap}`

KronkErrorInfo

Data used to create errors (TypeScript interface).

Properties

  • additional? (string | string[], optional) — additional lines to be logged with the error
  • cause? (KronkErrorCause, optional) — info about the cause of the error
  • code? (ExitCode, optional) — the suggested exit code to use with process.exit
    • default: 1
  • id? (EmptyString | KronkErrorId, optional) — the unique id representing the error
    • default: 'kronk/error'
  • reason (string) — a human-readable description of the error

KronkErrorJson

JSON representation of an error (TypeScript interface).

Properties

  • additional (string[]) — additional lines to be logged with the error
  • cause? (KronkErrorCause, optional) — info about the cause of the error
  • code (number) — the suggested exit code to use with process.exit
  • id (KronkErrorId) — the unique id representing the error
  • message (string) — the human-readable description of the error
  • stack? (string, optional) — stack trace

KronkErrorMap

Registry of errors (TypeScript interface).

Each key is the suffix of an error id and each value is a KronkError.

interface KronkErrorMap {/* see code */}

When developing extensions that use additional errors, augment KronkErrorMap to register custom errors:

declare module '@flex-development/kronk' {
  interface KronkErrorMap {
    'action-error': CustomCommandError
    'parse-error': CustomKronkError
  }
}

KronkEventListener

Handle an event (TypeScript type).

type KronkEventListener<T extends KronkEvent = KronkEvent> = (
  event: T
) => undefined

Type Parameters

Parameters

  • event (T) — the emitted event

Returns

(undefined) Nothing

KronkEventNameMap

Registry of event names (TypeScript interface).

interface KronkEventNameMap extends OptionEventNameMap {/* see code */}

When developing extensions that use additional events, augment KronkEventNameMap to register custom event names:

declare module '@flex-development/kronk' {
  interface KronkEventNameMap {
    custom: 'command:custom'
  }
}

KronkEventName

Union of registered event names (TypeScript type).

To register custom event names, augment KronkEventNameMap. They will be added to this union automatically.

type KronkEventName = KronkEventNameMap[keyof KronkEventNameMap]

List

A list (TypeScript type).

Type Parameters

  • T (any, optional) — list item type
type List<T = unknown> = ReadonlySet<T> | readonly T[]

OptionData

Data transfer object for command options (TypeScript interface).

Properties

  • choices? (List<string>, optional) — list of option choices
  • default? (DefaultInfo, optional) — default value configuration

    👉 note: the option-argument parser will not be called.

  • description? (URL | string, optional) — description of the option
    • default: ''
  • env? (List<string>| string, optional) — the name of the environment variable to check for option value, or a list of names, in order of priority, to check
  • hidden? (boolean, optional) — whether the option should be not displayed in help text
    • default: false
  • mandatory? (boolean, optional) — whether the option is mandatory. mandatory options must have a value after parsing, which usually means the option must be specified on the command line
    • default: false
  • parser? (ParseArg<any, string> | ParseArg<any, string[]>, optional) — handler used to parse option-arguments. the handler receives two parameters, the raw, unparsed option-argument (or option-arguments for variadic options), and the previous (default) value for the argument. it should return the new value for the argument
  • preset? (string, optional) — for boolean and optional options, the preset to use when the option is specified without an option-argument.

    👉 note: the option-argument parser will be called.

  • snakecase? (boolean, optional) — whether to use snake_case format when converting the option id to an object property key

OptionEventListener

Handle a parsed command option event (TypeScript type).

type OptionEventListener<T extends Option = Option> = (
  event: OptionEvent<T>
) => undefined

Type Parameters

  • T (Option, optional) — the parsed command option

Parameters

Returns

(undefined) Nothing

OptionEventNameMap

Registry of option event names (TypeScript interface).

interface OptionEventNameMap {/* see code */}

When developing extensions that use additional events, augment OptionEventNameMap to register custom event names:

declare module '@flex-development/kronk' {
  interface OptionEventNameMap {
    custom: `option.${string}`
  }
}

OptionEventName

Union of registered option event names (TypeScript type).

To register custom event names, augment OptionEventNameMap. They will be added to this union automatically.

type OptionEventName = OptionEventNameMap[keyof OptionEventNameMap]

OptionInfo

Data used to create command options (TypeScript interface).

Extends

Properties

  • flags (Flags) — option flags

OptionMetadata

Command option metadata (TypeScript interface).

Extends

Properties

  • long (string | null | undefined) — long flag
  • optional (boolean) — whether a value is optional when the option is specified
  • required (boolean) — whether a value must be supplied when the option is specified
  • short (string | null | undefined) — short (or shortish, e.g. --ws) flag

    👉 note: if null or undefined, the long flag will be a non-empty string

  • variadic (boolean) — whether the option can be specified multiple times

OptionPriority

Union of strategies used when merging global and local options (TypeScript type).

  • global: global options overwrite local options
  • local: local options overwrite global options
type OptionPriority = 'global' | 'local'

OptionValueSourceMap

Registry of option value sources (TypeScript interface).

interface OptionValueSourceMap {/* see code */}

When developing extensions that use additional sources, augment OptionValueSourceMap to register custom sources:

declare module '@flex-development/kronk' {
  interface OptionValueSourceMap {
    builder: 'builder'
  }
}

OptionValueSource

Union of registered option value sources (TypeScript type).

To register custom sources, augment OptionValueSourceMap. They will be added to this union automatically.

type OptionValueSource = OptionValueSourceMap[keyof OptionValueSourceMap]

OptionValueSources

Record, where each key is an option key (Option.key) and each value is an OptionValueSource (TypeScript type).

type OptionValueSources = {
  [x: Option['key']]: OptionValueSource | null | undefined
}

OptionValues

Record, where each key is an option key (Option.key) and each value is a parsed option value (TypeScript type).

Type Parameters

  • T (any, optional) — parsed option value type
type OptionValues<T = any> = { [x: Option['key']]: T }

OptionsData

Union of types used to create command options (TypeScript type).

type OptionsData =
  | Flags
  | List<Flags | OptionInfo>
  | OptionInfo

ParseArg

Parse a command or option argument value (TypeScript type).

type ParseArg<T = any, Value extends string | string[] = string | string[]> = (
  this: void,
  value: Value,
  previous: T | undefined
) => T

Type Parameters

  • T (any, optional) — parse result
  • Value (string | string[], optional) — the argument or arguments to parse

Parameters

  • this (Command) — the current command or subcommand being executed
  • value (Value) — the raw argument or arguments to parse
  • previous (T | undefined) — the default argument value

Returns

(T) Parse result

ParseOptions

Options for parsing command-line arguments (TypeScript interface).

Properties

  • from? (ArgvSource, optional) — the source of the command line arguments

ProcessEnv

Information about the current user environment (TypeScript interface).

interface ProcessEnv {
  [key: string]: string | undefined
}

Process

Information about the current process (TypeScript interface).

Properties

  • argv (string[]) — list of command-line arguments passed when the process was launched
  • env (ProcessEnv) — object containing information about the user environment
  • exit (ExitProcess) — terminate the process synchronously with an exit status of code
  • exitCode? (ExitCode, optional) — the exit code to use when the process exits gracefully, or is exited via exit without specifying a code
  • stderr (WriteStream) — the writeable stream for standard error output
  • stdout (WriteStream) — the writeable stream for standard output

RawOptionValue

Union of raw option value types (TypeScript type).

type RawOptionValue = boolean | string | string[] | null

SubcommandInfo

Data used to create subcommands (TypeScript interface).

Extends

Properties

  • name (string) — the name of the subcommand

SubcommandsData

Union of types used to create subcommands (TypeScript type).

type SubcommandsData = SubcommandInfo | SubcommandsInfo

SubcommandsInfo

Record, where each key is the name of a subcommand and each value is an info object.

type SubcommandsInfo = { [subcommand: string]: CommandInfo }

UnknownStrategy

Union of values used to alter handling of unknown command-line arguments (TypeScript type).

  • 'arguments': allow unknown command-arguments only
  • 'options': allow unknown options only
  • false: disallow unknown command-arguments and options
  • true: allow unknown command-arguments and options
type UnknownStrategy = 'arguments' | 'options' | boolean

UsageData

An object describing command usage (TypeScript interface).

Properties

  • arguments? (string, optional) — command arguments descriptor

    👉 note: displayed in auto-generated help text only when a command has at least one visible argument

    • default: generated using visible command arguments
  • options? (string, optional) — command options descriptor

    👉 note: displayed in auto-generated help text only when a command has at least one visible option

    • default: '[options]'
  • subcommand? (string, optional) — subcommands descriptor

    👉 note: displayed in auto-generated help text only when a command has at least one visible subcommand

    • default: '[command]'

UsageInfo

Command usage info (TypeScript interface).

Extends

Properties

  • options (string) — command options descriptor

    👉 note: displayed in auto-generated help text only when a command has at least one visible option

  • subcommand (string) — subcommands descriptor

    👉 note: displayed in auto-generated help text only when a command has at least one visible subcommand

VersionData

Union of types used to configure the version of a Command (TypeScript type).

type VersionData = Version | VersionOption | VersionOptionInfo

VersionOptionInfo

Data used to create command version options (i.e. -v | --version) (TypeScript interface).

Extends

Properties

  • flags? (Flags, optional) — option flags
    • default: '-v | --version'
  • version (Version) — the command version

Version

Union of command version types (TypeScript type).

type Version = import('semver').SemVer | string

Contribute

See CONTRIBUTING.md.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages