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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226#!/usr/bin/env YARN_SILENT=1 yarn ts-node
/* istanbul ignore file */
import { binary, boolean, command, extendType, flag, number, option, optional, positional, restPositionals, run, string, subcommands, union,} from "../src";import { Integer, ReadStream } from "./test-types";
const complex = command({ version: "6.6.6-alpha", args: { pos1: positional({ displayName: "pos1", type: Integer, }), pos2: positional(), named1: option({ type: Integer, short: "n", long: "number", }), intOrString: option({ type: union([Integer, string]), long: "int-or-string", }), floatOrString: option({ type: union([number, string]), long: "float-or-string", }), optionalOption: option({ type: optional(string), long: "optional-option", }), optionWithoutType: option({ long: "no-type-option", }), optionWithDefault: option({ long: "optional-with-default", env: "SOME_ENV_VAR", type: { ...string, defaultValue: () => "Hello", defaultValueIsSerializable: true, }, }), bool: flag({ type: boolean, long: "boolean", }), boolWithoutType: flag({ long: "bool-without-type" }), rest: restPositionals(), }, name: "printer", description: "Just prints the arguments", examples: [ { description: "Print with required args", command: "complex 42 hello -n 10 --int-or-string 5 --float-or-string 3.14", }, { description: "Using a string for int-or-string", command: "complex 1 world -n 1 --int-or-string foo --float-or-string bar", }, ], handler: (args) => { /** @export complex -> intOrString */ const x = args.intOrString;
/** @export complex -> floatOrString */ const y = args.floatOrString;
/** @export complex -> pos2 */ const pos2 = args.pos2;
/** @export complex -> boolWithoutType */ const boolWithoutType = args.boolWithoutType;
/** @export complex -> optionWithoutType */ const optionWithoutType = args.optionWithoutType;
/** @export complex -> rest */ const restPos = args.rest;
console.log( "I got", args, x, y, pos2, optionWithoutType, boolWithoutType, restPos, ); },});
const withStream = command({ args: { stream: positional({ displayName: "stream", type: ReadStream, }), restStreams: restPositionals({ displayName: "stream", type: ReadStream, }), }, description: "A simple `cat` clone", name: "cat", aliases: ["read"], handler: (result) => { /** @export cat -> stream */ const stream = result.stream; /** @export cat -> restStreams */ const restStreams = result.restStreams; [stream, ...restStreams].forEach((s) => s.pipe(process.stdout)); },});
const composed = subcommands({ name: "another-command", cmds: { cat: withStream, }, description: "a nested subcommand",});
const Name = extendType(string, { async from(s) { if (s.length === 0) { throw new Error("name cannot be empty"); } if (s === "Bon Jovi") { throw new Error(`Woah, we're half way there\nWoah! living on a prayer!`); } if (s.charAt(0).toUpperCase() !== s.charAt(0)) { throw new Error("name must be capitalized"); } return s; }, displayName: "name",});
const withSubcommands = subcommands({ cmds: { complex, cat: withStream, greet: command({ name: "greet", description: "greet a person", args: { times: option({ type: { ...Integer, defaultValue: () => 1 }, long: "times", }), name: positional({ displayName: "name", type: Name, }), noExclaim: flag({ type: boolean, long: "no-exclaim", }), greeting: option({ long: "greeting", type: string, description: "the greeting to say", env: "GREETING_NAME", }), }, examples: [ { description: "Greet someone", command: "greet --greeting Hello World", }, { description: "Greet multiple times", command: "greet --greeting Hi --times 3 Gal", }, ], handler: (result) => { const args = result; /** @export greet -> greeting */ const greeting = args.greeting; /** @export greet -> noExclaim */ const noExclaim = args.noExclaim; /** @export greet -> name */ const name = args.name; const exclaim = noExclaim ? "" : "!"; console.log(`${greeting}, ${name}${exclaim}`); }, }), composed, }, name: "subcmds", description: "An awesome subcommand app!", version: "1.0.0", examples: [ { description: "Show help for a command", command: "subcmds greet --help" }, { description: "Run the cat command", command: "subcmds cat ./file.txt" }, ],});
const cli = binary(withSubcommands);
async function main() { await run(cli, process.argv);}
main();