README
This project is not production ready, do not use it for your projects
Decli
Decli is a set of tools that makes building CLI's simple and offers type safety.
Installtion
To get started you need to install the following package:
yarn add @bauke2112/decli
If you are using NPM:
npm i @bauke2112/decli
Remember to add those lines to your tsconfig.json
file:
{
"compilerOptions": {
"experimentalDecorators": true
}
}
Quick Start
This section explains how to get started with Decli and how to create your fisrt CLI. Remember that Decli uses decorators to help you build your program, but you don't need to be intimidated!
Basic Boilerplate
Let's start by creating an index.ts
file and importing the required decli dependencies:
import '@bauke2112/decli/metadata';
import { Command, build } from '@bauke2112/decli';
export class MyCLI {
}
const cli = build(MyCLI);
cli.parse(process.argv);
This is the basic Decli boilerplate. Every CLI must be a class, and its methods are the possible commands.
In line 7, we call the build
function, passing the class itself as a parameter. That will build our CLI from the collected metadata of the decorators. The name of the CLI will be the lowercase class name, in this example: mycli
.
In the last line, we pass to the parse method what the user typed in the command line.
Defining a command
After that we can create our first command:
import '@bauke2112/decli/metadata';
import { Command, build } from '@bauke2112/decli';
interface HelloCommandArgs {
name: string;
}
export class MyCLI {
@Command({
args: {
name: 'string',
},
flags: {}
})
public hello({ name }: HelloCommandArgs): void {
console.log(`Hello, ${name}!`);
}
}
const cli = build(MyCLI);
cli.parse(process.argv.slice(1));
That is a pretty simple command. It just says hi to a name that it receives. In line 5 we declare an interface for the command arguments that contains only a string called name.
Inside the class, we declare a method called hello
that receives an object of type HelloCommandArgs
. The decorator @Command
is where the magic happens. It recieves an schema for the command: we must declare its args
(positional parameters) and flags
(named parameters).
In the example above, we declare one argument and no flags. The argument has name hello
(the same in the interface) and has the type 'string'
. The allowed types are string
, number
or boolean
.
Running our command
In the development environment, we can run our CLI with the following command (note that if your are using typescript, you will need ts-node
installed):
yarn ts-node src/index.ts mycli hello Gustavo
And the output will be:
Hello, Gustavo!
For more information check the full documentation here.