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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import { Type, identity, InputOf, OutputOf } from './type';
/** * A number type to be used with `option` * * Throws an error when the provided string is not a number */export const number: Type<string, number> = { async from(str) { const decoded = parseInt(str, 10);
if (Number.isNaN(decoded)) { throw new Error('Not a number'); } else { return decoded; } }, displayName: 'number', description: 'a number',};
/** * A string type to be used with `option`. */export const string: Type<string, string> = { ...identity(), description: 'a string', displayName: 'str',};
/** * A boolean type to be used with `flag`. */export const boolean: Type<boolean, boolean> = { ...identity(), description: 'a boolean', displayName: 'true/false', defaultValue() { return false; },};
/** * Makes any type optional, by defaulting to `undefined`. */export function optional<T extends Type<any, any>>( t: T): Type<InputOf<T>, OutputOf<T> | undefined> { return { ...t, defaultValue(): OutputOf<T> | undefined { return undefined; }, };}