From 2ec1f142a93cf3b7da3c71a8ab68f1b0869eef6d Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Fri, 1 Oct 2021 22:25:17 +0200 Subject: [PATCH] removed optionality it does not make sense --- examples/compiler.rs | 2 +- src/lib.rs | 7 +++---- src/macros.rs | 17 +++------------- src/parse.rs | 46 +++++++++++++++++++++++++++++++++++++++----- src/schema.rs | 15 ++++----------- 5 files changed, 52 insertions(+), 35 deletions(-) diff --git a/examples/compiler.rs b/examples/compiler.rs index 5d067f1..be32f5a 100644 --- a/examples/compiler.rs +++ b/examples/compiler.rs @@ -1,6 +1,6 @@ use badargs::arg; -arg!(OutFile: "output", 'o' -> String, required); +arg!(OutFile: "output", 'o' -> String); arg!(Force: "force", 'f' -> bool); arg!(OLevel: "optimize" -> usize); diff --git a/src/lib.rs b/src/lib.rs index 06e5daf..de7a5be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,8 +32,7 @@ where /// ``` /// # use badargs::arg; /// arg!(Force: "force", 'f' -> bool); -/// -/// arg!(OutFile: "output", 'o' -> String, required); +/// arg!(OutFile: "output", 'o' -> String); /// // OutFile now implements CliArg /// ``` // This trait requires any because some dynamic typing is done in the background @@ -42,7 +41,6 @@ pub trait CliArg: Any { fn long() -> &'static str; fn short() -> Option; - fn required() -> bool; } /// The struct containing parsed argument information @@ -107,8 +105,9 @@ mod error { #[derive(Debug, Clone, Eq, PartialEq)] pub enum CallError { SingleMinus, - UnnamedArgument, ShortFlagNotFound(char), ExpectedValue(String), + INan(String), + UNan(String), } } diff --git a/src/macros.rs b/src/macros.rs index 364fefb..e7383e1 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -2,19 +2,12 @@ macro_rules! arg { // implicit optional ($name:ident: $long:literal, $short:literal -> $result:ty) => { - arg!(@$name: ($long, ::std::option::Option::Some($short)) -> $result, false); + arg!(@$name: ($long, ::std::option::Option::Some($short)) -> $result); }; ($name:ident: $long:literal -> $result:ty) => { - arg!(@$name: ($long, ::std::option::Option::None) -> $result, false); + arg!(@$name: ($long, ::std::option::Option::None) -> $result); }; - // required - ($name:ident: $long:literal, $short:literal -> $result:ty, required) => { - arg!(@$name: ($long, ::std::option::Option::Some($short)) -> $result, true); - }; - ($name:ident: $long:literal -> $result:ty, required) => { - arg!(@$name: ($long, ::std::option::Option::None) -> $result, true); - }; - (@$name:ident: ($long:literal, $short:expr) -> $result:ty, $required:literal) => { + (@$name:ident: ($long:literal, $short:expr) -> $result:ty) => { #[derive(Default)] struct $name; @@ -28,10 +21,6 @@ macro_rules! arg { fn short() -> Option { $short } - - fn required() -> bool { - $required - } } }; } diff --git a/src/parse.rs b/src/parse.rs index d87e414..3bff4bc 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -9,6 +9,7 @@ type Result = std::result::Result; #[derive(Debug, Default)] pub struct CliArgs { args: HashMap<&'static str, Box>, + unnamed: Vec, } impl CliArgs { @@ -21,7 +22,7 @@ impl CliArgs { parse_shorts(schema, &mut result, shorts, &mut args)?; } else if let Some(_longs) = arg.strip_prefix("--") { } else { - return Err(CallError::UnnamedArgument); + result.unnamed.push(arg); } } @@ -35,6 +36,10 @@ impl CliArgs { any.downcast_ref() } + pub fn unnamed(&self) -> &[String] { + &self.unnamed + } + fn insert(&mut self, long: &'static str, value: Box) { self.args.insert(long, value); } @@ -62,12 +67,30 @@ fn parse_shorts( match command.kind { SchemaKind::String => { - let next = args + let string = args .next() .ok_or_else(|| CallError::ExpectedValue(command.long.to_string()))?; - results.insert(command.long, Box::new(next)); + results.insert(command.long, Box::new(string)); + } + SchemaKind::INum => { + let integer = args + .next() + .ok_or_else(|| CallError::ExpectedValue(command.long.to_string()))? + .parse::() + .map_err(|_| CallError::INan(command.long.to_string()))?; + results.insert(command.long, Box::new(integer)) + } + SchemaKind::UNum => { + let integer = args + .next() + .ok_or_else(|| CallError::ExpectedValue(command.long.to_string()))? + .parse::() + .map_err(|_| CallError::UNan(command.long.to_string()))?; + results.insert(command.long, Box::new(integer)) + } + SchemaKind::Bool => { + results.insert(command.long, Box::new(true)); } - _ => todo!(), } } else { return Err(CallError::SingleMinus); @@ -84,7 +107,7 @@ mod test { use crate::arg; use crate::schema::Schema; - arg!(OutFile: "output", 'o' -> Option); + arg!(OutFile: "output", 'o' -> String); arg!(Input: "input", 'i' -> String); arg!(Force: "force", 'f' -> bool); arg!(SetUpstream: "set-upstream" -> String); @@ -109,4 +132,17 @@ mod test { let args = parse_args("-i stdin").unwrap(); assert_eq!(args.get::("input"), Some(&"stdin".to_string())) } + + #[test] + fn two_unnamed() { + let args = parse_args("hallo welt").unwrap(); + assert_eq!(args.unnamed(), &["hallo", "welt"]); + } + + #[test] + fn arg_param_and_unnamed() { + let args = parse_args("-i stdin uwu").unwrap(); + assert_eq!(args.unnamed(), &["uwu"]); + assert_eq!(args.get::("input"), Some(&"stdin".to_string())) + } } diff --git a/src/schema.rs b/src/schema.rs index ff36728..5e730b0 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -4,6 +4,7 @@ //! This makes the interface of this crate fully type-safe! (and kind of cursed) use super::Result; +use crate::error::CallError; use crate::{CliArg, CliReturnValue, SchemaError}; use std::collections::HashMap; @@ -26,7 +27,6 @@ pub struct SchemaCommand { pub kind: SchemaKind, pub long: &'static str, pub short: Option, - pub required: bool, } /// @@ -107,7 +107,6 @@ where kind: T::Content::kind(), long: T::long(), short, - required: T::required(), }; if let Some(short) = short { schema.add_short_command(short, command)?; @@ -119,12 +118,12 @@ where #[cfg(test)] mod test { use crate::arg; - use crate::schema::{Schema, SchemaCommand, SchemaKind, SchemaKindType}; + use crate::schema::{Schema, SchemaCommand, SchemaKind}; - arg!(OutFile: "output", 'o' -> Option); + arg!(OutFile: "output", 'o' -> String); arg!(Force: "force", 'f' -> bool); arg!(SetUpstream: "set-upstream" -> String); - arg!(OutFile2: "output", 'o' -> Option); + arg!(OutFile2: "output", 'o' -> String); #[test] fn one_command_schema() { @@ -133,7 +132,6 @@ mod test { kind: SchemaKind::String, long: "output", short: Some('o'), - required: false, }; assert_eq!(schema.longs.get("output"), Some(&out_file)); assert_eq!(schema.shorts.get(&'o'), Some(&out_file)); @@ -147,13 +145,11 @@ mod test { kind: SchemaKind::String, long: "output", short: Some('o'), - required: false, }; let force = SchemaCommand { kind: SchemaKind::Bool, long: "force", short: Some('f'), - required: true, }; assert_eq!(schema.longs.get("output"), Some(&out_file)); @@ -172,19 +168,16 @@ mod test { kind: SchemaKind::String, long: "output", short: Some('o'), - required: false, }; let force = SchemaCommand { kind: SchemaKind::Bool, long: "force", short: Some('f'), - required: true, }; let set_upstream = SchemaCommand { kind: SchemaKind::String, long: "set-upstream", short: None, - required: true, }; assert_eq!(schema.longs.get("output"), Some(&out_file));