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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160import { ArgParser, ParsingError, ParsingResult, ParseContext,} from './argparser';import { OutputOf } from './from';import { findOption } from './newparser/findOption';import { ProvidesHelp, Descriptive, LongDoc, EnvDoc, ShortDoc,} from './helpdoc';import { Type, HasType } from './type';import chalk from 'chalk';import { Default } from './default';import { AllOrNothing } from './utils';import * as Result from './Result';
type OptionConfig<Decoder extends Type<string, any>> = LongDoc & HasType<Decoder> & Partial<Descriptive & EnvDoc & ShortDoc> & AllOrNothing<Default<OutputOf<Decoder>>>;
/** * 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: 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}`); }
const defaultValueFn = config.defaultValue ?? config.type.defaultValue;
if (defaultValueFn) { try { const defaultValue = defaultValueFn(); if ( config.defaultValueIsSerializable ?? config.type.defaultValueIsSerializable ) { defaults.push('default: ' + chalk.italic(defaultValue)); } else { defaults.push('optional'); } } catch (e) {} }
return [ { category: 'options', usage, defaults, description: config.description ?? config.type.description ?? 'self explanatory', }, ]; }, register(_opts) {}, 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 option = options[0]; let rawValue: string; let envPrefix = ''; const defaultValueFn = config.defaultValue ?? config.type.defaultValue;
if (option?.value) { rawValue = option.value.node.raw; } else if (valueFromEnv !== undefined) { rawValue = valueFromEnv; envPrefix = `env[${chalk.italic(config.env)}]: `; } else if (!option && typeof defaultValueFn === 'function') { try { return Result.ok(defaultValueFn()); } catch (e) { const message = `Default value not found for '--${config.long}': ${e.message}`; return Result.err({ errors: [ { nodes: [], message, }, ], }); } } else { const raw = option?.type === 'shortOption' ? `-${option?.key}` : `--${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); }, };}