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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import chalk from 'chalk';import { tokenize } from '../src/newparser/tokenizer';import { parse } from '../src/newparser/parser';import { errorBox } from '../src/errorBox';import { option } from '../src/option';import { number } from './test-types';import * as Result from '../src/Result';
test('works for multiple nodes', async () => { const argv = `hello world --some arg --flag --some another --flag --this-is=option -abcde=f -abcde`;
const tokens = tokenize(argv.split(' ')); const tree = parse(tokens, { longFlagKeys: new Set(), shortFlagKeys: new Set(), });
const opt = option({ type: number, long: 'some', });
const result = await opt.parse({ nodes: tree, visitedNodes: new Set(), });
if (Result.isOk(result)) { throw new Error('should fail...'); }
const errors = errorBox(tree, result.error.errors, []); expect(errors).toMatch('Too many times provided');});
test('works for a short flag', async () => { const argv = `hello world -fn not_a_number hey`;
const tokens = tokenize(argv.split(' ')); const tree = parse(tokens, { longFlagKeys: new Set(), shortFlagKeys: new Set(), });
const opt = option({ type: number, long: 'some', short: 'n', });
const result = await opt.parse({ nodes: tree, visitedNodes: new Set(), });
if (Result.isOk(result)) { throw new Error('should fail...'); }
const errors = errorBox(tree, result.error.errors, []); expect(errors).toMatch(chalk.red('n not_a_number'));});
test('works for a single node', async () => { const argv = `hello world --flag --some not_a_number --flag --this-is=option -abcde=f -abcde`;
const tokens = tokenize(argv.split(' ')); const tree = parse(tokens, { longFlagKeys: new Set(), shortFlagKeys: new Set(), });
const opt = option({ type: number, long: 'some', });
const result = await opt.parse({ nodes: tree, visitedNodes: new Set(), });
if (Result.isOk(result)) { throw new Error('should fail...'); }
const errors = errorBox(tree, result.error.errors, []); expect(errors).toMatch('Not a number');});
test('works when no nodes', async () => { const argv = `hello world --flag --flag --this-is=option -abcde=f -abcde`;
const tokens = tokenize(argv.split(' ')); const tree = parse(tokens, { longFlagKeys: new Set(), shortFlagKeys: new Set(), });
const opt = option({ type: number, long: 'some', });
const result = await opt.parse({ nodes: tree, visitedNodes: new Set(), });
if (Result.isOk(result)) { throw new Error('should fail...'); }
const errors = errorBox(tree, result.error.errors, []); expect(errors).toMatch(`No value provided for --some`);});