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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137import chalk from "chalk";import { expect, test } from "vitest";import * as Result from "../src/Result";import { errorBox } from "../src/errorBox";import { parse } from "../src/newparser/parser";import { tokenize } from "../src/newparser/tokenizer";import { option } from "../src/option";import { createRegisterOptions } from "./createRegisterOptions";import { number } from "./test-types";
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, createRegisterOptions());
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, createRegisterOptions());
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, createRegisterOptions());
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, createRegisterOptions());
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");});
test("multiline non-highlight errors are indented", () => { const errors = errorBox( [], [ { nodes: [], message: "Failed to get missing value for '--token'\n" + "hint: one line.\n" + "╰▶ second line", }, ], [], );
expect(errors).toMatchInlineSnapshot( ` "error: found 1 error
1. Failed to get missing value for '--token' hint: one line. ╰▶ second line
hint: for more information, try ' --help'" `, );});