From a1af6a0fcfa4598fed22d4b8fd05fe369330c0e1 Mon Sep 17 00:00:00 2001 From: ksk001100 Date: Sun, 16 Feb 2020 00:34:03 +0900 Subject: [PATCH] v0.4.1 --- Cargo.toml | 3 ++- README.md | 2 +- example/src/main.rs | 2 +- src/context.rs | 12 ++++++++++-- src/flag.rs | 2 +- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bb2d32d..2aa10f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,10 @@ [package] name = "seahorse" -version = "0.4.0" +version = "0.4.1" authors = ["ksk001100 "] edition = "2018" keywords = ["cli"] +repository = "https://github.com/ksk001100/seahorse" readme = "README.md" license-file = "LICENSE" description = "A minimal CLI framework written in Rust" diff --git a/README.md b/README.md index a8d1a67..53fb050 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A minimal CLI framework written in Rust ```toml [dependencies] -seahorse = "0.4.0" +seahorse = "0.4.1" ``` ## Example diff --git a/example/src/main.rs b/example/src/main.rs index b0739db..c0faa91 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -1,5 +1,5 @@ use std::env; -use seahorse::{App, Action, Command, color, Flag, FlagType, Context}; +use seahorse::{App, Command, color, Flag, FlagType, Context}; fn main() { let args: Vec = env::args().collect(); diff --git a/src/context.rs b/src/context.rs index 6fd3a96..e533a5d 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,4 +1,4 @@ -use crate::{Flag, FlagValue}; +use crate::{Flag, FlagValue, FlagType}; pub struct Context { pub args: Vec, @@ -8,9 +8,17 @@ pub struct Context { impl Context { pub fn new(args: Vec, flags: Option>) -> Self { let mut v = Vec::new(); + let mut parsed_args = args.clone(); let flags_val = match flags { Some(flags) => { for flag in flags { + if parsed_args.contains(&format!("--{}", flag.name)) { + let index = parsed_args.iter().position(|arg| *arg == format!("--{}", flag.name)).unwrap(); + parsed_args.remove(index); + if flag.flag_type != FlagType::Bool { + parsed_args.remove(index); + } + } v.push((flag.name.to_string(), flag.value(args.clone()))) } Some(v) @@ -18,7 +26,7 @@ impl Context { None => None }; - Self { args, flags: flags_val } + Self { args: parsed_args, flags: flags_val } } fn option_flag_value(&self, name: &str) -> Option<&FlagValue> { diff --git a/src/flag.rs b/src/flag.rs index efd4e7f..1e78110 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -5,7 +5,7 @@ pub struct Flag { pub flag_type: FlagType, } -#[derive(Clone)] +#[derive(PartialOrd, PartialEq, Clone)] pub enum FlagType { Bool, String,