Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

typescript-to-cli

> 开发工具
Open source

Transform your typescript module into a CLI

100 stars0 likes3 views
WebsiteGitHub

About

Transform your typescript module into a CLI

typescript-to-cli

Transform your typescript module into a CLI

typescript-to-cli leverages the typescript type system to generate a CLI based on the exported function signature of your module.

  • Parses the program options
  • Validates if the options have the right type
  • Suggests correction if the options contain a typo
  • Generates help based on the documentation

:warning:

This is an experimental project! A lot of features are missing and it probably contains some bugs. If one of your use case is not supported, please create an issue or contact me on twitter.

:warning:

Usage

Let's take the following module as an example.

send-ships.ts

typescript
/**
 * Send ships to the specified destination
 *
 * @param destination the ships target
 * @param numberOfShips the number of ships to send
 * @param armed whether the ships are equipped with weapons or not
 */
export default function(
  destination: string,
  numberOfShips: number,
  armed: boolean
) {
  console.log(
    `You sent ${numberOfShips} ${armed ? "armed" : ""} ships to ${destination}.`
  );
}

Generate the CLI

bash
$ npx typescript-to-cli ./send-ships.ts
send-ships.js CLI has been generated.

tsconfig.json resolution

If typescript-to-cli detects a tsconfig.json in the current folder, it will use it to generate the CLI. If you want to use a custom tsconfig.json, you can use the command line option --project (or just -p) that specifies the path of a directory containing a tsconfig.json file, or a path to a valid .json file containing the configurations.

bash
$ npx typescript-to-cli ./send-ships.ts --project ./path-to-config/tsconfig.json
send-ships.js CLI has been generated.

Execute the CLI

bash
$ ./send-ships.js --destination Mars --number-of-ships 5 --armed
You sent 5 armed ships to Mars.

Or with the = symbol between options and values

bash
$ ./send-ships.js --destination=Europa --number-of-ships=2
You sent 2 ships to Europa.

Examples

Missing option

bash
$ ./send-ships.js --number-of-ships 5 --armed
Missing option --destination

Invalid option type

bash
$ ./send-ships.js --destination Mars --number-of-ships X --armed
--number-of-ships should be a number

Non required option

typescript
export default function(option: string | undefined) {}

or

typescript
export default function(option?: string) {}

Restrict option with string literal types

typescript
// cli.ts

export default function(primaryColor: "cyan" | "magenta" | "yellow") {}
bash
$ npx typescript-to-cli ./cli.ts
cli.js CLI has been generated.

$ ./cli.js --primary-color green
--primary-color does not accept the value "green". Allowed values: cyan, magenta, yellow

Display help

bash
$ ./send-ships.js --help
Usage send-ships.js [options]

Send ships to the specified destination

Options:

--help                     output usage information
--destination      the ships destination
--number-of-ship   the number of ships to send
--armed                    whether the ships are equipped with weapons or not

Generate the CLI from a js module and JSDoc type annotations

typescript-to-cli can also infer the parameters types from JSDoc annotations. However, your tsconfig.json should allow js files with "allowJs": true and an outDir to avoid overriding the input file. Let's take the same previous example but with JSDoc type annotations instead.

send-ships.js

javascript
/**
 * Send ships to the specified destination
 *
 * @param {string} destination the ships target
 * @param {number} numberOfShips the number of ships to send
 * @param {boolean} armed whether the ships are equipped with weapons or not
 */
export default function(destination, numberOfShips, armed) {
  console.log(
    `You sent ${numberOfShips} ${armed ? "armed" : ""} ships to ${destination}.`
  );
}

tsconfig.json

json
{
  "compilerOptions": {
    "allowJs": true,
    "outDir": "dist"
  }
}
bash
$ npx typescript-to-cli ./send-ships.js
./dist/send-ships.js CLI has been generated.

Limitations

  • Supports only boolean, number, string, string literals
  • No option shorcut

Issues· 1 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptargument-parserclitypescript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具