Crate clier

source ·
Expand description

Command Line Argument Parser for Rust

Clier is a command line argument parser and command framework for rust.

Parser

To start a new cli projects run:

$ cargo new demo && cd demo
$ cargo add clier

Then define your CLI in src/main.rs:

use clier::Argv;
use clier::Clier;

fn main() {
  let args: Argv = Clier::parse().args;
  println!("{:#?}", args);
}

And try it out:

$ cargo run -- command subcommand --test=value --no-production --help --try-me=false
Argv {
    commands: [
        "command",
        "subcommand",
    ],
    flags: {
        "test": "value",
        "production": "false",
        "help": "true",
        "try-me": "false",
    },
}

Framework

To start a new cli app run:

$ cargo new demo-app && cd demo-app
$ cargo add clier

Then define your CLI in src/main.rs:

use clier::builder::{CmdArgs, RCommand};
use clier::error;
use clier::hooks::use_flags;
use clier::run::{ExitCode, Meta, Runnable};
use clier::Clier;

fn first_command_handler(args: CmdArgs) -> i32 {
  let flags = use_flags(&args);
  println!("{:?}", flags);
  0
}
fn main() -> Result<ExitCode, error::Error> {
  let clier = Clier::parse();

  let meta = Meta::new("clier-example-framework", "This is the description", "1.0.0");
  let first_command = RCommand::new("first-command", "Command description", first_command_handler)
    .usage("test")
    .flag("tes", None, "testing")
    .subcommand("name", "descriptin", None, |_| {
      /* Code goes here */
      0 /* <- Exit code */
    })
    .subcommand("andra", "descriptin", None, |_| {
      /* Code goes here */
      0
    });

  clier.meta(&meta).command(first_command).run()
}

Modules

  • Structs for easliy building entities describing the commands and flags.
  • Struct for interface/ui of app.
  • Error enum
  • Hooks for runtime of app, inspired by react.
  • macrosmacros
    Short hand for building commands and flags
  • Run

Macros

Structs

  • Typestate pattern: State for (CliMeta)
  • Example structure:
  • Clier is the main struct for the framework
  • Typestate pattern: State for (CliMeta)