Clishé
Clishé is a cli mini-framework in rust. Write your CLI using an idiom focused on the following values:
- Reduction of boilerplate for repetitive command declaration
- Command implementation and definition live side-to-side
- Control-flow skeleton around args, mutable state (context), return type
- We should be able to generate shells on-demand based on a cli
- Auto-complete is a first-class citizen, in and out of the inner shell [wip]
When I work on my rust ecosystem, I sometimes find it necessary to "try out a piece" by calling some specific endpoint or library API. It should be trivial to create small, re-usable clis for these purposes. This is what clishé aims to achieve by being opinionated and limiting scaffolding.
Clishé is a surprisingly thin wrapper around the following technologies:
anyhow
for error handlingclap
for cli buildingrustyline
/shellwords
for shell generation
Those libraries contribute most of its power to this framework, which is not much more than a collection of technologies, a thin DSL and an opinion.
Example
This is a simple app with dummy commands to display the spirit of clishe.
extern crate clap;
extern crate clishe;
use *;
// Could also be called Database, State, ... depending on the domain of your
// CLI. This is the single object available in commands aside from
// arguments/options. The context has two likely lifetimes:
//
// - Created right before, handed to this command and dies with this command
// - Created at the beginning of the shell, passed from one command to another
;
// Could be anything. This turns the cli app into a function(Context, args from
// user) = Returned.
//
// This type offers us two approaches for our CLI apps: procedural and
// functional in nature. In the first one, one would apply side-effects inside
// of the application tree. On the other hand, one could aggregate functionally
// all side-effects to the Returned type and execute them at the scope of the
// main.
;
// Dispatchers are commands which hold sub-commands. The root of a cli-like
// application is often a dispatcher. The application then takes the shape of a
// tree of dispatcher nodes, with commands!{} implementations as leaves.
dispatchers!
This code will provide you with the following program:
$ cargo run --example complete
clishe
USAGE:
complete <SUBCOMMAND>
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
SUBCOMMANDS:
help Prints this message or the help of the given subcommand(s)
meat Aimez la viande, mangez-en mieux
shell The subcommands of this command in a shell
veggies Welcome to the Jungle
You can also invoke the shell and "enter" the cli:
$ cargo run --example complete shell
> veggies lettuce friend
Welcome to the table, friend
> _