mirror of
https://github.com/Noratrieb/badargs.git
synced 2026-01-15 20:25:05 +01:00
arg parsing works
This commit is contained in:
parent
2ec1f142a9
commit
92628dfbbe
3 changed files with 123 additions and 43 deletions
|
|
@ -106,8 +106,10 @@ mod error {
|
||||||
pub enum CallError {
|
pub enum CallError {
|
||||||
SingleMinus,
|
SingleMinus,
|
||||||
ShortFlagNotFound(char),
|
ShortFlagNotFound(char),
|
||||||
|
LongFlagNotFound(String),
|
||||||
ExpectedValue(String),
|
ExpectedValue(String),
|
||||||
INan(String),
|
INan(String),
|
||||||
UNan(String),
|
UNan(String),
|
||||||
|
CombinedShortWithValue(String),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
163
src/parse.rs
163
src/parse.rs
|
|
@ -2,25 +2,24 @@ use crate::error::CallError;
|
||||||
use crate::schema::{Schema, SchemaKind};
|
use crate::schema::{Schema, SchemaKind};
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::iter::Peekable;
|
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, CallError>;
|
type Result<T> = std::result::Result<T, CallError>;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct CliArgs {
|
pub(crate) struct CliArgs {
|
||||||
args: HashMap<&'static str, Box<dyn Any>>,
|
args: HashMap<&'static str, Box<dyn Any>>,
|
||||||
unnamed: Vec<String>,
|
unnamed: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CliArgs {
|
impl CliArgs {
|
||||||
pub fn from_args(schema: &Schema, args: impl Iterator<Item = String>) -> Result<Self> {
|
pub fn from_args(schema: &Schema, mut args: impl Iterator<Item = String>) -> Result<Self> {
|
||||||
let mut result = Self::default();
|
let mut result = Self::default();
|
||||||
|
|
||||||
let mut args = args.peekable();
|
|
||||||
while let Some(arg) = args.next() {
|
while let Some(arg) = args.next() {
|
||||||
if let Some(shorts) = arg.strip_prefix('-') {
|
if let Some(long) = arg.strip_prefix("--") {
|
||||||
|
parse_long(schema, &mut result, long, &mut args)?;
|
||||||
|
} else if let Some(shorts) = arg.strip_prefix('-') {
|
||||||
parse_shorts(schema, &mut result, shorts, &mut args)?;
|
parse_shorts(schema, &mut result, shorts, &mut args)?;
|
||||||
} else if let Some(_longs) = arg.strip_prefix("--") {
|
|
||||||
} else {
|
} else {
|
||||||
result.unnamed.push(arg);
|
result.unnamed.push(arg);
|
||||||
}
|
}
|
||||||
|
|
@ -30,7 +29,6 @@ impl CliArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a value from the map, expecting it to have type T
|
/// Get a value from the map, expecting it to have type T
|
||||||
/// Important: T should never be Option, making thisfh sjfhsekld fjkdsaljföoilkaesdf jikasoeldöojfliköesdafjisdolkyafj idrs
|
|
||||||
pub fn get<T: Any>(&self, long: &str) -> Option<&T> {
|
pub fn get<T: Any>(&self, long: &str) -> Option<&T> {
|
||||||
let any = self.args.get(long)?;
|
let any = self.args.get(long)?;
|
||||||
any.downcast_ref()
|
any.downcast_ref()
|
||||||
|
|
@ -49,55 +47,88 @@ fn parse_shorts(
|
||||||
schema: &Schema,
|
schema: &Schema,
|
||||||
results: &mut CliArgs,
|
results: &mut CliArgs,
|
||||||
shorts: &str,
|
shorts: &str,
|
||||||
args: &mut Peekable<impl Iterator<Item = String>>,
|
args: &mut impl Iterator<Item = String>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// there are kinds of short arguments
|
// there are kinds of short arguments
|
||||||
// single shorts that takes values: `-o main`
|
// single shorts that takes values: `-o main`
|
||||||
// multiple flags combined: `-xzf`
|
// multiple flags combined: `-xzf`
|
||||||
// combining these is invalid: `-xo main`
|
// combining these is invalid: `-xo main`
|
||||||
|
|
||||||
let mut chars = shorts.chars();
|
let mut all_shorts = shorts.chars();
|
||||||
|
|
||||||
let first_flag = chars.next();
|
let first_flag = all_shorts.next();
|
||||||
|
|
||||||
if let Some(flag) = first_flag {
|
if let Some(flag) = first_flag {
|
||||||
let command = schema
|
let command = schema
|
||||||
.short(flag)
|
.short(flag)
|
||||||
.ok_or_else(|| CallError::ShortFlagNotFound(flag))?;
|
.ok_or_else(|| CallError::ShortFlagNotFound(flag))?;
|
||||||
|
|
||||||
match command.kind {
|
parse_value(command.kind, results, &command.long, args)?;
|
||||||
SchemaKind::String => {
|
|
||||||
let string = args
|
|
||||||
.next()
|
|
||||||
.ok_or_else(|| CallError::ExpectedValue(command.long.to_string()))?;
|
|
||||||
results.insert(command.long, Box::new(string));
|
|
||||||
}
|
|
||||||
SchemaKind::INum => {
|
|
||||||
let integer = args
|
|
||||||
.next()
|
|
||||||
.ok_or_else(|| CallError::ExpectedValue(command.long.to_string()))?
|
|
||||||
.parse::<isize>()
|
|
||||||
.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::<usize>()
|
|
||||||
.map_err(|_| CallError::UNan(command.long.to_string()))?;
|
|
||||||
results.insert(command.long, Box::new(integer))
|
|
||||||
}
|
|
||||||
SchemaKind::Bool => {
|
|
||||||
results.insert(command.long, Box::new(true));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return Err(CallError::SingleMinus);
|
return Err(CallError::SingleMinus);
|
||||||
}
|
}
|
||||||
|
|
||||||
for _flag_name in chars {}
|
for flag in all_shorts {
|
||||||
|
let command = schema
|
||||||
|
.short(flag)
|
||||||
|
.ok_or_else(|| CallError::ShortFlagNotFound(flag))?;
|
||||||
|
|
||||||
|
if let SchemaKind::Bool = command.kind {
|
||||||
|
results.insert(&command.long, Box::new(true));
|
||||||
|
} else {
|
||||||
|
return Err(CallError::CombinedShortWithValue(command.long.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_long(
|
||||||
|
schema: &Schema,
|
||||||
|
results: &mut CliArgs,
|
||||||
|
long: &str,
|
||||||
|
args: &mut impl Iterator<Item = String>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let command = schema
|
||||||
|
.long(long)
|
||||||
|
.ok_or_else(|| CallError::LongFlagNotFound(long.to_string()))?;
|
||||||
|
|
||||||
|
parse_value(command.kind, results, &command.long, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_value(
|
||||||
|
kind: SchemaKind,
|
||||||
|
results: &mut CliArgs,
|
||||||
|
long: &'static str,
|
||||||
|
args: &mut impl Iterator<Item = String>,
|
||||||
|
) -> Result<()> {
|
||||||
|
match kind {
|
||||||
|
SchemaKind::String => {
|
||||||
|
let string = args
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| CallError::ExpectedValue(long.to_string()))?;
|
||||||
|
results.insert(long, Box::new(string));
|
||||||
|
}
|
||||||
|
SchemaKind::INum => {
|
||||||
|
let integer = args
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| CallError::ExpectedValue(long.to_string()))?
|
||||||
|
.parse::<isize>()
|
||||||
|
.map_err(|_| CallError::INan(long.to_string()))?;
|
||||||
|
results.insert(long, Box::new(integer))
|
||||||
|
}
|
||||||
|
SchemaKind::UNum => {
|
||||||
|
let integer = args
|
||||||
|
.next()
|
||||||
|
.ok_or_else(|| CallError::ExpectedValue(long.to_string()))?
|
||||||
|
.parse::<usize>()
|
||||||
|
.map_err(|_| CallError::UNan(long.to_string()))?;
|
||||||
|
results.insert(long, Box::new(integer))
|
||||||
|
}
|
||||||
|
SchemaKind::Bool => {
|
||||||
|
results.insert(long, Box::new(true));
|
||||||
|
}
|
||||||
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,11 +140,20 @@ mod test {
|
||||||
|
|
||||||
arg!(OutFile: "output", 'o' -> String);
|
arg!(OutFile: "output", 'o' -> String);
|
||||||
arg!(Input: "input", 'i' -> String);
|
arg!(Input: "input", 'i' -> String);
|
||||||
arg!(Force: "force", 'f' -> bool);
|
|
||||||
arg!(SetUpstream: "set-upstream" -> String);
|
arg!(SetUpstream: "set-upstream" -> String);
|
||||||
|
|
||||||
|
arg!(Force: "force", 'f' -> bool);
|
||||||
|
arg!(Gentle: "gentle", 'g' -> bool);
|
||||||
|
|
||||||
|
arg!(OLevel: "olevel", 'l' -> usize);
|
||||||
|
arg!(Iq: "iq", 'q' -> isize);
|
||||||
|
|
||||||
fn schema() -> Schema {
|
fn schema() -> Schema {
|
||||||
Schema::create::<((OutFile, Input), (Force, SetUpstream))>().unwrap()
|
Schema::create::<(
|
||||||
|
(OutFile, (Input, (OLevel, Iq))),
|
||||||
|
((Force, Gentle), SetUpstream),
|
||||||
|
)>()
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_args(args: &str) -> Result<CliArgs> {
|
fn parse_args(args: &str) -> Result<CliArgs> {
|
||||||
|
|
@ -121,7 +161,6 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn single_short_flag() {
|
fn single_short_flag() {
|
||||||
let args = parse_args("-f").unwrap();
|
let args = parse_args("-f").unwrap();
|
||||||
assert_eq!(args.get::<bool>("force"), Some(&true))
|
assert_eq!(args.get::<bool>("force"), Some(&true))
|
||||||
|
|
@ -140,9 +179,49 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn arg_param_and_unnamed() {
|
fn short_arg_param_and_unnamed() {
|
||||||
let args = parse_args("-i stdin uwu").unwrap();
|
let args = parse_args("-i stdin uwu").unwrap();
|
||||||
assert_eq!(args.unnamed(), &["uwu"]);
|
assert_eq!(args.unnamed(), &["uwu"]);
|
||||||
assert_eq!(args.get::<String>("input"), Some(&"stdin".to_string()))
|
assert_eq!(args.get::<String>("input"), Some(&"stdin".to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn short_numbers() {
|
||||||
|
let args = parse_args("-q -5423 -l 235235").unwrap();
|
||||||
|
assert_eq!(args.get::<isize>("iq"), Some(&-5423));
|
||||||
|
assert_eq!(args.get::<usize>("olevel"), Some(&235235));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn combined_shorts() {
|
||||||
|
let args = parse_args("-gf").unwrap();
|
||||||
|
assert_eq!(args.get::<bool>("gentle"), Some(&true));
|
||||||
|
assert_eq!(args.get::<bool>("force"), Some(&true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn long_flags() {
|
||||||
|
let args = parse_args("--force --gentle").unwrap();
|
||||||
|
assert_eq!(args.get::<bool>("gentle"), Some(&true));
|
||||||
|
assert_eq!(args.get::<bool>("force"), Some(&true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn long_params() {
|
||||||
|
let args = parse_args("--output main.c --iq 75").unwrap();
|
||||||
|
assert_eq!(args.get::<isize>("iq"), Some(&75));
|
||||||
|
assert_eq!(args.get::<String>("output"), Some(&"main.c".to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn many() {
|
||||||
|
let args = parse_args("--output main.c --iq 75 hallo -fg random").unwrap();
|
||||||
|
assert_eq!(args.unnamed(), &["hallo", "random"]);
|
||||||
|
assert_eq!(args.get::<bool>("gentle"), Some(&true));
|
||||||
|
assert_eq!(args.get::<bool>("force"), Some(&true));
|
||||||
|
assert_eq!(args.get::<String>("output"), Some(&"main.c".to_string()));
|
||||||
|
assert_eq!(args.get::<isize>("iq"), Some(&75));
|
||||||
|
assert_eq!(args.get::<usize>("olevel"), None);
|
||||||
|
assert_eq!(args.get::<String>("input"), None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@
|
||||||
//! This makes the interface of this crate fully type-safe! (and kind of cursed)
|
//! This makes the interface of this crate fully type-safe! (and kind of cursed)
|
||||||
|
|
||||||
use super::Result;
|
use super::Result;
|
||||||
use crate::error::CallError;
|
|
||||||
use crate::{CliArg, CliReturnValue, SchemaError};
|
use crate::{CliArg, CliReturnValue, SchemaError};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue