UNPKG

@eeue56/baner

Version:

Flag parsing library in Typescript

125 lines (124 loc) 3.59 kB
import { Result } from "@eeue56/ts-core/build/main/lib/result"; export declare type StringArgument = { kind: "StringArgument"; }; /** * An argument parser that treats an argument as a string */ export declare function string(): FlagArgument; export declare type NumberArgument = { kind: "NumberArgument"; }; /** * An argument parser that treats an argument as a number */ export declare function number(): FlagArgument; export declare type BooleanArgument = { kind: "BooleanArgument"; }; /** * An argument parser that treats an argument as a boolean */ export declare function boolean(): FlagArgument; export declare type EmptyArgument = { kind: "EmptyArgument"; }; /** * An argument parser that always passes */ export declare function empty(): FlagArgument; export declare type ListArgument = { kind: "ListArgument"; items: FlagArgument[]; }; /** * An argument parser that treats an argument as a list */ export declare function list(flagArgumentParsers: FlagArgument[]): FlagArgument; export declare type VariableListArgument = { kind: "VariableListArgument"; item: FlagArgument; }; /** * An argument parser that treats an argument as an enum */ export declare function oneOf(items: string[]): FlagArgument; export declare type OneOfArgument = { kind: "OneOfArgument"; items: string[]; }; /** * An argument parser that treats an argument as a list */ export declare function variableList(flagArgumentParser: FlagArgument): FlagArgument; declare type KnownTypes = string | number | boolean | null | KnownTypes[]; export declare type FlagArgument = StringArgument | NumberArgument | BooleanArgument | EmptyArgument | ListArgument | VariableListArgument | OneOfArgument; export declare type Short = { kind: "Short"; name: string; help: string; parser: FlagArgument; }; export declare type Long = { kind: "Long"; name: string; help: string; parser: FlagArgument; }; export declare type Both = { kind: "Both"; shortName: string; longName: string; help: string; parser: FlagArgument; }; export declare type Flag = Short | Long | Both; /** * A short flag, like -y */ export declare function shortFlag(name: string, help: string, parser: FlagArgument): Short; /** * A long flag, like --yes */ export declare function longFlag(name: string, help: string, parser: FlagArgument): Long; /** * A short or long flag, like -y or --yes */ export declare function bothFlag(shortName: string, longName: string, help: string, parser: FlagArgument): Both; /** * A program parser is composed of an array of flags */ export declare type ProgramParser = { flags: Flag[]; }; /** * A parser is composed of an array of flags */ export declare function parser(flags: Flag[]): ProgramParser; /** * A Program contains all arguments given to it, and an record of all the flags */ export declare type Program = { args: string[]; flags: Record<string, { isPresent: boolean; arguments: Result<string, KnownTypes>; }>; }; /** * Creates a help text for a given program parser */ export declare function help(flagParser: ProgramParser): string; /** * Reports all errors in a program, ignoring missing flags. */ export declare function allErrors(program: Program): string[]; /** * Reports missing flags, ignoring the ones you don't care about. */ export declare function allMissing(program: Program, ignore: string[]): string[]; /** * Runs a flag parser on the args */ export declare function parse(flagParser: ProgramParser, args: string[]): Program; export {};