All Projects → GuillaumeSalles → Typescript To Cli

GuillaumeSalles / Typescript To Cli

Licence: mit
Transform your typescript module into a CLI

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typescript To Cli

Yaap
Yet Another (Swift) Argument Parser
Stars: ✭ 124 (+22.77%)
Mutual labels:  cli, argument-parser
Lyra
A simple to use, composable, command line parser for C++ 11 and beyond
Stars: ✭ 238 (+135.64%)
Mutual labels:  cli, argument-parser
Sywac
🚫 🐭 Asynchronous, single package CLI framework for Node
Stars: ✭ 109 (+7.92%)
Mutual labels:  cli, argument-parser
Clikt
Multiplatform command line interface parsing for Kotlin
Stars: ✭ 1,658 (+1541.58%)
Mutual labels:  cli, argument-parser
Fire Hpp
Fire for C++: Create fully functional CLIs using function signatures
Stars: ✭ 395 (+291.09%)
Mutual labels:  cli, argument-parser
Argparse
Argparse for golang. Just because `flag` sucks
Stars: ✭ 294 (+191.09%)
Mutual labels:  cli, argument-parser
Deno Cliffy
Command line framework for deno 🦕 Including Commandline-Interfaces, Prompts, CLI-Table, Arguments Parser and more...
Stars: ✭ 149 (+47.52%)
Mutual labels:  cli, argument-parser
Clipp
easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation
Stars: ✭ 687 (+580.2%)
Mutual labels:  cli, argument-parser
Caporal.js
A full-featured framework for building command line applications (cli) with node.js
Stars: ✭ 3,279 (+3146.53%)
Mutual labels:  cli, argument-parser
Getopt Php
A PHP library for command-line argument processing
Stars: ✭ 305 (+201.98%)
Mutual labels:  cli, argument-parser
Clize
CLIze: Turn Python functions into command-line interfaces
Stars: ✭ 439 (+334.65%)
Mutual labels:  cli, argument-parser
Argh
Argh! A minimalist argument handler.
Stars: ✭ 752 (+644.55%)
Mutual labels:  cli, argument-parser
Transit Map
Generate a schematic map (“metro map”) for a given (transit) network graph using Mixed Integer Programming.
Stars: ✭ 98 (-2.97%)
Mutual labels:  cli
Snitch
Snitch is the tool that keeps your tests under surveillence.
Stars: ✭ 100 (-0.99%)
Mutual labels:  cli
Tooling
Advancing Node.js as a framework for writing great tools
Stars: ✭ 98 (-2.97%)
Mutual labels:  cli
Terminal layout
The project help you to quickly build layouts in terminal,cross-platform(一个跨平台的命令行ui布局工具)
Stars: ✭ 98 (-2.97%)
Mutual labels:  cli
Npm Quick Run
Quickly run NPM script by prefix without typing the full name
Stars: ✭ 97 (-3.96%)
Mutual labels:  cli
Shtab
↔️ Automagic shell tab completion for Python CLI applications
Stars: ✭ 99 (-1.98%)
Mutual labels:  cli
Awesome React Generator
No more clicking around to create files in your react project! Awesome React Generator is Command Line Tool that let's you scaffold your components without leaving your terminal.
Stars: ✭ 98 (-2.97%)
Mutual labels:  cli
Blockshell
🎉 Minimal Blockchain Learning CLI
Stars: ✭ 1,348 (+1234.65%)
Mutual labels:  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

⚠️

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.

⚠️

Usage

Let's take the following module as an example.

send-ships.ts

/**
 * 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

$ 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.

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

Execute the CLI

$ ./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

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

Examples

Missing option

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

Invalid option type

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

Non required option

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

or

export default function(option?: string) {}

Restrict option with string literal types

// cli.ts

export default function(primaryColor: "cyan" | "magenta" | "yellow") {}
$ 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

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

Send ships to the specified destination

Options:

--help                     output usage information
--destination <string>     the ships destination
--number-of-ship <number>  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

/**
 * 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

{
  "compilerOptions": {
    "allowJs": true,
    "outDir": "dist"
  }
}
$ 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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].