@christianjuth/ts-cli-generator
v1.0.0-alpha.17
Published
Generate a CLI from TypeScript functions
Downloads
161
Readme
@christianjuth/ts-cli-generator
Create a new CLI project
npx @christianjuth/ts-cli-generator
Resources
Demo
How it works
The following index.ts
file:
import { call, CLI } from "@christianjuth/ts-cli-generator";
/**
* Add two numbers
*/
function add(x: number, y: number) {
console.log(x + y);
}
/**
* Subtract two numbers
*/
function _subtract(x: number, y: number) {
return x - y;
}
/**
* Add then subtract as separate interactioins
*/
async function addSubtract(x: number, y: number) {
console.log(x + y);
// call allows you to call a cli function
// from within another cli function
console.log(await call(_subtract)());
}
/**
* Get the length of a string
*/
function lengthOfString(str: string) {
console.log(str.length);
}
export const cli: CLI = {
add,
addSubtract,
lengthOfString,
// underscore means function is available but hidden
_subtract,
};
Will generate the following CLI:
name-of-cli CLI 0.0.0
Powered by @christianjuth/ts-cli-generator
Commands:
add Add two numbers
add-subtract Add then subtract as separate interactions
length-of-string Get the length of a string
Options:
-i, --interactive Run CLI in interactive mode
name-of-cli add
✔ param: x … 5
✔ param: y … 6
11
name-of-cli add-subtract
✔ param: x … 5
✔ param: y … 6
11
subtract
✔ param: x … 7
✔ param: y … 8
-1
name-of-cli length-of-string
✔ param: str … hello world
11