ts-cli-creator <entry>
Given the entry file:
export interface Options {
foo: string
}
function command(param: string, options: Options): void {
/* your code here */
}
export default command
Will generate:
import * as yargs from 'yargs'
import handler from './handler'
export default function main(args: string[] = process.argv.slice(2)): void {
yargs
.strict()
.command(
'$0 <param>', '',
yargs => {
return yargs
.positional('param', { type: string, demandOption: "true" })
.option('foo', { type: string })
},
args => {
const { _, $0, param, ...options } = args
handler(param, options)
})
.help()
.alias('help', 'h')
.parse(args)
}
ts-cli-creator generate a yargs commander from function declaration.
function command(foo: string, bar?: number) {}
Transform to:
.command(`$0 <foo> <bar>`, ``,
yargs => {
return yargs
.positional(`foo`, { type: string, demandOption: "true" })
.positional(`bar`, { type: number })
},
args => {
const { _, $0, foo, bar } = args
handler(foo, bar)
}
)
Supports positional argument types:
Typescript Types | Yargs Positional Options |
---|---|
string |
{ type: 'string' } |
number |
{ type: 'number' } |
boolean |
{ type: 'boolean' } |
enum E(string iterial only) |
{ choices: E[] } |
/**
* Description for commander
*/
function command() {}
Transform to:
.command(`$0`, `Description for command`)
When the name of function last param matched /^options?$/
, and type was interface declaration. like:
interface Options {}
function command(param: string, options: Options) {}
ts-cli-creator will generate yargs options for you:
interface Options {
foo: string
}
Transform to:
.option(`foo`, { type: `string` })
Supports options types:
Typescript Types | Yargs Options |
---|---|
string |
{ type: 'string' } |
number |
{ type: 'number' } |
boolean |
{ type: 'boolean' } |
string[] |
{ type: 'string', array: true } |
number[] |
{ type: 'number', array: true } |
boolean[] |
{ type: 'boolean', array: true } |
enum E(string iterial only) |
{ choices: E[] } |
interface Options {
/** A description for foo */
foo: string
}
Transform to:
.option('foo', {
type: 'string',
description: 'A description for foo'
})
interface Options {
/**
* @default 42
* @demandOption
*/
foo: number,
/**
* @alias b
* @default 'baz'
*/
bar: string
}
Transform to:
.option(`foo`, {
type: `number`,
default: 42,
demandOption: true
})
.option(`bar`, {
type: `string`,
alias: `b`,
default: `baz`
})
Supports options properties:
JSDoc tag | Yargs option |
---|---|
@alias |
alias |
@default |
default |
@demandOption |
demandOption |
@require |
demandOption |
@required |
demandOption |
ts-cli-creator ./src/handler.ts
ts-cli-creator ./src/handler.ts -o ./cli.ts
Generate file to ./src/cli.ts
. The output path relative entry directory path when not use absolute path.
cat ./src/handler.ts | ts-cli-creator
or
echo function add(a:number,b:number){} | ts-cli-creator
Warning. this mode will inline the code to output content replace require the entry module
You can preview cli message via --runnable
option and pass raw arguments:
ts-cli-creator ./src/handler.ts --no-color --runnable --js -- -h | node
Or use ts-node:
ts-cli-creator ./src/handler.ts --no-color --runnable -- -h | ts-node -T --skip-project ./cli.ts
See the simple example:
echo function add(a:number,b:number){console.log(a+b)} | ts-cli-creator --no-color --js -- 1 2 | node
## will output 3
Name | Description | Type | Default |
---|---|---|---|
--output, -o | Output file path, output to stdout when not set | string |
undefined |
--js | Generate js file, base on your tsconfig | boolean |
false |
--json | Output json data | boolean |
false |
--color | Colourful output with write to stdout | boolean |
true |
--verbose | Output full infomations | boolean |
false |
--function-name | Generate Wrapper function name | string |
cli |
--async-function | Use async function | boolean |
false |
--runnable | Add main function call at last, default to false | boolean |
false |
--strict | enable strict mode | boolean |
true |
--helper | global helper options to show helper messages | boolean |
true |
--helper-alias | helper options short for 'h' | boolean |
true |
--version | global version options, show current version | boolean |
true |
- Sub commander
- Custom type parser
- Other cli provider, like commander