Giter Club home page Giter Club logo

clop's Introduction

Command Line Option Parser

Provides a very light weight command line tokeniser and a BYO (Bring Your Own) argument parser.

The name is a play on CLAP, the Command Line Argument Parser. This library is an attempt at a polar opposite, a minimally featured library to parse command lines.

Examples

This library provides a very simple command line tokenizer and a state enum indicating the current parser state. With these you can easily create your own argument parser:

Try this example out: cargo run --example readme.

extern crate clop;

fn main() {
	// Holds the current parser state
	let mut state = clop::State::Command;

	// When parsing options with values as a separate argument,
	// the value needs to know what option it is associated with.
	let mut opt_name = String::new();

	for token in std::env::args() {
		// Expecting to start with the command
		if let clop::State::Command = state {
			// After the command we're expecting a number of options
			state = clop::State::Options;
			continue;
		}
		else if let clop::State::Options = state {
			if token == "-o" {
				println!("Got the -o option");
				continue;
			}
			else if token == "--long-arg" {
				opt_name = token;
				state = clop::State::Value;
				continue;
			}
			else if token == "--" {
				println!("No more options");
				state = clop::State::Fixed(0);
				continue;
			}
			else if token.starts_with("-") {
				println!("Unknown option: {}", token);
				continue;
			}
			// Next expecting a fixed argument, target
			state = clop::State::Fixed(0);
			// Fallthrough is on purpose!
			// The current iteration `token` holds the fixed argument
		}
		else if let clop::State::Value = state {
			if opt_name == "--long-arg" {
				println!("Got value for {}: {}", opt_name, token);
				state = clop::State::Options;
				continue;
			}
			else {
				unreachable!()
			}
		}
		if let clop::State::Fixed(_) = state {
			println!("Got the fixed argument target: {}", token);
			state = clop::State::Args;
			continue;
		}
		if let clop::State::Args = state {
			println!("Got a variable argument: {}", token);
			continue;
		}
	}
}

License

Licensed under MIT License, see license.txt.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions.

clop's People

Contributors

casualx avatar

Watchers

James Cloos avatar  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.