Skip to content

combinators

WARNING

This module is experimental and may change in future versions.

Parser combinator entry point.

This entry point exports composable combinator factory functions for building type-safe argument schemas. These combinators produce ArgSchema objects that can be used anywhere regular argument schemas are accepted.

Example

ts
import { string, integer, boolean, required, withDefault, short } from 'gunshi/combinators'
import { define, cli } from 'gunshi'

const command = define({
  name: 'serve',
  args: {
    host: withDefault(string(), 'localhost'),
    port: withDefault(integer({ min: 1, max: 65535 }), 8080),
    verbose: short(boolean(), 'v')
  },
  run: ctx => {
    console.log(`Listening on ${ctx.values.host}:${ctx.values.port}`)
  }
})

Functions

FunctionDescription
argsType-safe schema factory.
booleanCreate a boolean argument schema.
choiceCreate an enum-like argument schema with literal type inference.
combinatorCreate a custom argument schema with a user-defined parse function.
describeSet a description on a combinator schema for help text generation.
extendExtend a schema by overriding or adding fields.
floatCreate a floating-point argument schema with optional range validation.
integerCreate an integer argument schema with optional range validation.
mapTransform the output of a combinator schema.
mergeCompose multiple Args schemas into one.
multipleMark a combinator schema as accepting multiple values.
numberCreate a number argument schema with optional range validation.
positionalCreate a positional argument schema.
requiredMark a combinator schema as required.
shortSet a short alias on a combinator schema.
stringCreate a string argument schema with optional validation.
unrequiredMark a combinator schema as not required.
withDefaultSet a default value on a combinator schema.

Interfaces

InterfaceDescription
BaseOptionsCommon options shared by all base combinators.
BooleanOptionsOptions for the boolean combinator.
CombinatorOptionsOptions for the combinator factory function.
FloatOptionsOptions for the float combinator.
IntegerOptionsOptions for the integer combinator.
NumberOptionsOptions for the number combinator.
StringOptionsOptions for the string combinator.

Type Aliases

Type AliasDescription
CombinatorA combinator produced by combinator factory functions.
CombinatorSchemaA schema produced by combinator factory functions. Any ArgSchema with a parse function qualifies.

Released under the MIT License.