8 unstable releases (3 breaking)
1.0.0-beta-1 | May 18, 2022 |
---|---|
0.6.1 | May 24, 2022 |
0.5.2 | Apr 21, 2022 |
0.4.1 | Apr 17, 2022 |
0.1.0 |
|
#827 in Command-line interface
Used in 2 crates
79KB
2K
SLoC
A simple, lightweight and extensible command line argument parser for rust codebases.
This crate aims to provide you with an easy-to-use and extensible API without compromising the speed and performance that is expected of rust codebases. The syntax of the builder interface can be attributed to the javascript package commander-js
. To get started, create a new instance of the program and manipulate it as shown.
A basic example
The following is a basic example on how to use the crate. More in depth documentation can be found from docs.rs or the github repo.
let mut program = Program::new();
program
.version("0.1.0")
.description("An example CLI")
.author("Author's name");
program
.subcommand("test")
.argument("<app-name>", "Pass the name of the app to test")
.alias("t")
.description("A test command")
.option("-s --skip", "Skip checking/installing the dependencies")
.option("-p --priority", "The priority to use when testing apps")
.action(|matches| { dbg!(matches); });
// ...
program.parse();
Extending the functionality using event listeners
The default behaviour of the program can be easily extended or even overriden by use of event listeners. View the docs to see all possible events of the program.
//...
program.on(Event::OutputVersion, |config|{
let prog_ref = config.get_program();
println!("Current program version is: {}", prog_ref.get_version());
});
program.after_help(|config|{
let prog_ref = config.get_program();
println!("This program was authored by: {}", prog_ref.get_author());
});
program.before_all(|config|{
println!("An Aram Mojtabai banger!!!😂")
});
// ...
Configuring program settings
Modify the settings to control the behavior of the program. See the documentation for all possible configurable settings
// ...
program.set(Setting::ShowHelpOnAllErrors(true));
program.set(Setting::ChoosePredefinedTheme(PredefinedThemes::Colorful));
program.set(Setting::SetProgramPattern(Pattern::Legacy));
program.set(Setting::OverrideAllDefaultListeners(true));
// ...
Customizing themes and patterns
You can define your own color pallete to be used when printing to std_out. Also, you can control how the information is printed by use of patterns.
// ...
use Color::*;
program.set(Setting::DefineCustomTheme(construct_theme!(
Green, Magenta, Blue, Red, White
)));
// ...
Refer to docs.rs for full documentation on the crate. Also check out the repository on github for examples of crate usage here. If you found this crate useful, consider starring this repo.
Contributing
Whether you have an awesome color palette to create a new predefined theme, or you wish to add a new use-case to the examples/ directory, or you have a change that can improve the crate, all contributions are welcome and highly appreciated!
Dependencies
~0–6.5MB
~36K SLoC