Crate cmder

source · []
Expand description

A simple, lighweight crate to parse command line arguments. Inspired by its javascript equivalent, commander.js.

This crate is relatively similar in syntax to the said library and is easy to get started with. It presents a builder interface to work with and can easily be extended to suit your needs. The crate only offers a builder interface, no derive features, if you’re looking such features or something more powerful, you should probably check out clap.

Constructs used within the crate include:

  • Command, which is exactly what it sounds like
  • Program which is a command marked as the entrypoint
  • Flags and Options(flags that take arguments)

The following is a full-fleged example on crate usage:

use cmder::{Program, Event, Setting, Pattern, PredefinedThemes};

let mut program = Program::new();

program
    .author("vndaba")
    .description("An example CLI")
    .version("0.1.0")
    .bin_name("example");

// Subcommands
program
    .subcommand("demo")
    .argument("<value>", "Some required value")
    .alias("d")
    .option("-f", "Some flag")
    .option("-n --name <value>", "Some option")
    .description("A demo subcommand")
    .action(|matches|{dbg!(matches);});

// ...

// Event listeners
program.before_all(|cfg| {
    let p_ref = cfg.get_program();
    println!("This program was authored by: {}", p_ref.get_author())
});

// ...

// Program settings
program.set(Setting::ShowHelpOnAllErrors(true));
program.set(Setting::ChoosePredefinedTheme(PredefinedThemes::Colorful));
program.set(Setting::SetProgramPattern(Pattern::Standard));
program.set(Setting::OverrideAllDefaultListeners(true));

program.parse();

Macros

This macro eases the work of creating a new theme, instead of creating the theme struct yourself, you can use the macro to do so. The macro receives all the desired colors. Do note that the order in which you place your colors is important.

Structs

The gist of the crate. Create instances of the program struct to chain to them all available methods. Event the program created is itself a command.

Similar to the Command struct except commands created via the Program::new() method are marked as the root command and also contain the version flag automatically. Exists due to maintain some familiarity with earlier versions of the crate

A simple struct containing the color palette to be used by the formatter when writing to standard output. It contains various fields each referring to a color variant to be used to print a specific value. Each of these values can be mapped to values in the Designation struct which is simply a fancy way of referring to what role a string value is assigned to.

Enums

The set of available colors for the terminal foreground/background.

Contains a few predefined themes that can be set to the program