#arguments-parser #command-line-arguments #enums #assisted

entrance

A command line argument parser library which provides type assisted tools

3 releases (breaking)

0.3.0 Nov 6, 2020
0.2.0 Sep 28, 2019
0.1.0 Jun 4, 2019

#882 in Rust patterns

Download history 15/week @ 2024-02-23 5/week @ 2024-03-01 40/week @ 2024-03-29 14/week @ 2024-04-05

54 downloads per month

MIT license

20KB
420 lines

entrance

Crates.io Document CircleCI

Type sytem assisted command line argument parser

entrance provides type assisted tools for parsing command line arguments.

Simple usage

use entrance::{Arguments, Options};
use std::env;
use std::path::PathBuf;

#[derive(Options, PartialEq)]
enum Opts {
    #[entrance(description = "Print help message")]
    #[entrance(short = 'h')]
    #[entrance(informative(entrance::help))]
    Help,

    #[entrance(description = "Print version infomation")]
    #[entrance(informative(entrance::version))]
    Version,

    #[entrance(description = "Use verbose output")]
    #[entrance(short = 'v')]
    Verbose,
}

#[derive(Arguments)]
struct Args {
    #[entrance(description = "The number of lines")]
    num: f64,

    #[entrance(description = "Path to a file")]
    file: PathBuf,
}

type Command = entrance::Command<Opts, Args>;

fn main() {
    let command = Command::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
    let (opts, args) = command.parse_or_exit(env::args());

    if opts.contains(&Opts::Verbose) {
        println!("enabled the verbose output");
    }

    println!("1st argument: \"{}\"", args.num);
    println!("2nd argument: \"{}\"", args.file.display());
}

Structs and traits

Command

This struct provides tools for parsing command line arguments.

Before parsing command line arguments, it is necessary to create the instance with the associated function new then, call parse of the instance.

Options

A derive macro is available for this.

Limitation: the derive macro supports only an Enum whose variants don't have any field.

Arguments

A derive macro is available for this.

Limitation: the macro supports only the struct with members implementing FromStr.

Dependencies

~330–790KB
~19K SLoC