Git Product home page Git Product logo

foropts-rs's Introduction

foropts-rs: iterator-style option parsing for Rust

Build Status Crates.io License: MIT License: Apache 2.0

Most argument parsing libraries, such as clap, treat the arguments as a multimap; foropts treats the arguments as a sequence. This usually isn’t what you want, but occasionally it is.

Usage

It's on crates.io, so you can add

[dependencies]
foropts = "0.3.6"

to your Cargo.toml and

extern crate foropts;

to your crate root.

This crate supports Rust version 1.22 and later.

Example

In this example, we accept one boolean flag, -v (or --verbose), and two string options, -b (or --before) and -a (or --after). The string options build a string, where the relative order of the appearances of -a and -b matters. This is hard to do when your arguments are treated as a multimap, but easy when you can iterate over them sequentially.

use foropts;

enum Opt {
    Before(String),
    After(String),
    Verbose,
}

let config =
    foropts::Config::new("build_string_example")
        .arg(foropts::Arg::parsed_param("BEFORE", Opt::Before)
             .short('b').long("before"))
        .arg(foropts::Arg::parsed_param("AFTER", Opt::After)
             .short('a').long("after"))
        .arg(foropts::Arg::flag(|| Opt::Verbose)
             .short('v').long("verbose"));

let mut verbose     = false;
let mut accumulator = String::new();

let opts = ["-b1", "-va", "2", "--after=3", "--before", "4"]
    .iter().map(ToString::to_string);

for opt in config.iter(opts) {
    match opt.unwrap_or_else(|e| config.exit_error(&e)) {
        Opt::Before(s) => accumulator = s + &accumulator,
        Opt::After(s)  => accumulator = accumulator + &s,
        Opt::Verbose   => verbose = true,
    }
}

assert_eq!( "4123", accumulator );
assert!( verbose );

foropts-rs's People

Contributors

tov avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.