Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Schniz/cmd-ts. 💻 A type-driven command line argument parser cmd-ts.now.sh
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218import chalk from "chalk";import * as Result from "./Result";import type { ArgParser, ParseContext, ParsingError, ParsingResult,} from "./argparser";import type { Default, OnMissing } from "./default";import type { OutputOf } from "./from";import type { Descriptive, EnvDoc, LongDoc, ProvidesHelp, ShortDoc,} from "./helpdoc";import { findOption } from "./newparser/findOption";import type { HasType, Type } from "./type";import { string } from "./types";import type { AllOrNothing } from "./utils";
type OptionConfig<Decoder extends Type<string, any>> = LongDoc & HasType<Decoder> & Partial<Descriptive & EnvDoc & ShortDoc & OnMissing<OutputOf<Decoder>>> & AllOrNothing<Default<OutputOf<Decoder>>>;
function fullOption<Decoder extends Type<string, any>>( config: OptionConfig<Decoder>,): ArgParser<OutputOf<Decoder>> & ProvidesHelp & Partial<Descriptive> { return { description: config.description ?? config.type.description, helpTopics() { const displayName = config.type.displayName ?? "value"; let usage = `--${config.long}`; if (config.short) { usage += `, -${config.short}`; } usage += ` <${displayName}>`;
const defaults: string[] = [];
if (config.env) { const env = process.env[config.env] === undefined ? "" : `=${chalk.italic(process.env[config.env])}`; defaults.push(`env: ${config.env}${env}`); }
if (config.defaultValue) { try { const defaultValue = config.defaultValue(); if (config.defaultValueIsSerializable) { defaults.push(`default: ${chalk.italic(defaultValue)}`); } else { defaults.push("optional"); } } catch (e) {} } else if (config.type.defaultValue) { try { const defaultValue = config.type.defaultValue(); if (config.type.defaultValueIsSerializable) { defaults.push(`default: ${chalk.italic(defaultValue)}`); } else { defaults.push("optional"); } } catch (e) {} } else if (config.onMissing || config.type.onMissing) { defaults.push("optional"); }
return [ { category: "options", usage, defaults, description: config.description ?? config.type.description ?? "self explanatory", }, ]; }, register(opts) { opts.forceOptionLongNames.add(config.long); if (config.short) { opts.forceOptionShortNames.add(config.short); } }, async parse({ nodes, visitedNodes, }: ParseContext): Promise<ParsingResult<OutputOf<Decoder>>> { const options = findOption(nodes, { longNames: [config.long], shortNames: config.short ? [config.short] : [], }).filter((x) => !visitedNodes.has(x));
options.forEach((opt) => visitedNodes.add(opt));
if (options.length > 1) { const error: ParsingError = { message: `Too many times provided. Expected 1, got: ${options.length}`, nodes: options, }; return Result.err({ errors: [error] }); }
const valueFromEnv = config.env ? process.env[config.env] : undefined; const defaultValueFn = config.defaultValue || config.type.defaultValue; const onMissingFn = config.onMissing || config.type.onMissing;
const option = options[0]; let rawValue: string; let envPrefix = ""; if (option?.value) { rawValue = option.value.node.raw; } else if (valueFromEnv !== undefined) { rawValue = valueFromEnv; envPrefix = `env[${chalk.italic(config.env)}]: `; } else if (defaultValueFn) { try { const defaultValue = defaultValueFn(); return Result.ok(defaultValue); } catch (e: any) { const message = `Default value not found for '--${config.long}': ${e.message}`; return Result.err({ errors: [ { nodes: [], message, }, ], }); } } else if (onMissingFn) { try { const missingValue = await onMissingFn(); return Result.ok(missingValue); } catch (e: any) { const message = `Failed to get missing value for '--${config.long}': ${e.message}`; return Result.err({ errors: [ { nodes: [], message, }, ], }); } } else { // If we reach here, no default or prompt value was available const raw = option?.type === "shortOption" ? `-${option?.key}` : `--${config.long}`; return Result.err({ errors: [ { nodes: options, message: `No value provided for ${raw}`, }, ], }); }
const decoded = await Result.safeAsync(config.type.from(rawValue)); if (Result.isErr(decoded)) { return Result.err({ errors: [ { nodes: options, message: envPrefix + decoded.error.message }, ], }); }
return Result.ok(decoded.value); }, };}
type StringType = Type<string, string>;
/** * Decodes an argument which is in the form of a key and a value, and allows parsing the following ways: * * - `--long=value` where `long` is the provided `long` * - `--long value` where `long` is the provided `long` * - `-s=value` where `s` is the provided `short` * - `-s value` where `s` is the provided `short` * @param config flag configurations */export function option<Decoder extends Type<string, any>>( config: LongDoc & HasType<Decoder> & Partial<Descriptive & EnvDoc & ShortDoc & OnMissing<OutputOf<Decoder>>> & AllOrNothing<Default<OutputOf<Decoder>>>,): ArgParser<OutputOf<Decoder>> & ProvidesHelp & Partial<Descriptive>;export function option( config: LongDoc & Partial< HasType<never> & Descriptive & EnvDoc & ShortDoc & OnMissing<OutputOf<StringType>> > & AllOrNothing<Default<OutputOf<StringType>>>,): ArgParser<OutputOf<StringType>> & ProvidesHelp & Partial<Descriptive>;export function option( config: LongDoc & Partial<HasType<any>> & Partial<Descriptive & EnvDoc & ShortDoc>,): ArgParser<OutputOf<any>> & ProvidesHelp & Partial<Descriptive> { return fullOption({ type: string, ...config, });}