vecli/lib.rs
1//! A zero-dependency, minimal CLI framework that's genuinely readable, the tool you've been looking for.
2//!
3//! Do you want to make a minimal CLI app but don't want a billion dependencies or doing all by yourself?
4//! Then vecli is perfect for you! If not? You're at the wrong place.
5//!
6//! # Getting Started
7//!
8//! > [!IMPORTANT]
9//! > A more friendly guide is being built. This is the API reference documentation. Check them out at [the getting started guide](https://razkar-studio.github.io/vecli/).
10//!
11//! ## Installation
12//!
13//! To install `vecli` as a dependency, run:
14//!
15//! ```bash
16//! cargo add vecli
17//! ```
18//!
19//! which is the same as including it in your `Cargo.toml` file:
20//!
21//! ```toml
22//! [dependencies]
23//! vecli = "0.3"
24//! ```
25//!
26//! ## Building Your First App
27//!
28//! Let's build your first app with `vecli`. This will be a simple "hello world" app that prints "hello" when run.
29//! After adding `vecli` to your dependencies, we can start by making a new `App`.
30//!
31//! ```ignore
32//! use vecli::*;
33//!
34//! fn main() {
35//! App::new("my-app")
36//! .run();
37//! }
38//! ```
39//!
40//! When you run the app, you should see something like:
41//!
42//! ```ignore
43//! error: No command provided. Try 'my-app --help'.
44//! ```
45//!
46//! And when you run `--help` (or `cargo run -- --help`), you should see a usage message like this:
47//!
48//! ```ignore
49//! Usage: my-app <command> [options]
50//!
51//! No commands available. Add some using .add_command()!
52//! ```
53//!
54//! ## Configuring Your App
55//!
56//! There's a handful of configuration options available for your app to further customize and to make your app look professional, listed:
57//!
58//! * **`.name("My App")`**: The human readable name for your app
59//! * **`.description("the most awesome app ever")`**: The description for your app
60//! * **`.version("0.1.0")`**: The version of your app
61//! * `.print_help_if_no_args(true)`: Prints the help screen when no command is specified.
62//! * `.print_help_on_fail(true)`: Prints help when no command found.
63//!
64//! ## Adding Commands
65//!
66//! Now let's make the app print "Hello" when the user passes in `hello` as a command. We'll want this:
67//!
68//! ```bash
69//! cargo run hello # would print:
70//! # Hello!
71//! ```
72//!
73//! Vecli makes this awfully simple. Use `add_command()` that takes a `Command`, and you can construct them like this:
74//!
75//! ```ignore
76//! app.add_command(
77//! Command::new("hello" /* The command the user would pass */, function /* The function that handles the command */)
78//! )
79//! ```
80//!
81//! And our `hello()` function:
82//!
83//! ```ignore
84//! fn hello(_ctx: &CommandContext) {
85//! println!("Hello!");
86//! }
87//! ```
88//!
89//! ### Configuring Commands
90//!
91//! Like you can configure an App, you can also configure your command to make it professional.
92//!
93//! - **`.description("Prints hello and exit.")`**: The description of the command, what it does.
94//! - **`.usage("[none]")`**: The usage for the command, will print alongside `my-app hello`.
95//! - `.flag(Flag::new("silent"))`: Add a flag to the command, input is without the `--` prefix.
96//! - `.strict_flags(true)`: If toggled, unknown flags will abort the program.
97//!
98//! ## Adding Flags
99//!
100//! Adding flags like `--silent` and `--dry-run` is also (kind of) simplified. To add a flag to your command, use the
101//! previously mentioned `.flag()` method.
102//!
103//! To construct a flag, only the name is needed, but you can customize them with an alias and description:
104//! - **`.alias("s")`**: An alias for the flag, in this case, `-s` will resolve to `--silent`. **CAUTION: Aliases will always return boolean!**
105//! - **`.description("Not say hello.")`**: The description for the flag.
106//!
107//! ## CommandContext
108//!
109//! Saw that CommandContext class earlier? Good eye. Vecli gives the handler the context of the command, including:
110//!
111//! - `ctx.subcommand`: `hello` itself
112//! - `ctx.positionals`: A vector of everything that comes after `subcommand`.
113//! - `ctx.flags`: A `HashMap` of passed flags.
114//!
115//! It's completely up to the function to check and act based on what context was given by Vecli. Keep this in mind!
116//!
117//! ---
118//!
119//! And that's all you need to make a simple hello app using *vecli*! `--help` (even help for commands!) and `--version` is fully built-in to vecli.
120//!
121//! Here's all of them combined:
122//!
123//! # Examples
124//! ```
125//! use vecli::*;
126//!
127//! fn hello(ctx: &CommandContext) {
128//! if !ctx.flags.contains_key("silent") {
129//! println!("Hello!")
130//! }
131//! }
132//!
133//! fn main() {
134//! App::new("my-app")
135//! .name("My App")
136//! .description("a very informative description")
137//! .print_help_if_no_args(true)
138//! .print_help_on_fail(true)
139//! .version("0.0.1")
140//! .add_command(
141//! Command::new("hello", hello)
142//! .description("prints hello and exit.")
143//! .usage("[none]")
144//! .flag(Flag::new("silent").alias("s").description("Not say hello."))
145//! .strict_flags(true),
146//! )
147//! .run();
148//! }
149//! ```
150//!
151//! ---
152//!
153//! The stuff here may be outdated. For the most up-to-date user guide, check the [User Guide](https://razkar-studio.github.io/vecli).
154//!
155//! If you want a feature added, please submit an issue to the [GitHub repository](https://github.com/razkar-studio/vecli.git)!
156//! This project is still very experimental, so expect bugs. When you *do* find one, also submit an issue!
157//! Feel free to read the rest of the documentation if you are a developer.
158//!
159//! Cheers, RazkarStudio.
160
161mod app;
162mod commands;
163mod flags;
164
165mod terminal;
166mod utils;
167
168pub use app::App;
169pub use commands::Command;
170pub use commands::CommandContext;
171pub use flags::Flag;
172pub use flags::PassedFlags;
173
174pub use terminal::Choice;
175pub use terminal::Confirm;
176pub use terminal::Terminal;