import { tupleWithOneElement } from './tupleWithOneElement'; import { Either } from 'fp-ts/lib/Either'; import * as t from 'io-ts'; import { MinimistResult, ParseItem } from './minimist'; import { TypeRecord, TROutput, TRErrors } from './composedType'; /** A string */ export declare const string: t.StringC; /** * Create a new type from string * * This is a handy utility to create a new `t.Type` without providing * details that aren't necessary for the two-way conversions: `clio-ts` * is only using a `string => T` conversion, while `io-ts` supports two sided * conversions: `string <=> T`. This is unnecessary and adds complexity, so this * tiny handler helps with providing defaults and `unimplemented` calls when necessary. */ export declare function fromStr(validator: t.Validate): t.Type; /** * A boolean argument, parses a string into a boolean: * * * `'true'` => true * * `'false'` => false * * otherwise => fails */ export declare const bool: t.Type; /** * An optional argument type. * * @param decoder an `io-ts` decoder to make undefinedable. * @example * ```ts * const optionalString = optional(t.string); * ``` */ export declare function optional(decoder: T): t.UnionC<[t.UndefinedC, T]>; /** * A command line argument parser */ export declare type Parser = { parse(argv: string[], context?: ParseItem[]): Either, Into>; }; /** * A named argument (`--long {value}` for the long names or `-s {value}` as short) */ export declare type NamedArgument = { kind: 'named'; /** * An `io-ts` decoder to be used for parsing the values. * * By default, the type expected to be able to parse from a list of strings, * so users will be able to write command line applications with multiple * named arguments with the same name: `--value=hello --value=world`. * * To allow only one value, use the [[single]] combinator that turns a decoder from string * to a decoder of a list of strings. */ type: t.Type; /** * A short (one-letter) name to be used. For instance, providing `s` would result in * allowing the user to pass `-s value` */ short?: string; /** * A long name to be used. For instance, providing `long-name` would result in * allowing the user to pass `--long-name value` */ long?: string; /** * A display name for the value, when showing help. For instance, when providing "hello", * it would result as `--long-name ` */ argumentName?: string; /** * An environment variable name to take as default, if given */ env?: string; /** * A description to be provided when showing help */ description?: string; /** * A default value, when no value is given */ defaultValue?: string; }; /** * A boolean argument (`--long` for long booleans, `-s` for short booleans) */ export declare type BooleanArgument = { kind: 'boolean'; /** * An `io-ts` decoder to be used for parsing the values. * * By default, the type expected to be able to parse from a list of strings, * so users will be able to write command line applications with multiple * named arguments with the same name: `--value=hello --value=world`. * * To allow only one value, use the [[single]] combinator that turns a decoder from string * to a decoder of a list of strings. */ type: t.Type; /** * A short (one-letter) name to be used. For instance, providing `s` would result in * allowing the user to pass `-s` */ short?: string; /** * A long name to be used. For instance, providing `long-name` would result in * allowing the user to pass `--long-name` */ long?: string; /** * A description to be provided when showing help */ description?: string; /** * A default value, when missing */ defaultValue?: boolean; }; /** * A positional argument */ export declare type PositionalArgument = { kind: 'positional'; /** * A type to parse from the string */ type: t.Type; /** * A display name for the argument. If missing, inferred from the given type */ displayName?: string; /** * A description to be provided when showing help */ description?: string; }; /** * An argument configuration */ export declare type Argument = PositionalArgument | NamedArgument | BooleanArgument; /** * A command configurations. An object where the keys are the results of a successful parse * and the values are a parsable [[Argument]] */ declare type CommandConfig = Record; /** * Creates a command line argument parser * * @param args the command arguments: an object where the keys are the names in your code * and the values are an [[Argument]] * @example * ```ts * const cmd = command({ * positional: { kind: 'positional', type: t.string }, * named: { kind: 'named', long: 'username', type: single(t.string), env: 'MY_APP_USER' }, * someBoolean: { kind: 'boolean', long: 'authenticate', short: 'a', type: bool } * }); * * const { positional, named, someBoolean } = parse(cmd, process.argv.slice(2)); * ``` * @returns [[Parser]] which parses into an object where its keys are the same * as the keys provided into the `args` argument, and the values are the result of the types for each key. */ export declare function command(args: Config, description?: string): Parser>; declare type ParseError = { parsed: MinimistResult; errors: TRErrors; commandConfig: CommandConfig; }; /** * Returns the value a parser resolves into */ export declare type Into

> = P extends Parser ? Into : never; declare type SubcommandResult>> = { [key in keyof Config]: { command: key; args: Into; }; } extends infer X ? X[keyof X] : never; /** * Lifts a parser (`command` or `subcommands`) into a binary parser * that can take a complete `process.argv` without slicing * * @example * ```ts * const cmd = command({ ... }); * const binary = binaryParser(cmd, 'my-app'); * const result = parse(binary, process.argv); * ``` */ export declare function binaryParser

>(p: P, binaryName?: string): Parser>; /** * Creates a subcommand selection in order to compose multiple commands into one * * @example * ```ts * const install = command({ ... }); * const uninstall = command({ ... }); * const cli = subcommands({ install, uninstall }); * const { command, args } = parse(cli, process.argv.slice(2)); * ``` */ export declare function subcommands>>(config: Config, description?: string): Parser>; /** * Parse arguments and exit on errors * * @param parser The command to parse with * @param args String arguments to pass to the command * @example * ```ts * const mycommand = command({ name: { kind: 'positional', type: t.string }); * const { name, _ } = parse(mycommand, ['hello', 'world']); * console.log(name); // => "hello" * console.log(_); // => ["world"] * ``` */ export declare function parse

>(parser: P, args: string[]): Into

; /** * Ensures that there's only one value provided for the argument. * * Takes an `io-ts` decoder that parses `A => B` and returns a decoder that parses `A[] => B` * and fails if there are more or less than one item. */ export declare const single: typeof tupleWithOneElement; export {};