mirror of
https://github.com/Noratrieb/badargs.git
synced 2026-01-14 19:55:08 +01:00
started parsing
This commit is contained in:
parent
148574ed6e
commit
cb734a708d
5 changed files with 86 additions and 63 deletions
|
|
@ -1,13 +1,13 @@
|
||||||
use badargs::arg;
|
use badargs::arg;
|
||||||
|
|
||||||
arg!(OutFile: "output", "o" -> Option<String>);
|
arg!(OutFile: "output", 'o' -> Option<String>);
|
||||||
arg!(Force: "force", "f" -> bool);
|
arg!(Force: "force", 'f' -> bool);
|
||||||
arg!(OLevel: "optimize" -> usize);
|
arg!(OLevel: "optimize" -> usize);
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = badargs::badargs::<(OutFile, (Force, OLevel))>().unwrap();
|
let args = badargs::badargs::<(OutFile, (Force, OLevel))>().unwrap();
|
||||||
|
|
||||||
let _outfile = args.get::<OutFile>();
|
let outfile = args.get::<OutFile>();
|
||||||
let _force = args.get::<Force>();
|
let force = args.get::<Force>();
|
||||||
let _o_level = args.get::<OLevel>();
|
let o_level = args.get::<OLevel>();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
37
src/lib.rs
37
src/lib.rs
|
|
@ -1,4 +1,5 @@
|
||||||
mod macros;
|
mod macros;
|
||||||
|
mod parse;
|
||||||
mod schema;
|
mod schema;
|
||||||
|
|
||||||
use crate::parse::CliArgs;
|
use crate::parse::CliArgs;
|
||||||
|
|
@ -17,7 +18,7 @@ where
|
||||||
{
|
{
|
||||||
let arg_schema = Schema::create::<S>()?;
|
let arg_schema = Schema::create::<S>()?;
|
||||||
|
|
||||||
let args = CliArgs::from_args(arg_schema, std::env::args_os())?;
|
let args = CliArgs::from_args(&arg_schema, std::env::args())?;
|
||||||
|
|
||||||
Ok(BadArgs { args })
|
Ok(BadArgs { args })
|
||||||
}
|
}
|
||||||
|
|
@ -29,14 +30,14 @@ where
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use badargs::arg;
|
/// # use badargs::arg;
|
||||||
/// arg!(OutFile: "output", "o" -> Option<String>);
|
/// arg!(OutFile: "output", 'o' -> Option<String>);
|
||||||
/// // OutFile now implements CliArg
|
/// // OutFile now implements CliArg
|
||||||
/// ```
|
/// ```
|
||||||
pub trait CliArg {
|
pub trait CliArg {
|
||||||
type Content: CliReturnValue;
|
type Content: CliReturnValue;
|
||||||
|
|
||||||
fn long() -> &'static str;
|
fn long() -> &'static str;
|
||||||
fn short() -> Option<&'static str>;
|
fn short() -> Option<char>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The struct containing parsed argument information
|
/// The struct containing parsed argument information
|
||||||
|
|
@ -92,34 +93,10 @@ mod error {
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum ArgError {
|
pub enum ArgError {
|
||||||
InvalidUtf8,
|
InvalidUtf8,
|
||||||
NameAlreadyExists(&'static str),
|
NameAlreadyExists(String),
|
||||||
InvalidSchema(String),
|
InvalidSchema(String),
|
||||||
IdkYet,
|
IdkYet,
|
||||||
}
|
UnnamedArgument,
|
||||||
}
|
SingleMinus,
|
||||||
|
|
||||||
mod parse {
|
|
||||||
use super::Result;
|
|
||||||
use crate::schema::Schema;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::ffi::OsString;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
|
||||||
pub struct CliArgs {
|
|
||||||
pub isize: HashMap<&'static str, isize>,
|
|
||||||
pub usize: HashMap<&'static str, isize>,
|
|
||||||
pub string: HashMap<&'static str, String>,
|
|
||||||
pub option_string: HashMap<&'static str, Option<String>>,
|
|
||||||
pub bool: HashMap<&'static str, bool>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CliArgs {
|
|
||||||
pub fn from_args(_schema: Schema, args: impl Iterator<Item = OsString>) -> Result<Self> {
|
|
||||||
let result = Self::default();
|
|
||||||
let mut args = args;
|
|
||||||
while let Some(_arg) = args.next() {}
|
|
||||||
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ macro_rules! arg {
|
||||||
$long
|
$long
|
||||||
}
|
}
|
||||||
|
|
||||||
fn short() -> Option<&'static str> {
|
fn short() -> Option<char> {
|
||||||
$short
|
$short
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
52
src/parse.rs
Normal file
52
src/parse.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
use super::Result;
|
||||||
|
use crate::schema::Schema;
|
||||||
|
use crate::ArgError;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::iter::Peekable;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct CliArgs {
|
||||||
|
pub isize: HashMap<&'static str, isize>,
|
||||||
|
pub usize: HashMap<&'static str, isize>,
|
||||||
|
pub string: HashMap<&'static str, String>,
|
||||||
|
pub option_string: HashMap<&'static str, Option<String>>,
|
||||||
|
pub bool: HashMap<&'static str, bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CliArgs {
|
||||||
|
pub fn from_args(schema: &Schema, args: impl Iterator<Item = String>) -> Result<Self> {
|
||||||
|
let mut result = Self::default();
|
||||||
|
|
||||||
|
let mut args = args.peekable();
|
||||||
|
while let Some(arg) = args.next() {
|
||||||
|
if let Some(shorts) = arg.strip_prefix('-') {
|
||||||
|
parse_shorts(schema, &mut result, shorts, &mut args)?;
|
||||||
|
} else if let Some(longs) = arg.strip_prefix("--") {
|
||||||
|
} else {
|
||||||
|
return Err(ArgError::UnnamedArgument);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_shorts(
|
||||||
|
schema: &Schema,
|
||||||
|
results: &mut CliArgs,
|
||||||
|
shorts: &str,
|
||||||
|
args: &mut Peekable<impl Iterator<Item = String>>,
|
||||||
|
) -> Result<()> {
|
||||||
|
if shorts.len() == 0 {
|
||||||
|
return Err(ArgError::SingleMinus);
|
||||||
|
}
|
||||||
|
|
||||||
|
for flag_name in shorts.chars() {}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expects_value_short(schema: &Schema, name: char) -> bool {
|
||||||
|
schema.short('5');
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
@ -32,7 +32,7 @@ pub struct SchemaCommand {
|
||||||
#[derive(Debug, Clone, Default, Eq, PartialEq)]
|
#[derive(Debug, Clone, Default, Eq, PartialEq)]
|
||||||
pub struct Schema {
|
pub struct Schema {
|
||||||
longs: HashMap<&'static str, SchemaCommand>,
|
longs: HashMap<&'static str, SchemaCommand>,
|
||||||
shorts: HashMap<&'static str, SchemaCommand>,
|
shorts: HashMap<char, SchemaCommand>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Schema {
|
impl Schema {
|
||||||
|
|
@ -48,19 +48,23 @@ impl Schema {
|
||||||
|
|
||||||
fn add_command(&mut self, long_name: &'static str, command: SchemaCommand) -> Result<()> {
|
fn add_command(&mut self, long_name: &'static str, command: SchemaCommand) -> Result<()> {
|
||||||
if let Some(_) = self.longs.insert(long_name, command) {
|
if let Some(_) = self.longs.insert(long_name, command) {
|
||||||
Err(ArgError::NameAlreadyExists(long_name))
|
Err(ArgError::NameAlreadyExists(long_name.to_string()))
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_short_command(
|
pub fn short(&self, name: char) -> Option<&SchemaCommand> {
|
||||||
&mut self,
|
self.shorts.get(&name)
|
||||||
short_name: &'static str,
|
}
|
||||||
command: SchemaCommand,
|
|
||||||
) -> Result<()> {
|
pub fn long(&self, name: &str) -> Option<&SchemaCommand> {
|
||||||
|
self.longs.get(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_short_command(&mut self, short_name: char, command: SchemaCommand) -> Result<()> {
|
||||||
if let Some(_) = self.shorts.insert(short_name, command) {
|
if let Some(_) = self.shorts.insert(short_name, command) {
|
||||||
Err(ArgError::NameAlreadyExists(short_name))
|
Err(ArgError::NameAlreadyExists(short_name.to_string()))
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -112,10 +116,10 @@ mod test {
|
||||||
use crate::schema::{Schema, SchemaCommand, SchemaKind};
|
use crate::schema::{Schema, SchemaCommand, SchemaKind};
|
||||||
use crate::{arg, ArgError};
|
use crate::{arg, ArgError};
|
||||||
|
|
||||||
arg!(OutFile: "output", "o" -> Option<String>);
|
arg!(OutFile: "output", 'o' -> Option<String>);
|
||||||
arg!(Force: "force", "f" -> bool);
|
arg!(Force: "force", 'f' -> bool);
|
||||||
arg!(SetUpstream: "set-upstream" -> String);
|
arg!(SetUpstream: "set-upstream" -> String);
|
||||||
arg!(OutFile2: "output", "o" -> Option<String>);
|
arg!(OutFile2: "output", 'o' -> Option<String>);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn one_command_schema() {
|
fn one_command_schema() {
|
||||||
|
|
@ -124,9 +128,8 @@ mod test {
|
||||||
kind: SchemaKind::OptionString,
|
kind: SchemaKind::OptionString,
|
||||||
};
|
};
|
||||||
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
||||||
assert_eq!(schema.shorts.get("o"), Some(&out_file));
|
assert_eq!(schema.shorts.get(&'o'), Some(&out_file));
|
||||||
assert_eq!(schema.longs.get("o"), None);
|
assert_eq!(schema.longs.get("o"), None);
|
||||||
assert_eq!(schema.shorts.get("output"), None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -140,14 +143,12 @@ mod test {
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
||||||
assert_eq!(schema.shorts.get("o"), Some(&out_file));
|
assert_eq!(schema.shorts.get(&'o'), Some(&out_file));
|
||||||
assert_eq!(schema.longs.get("o"), None);
|
assert_eq!(schema.longs.get("o"), None);
|
||||||
assert_eq!(schema.shorts.get("output"), None);
|
|
||||||
|
|
||||||
assert_eq!(schema.longs.get("force"), Some(&force));
|
assert_eq!(schema.longs.get("force"), Some(&force));
|
||||||
assert_eq!(schema.shorts.get("f"), Some(&force));
|
assert_eq!(schema.shorts.get(&'f'), Some(&force));
|
||||||
assert_eq!(schema.longs.get("f"), None);
|
assert_eq!(schema.longs.get("f"), None);
|
||||||
assert_eq!(schema.shorts.get("force"), None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -164,26 +165,19 @@ mod test {
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
||||||
assert_eq!(schema.shorts.get("o"), Some(&out_file));
|
assert_eq!(schema.shorts.get(&'o'), Some(&out_file));
|
||||||
assert_eq!(schema.longs.get("o"), None);
|
assert_eq!(schema.longs.get("o"), None);
|
||||||
assert_eq!(schema.shorts.get("output"), None);
|
|
||||||
|
|
||||||
assert_eq!(schema.longs.get("force"), Some(&force));
|
assert_eq!(schema.longs.get("force"), Some(&force));
|
||||||
assert_eq!(schema.shorts.get("f"), Some(&force));
|
assert_eq!(schema.shorts.get(&'f'), Some(&force));
|
||||||
assert_eq!(schema.longs.get("f"), None);
|
assert_eq!(schema.longs.get("f"), None);
|
||||||
assert_eq!(schema.shorts.get("force"), None);
|
|
||||||
|
|
||||||
assert_eq!(schema.longs.get("set-upstream"), Some(&set_upstream));
|
assert_eq!(schema.longs.get("set-upstream"), Some(&set_upstream));
|
||||||
assert_eq!(schema.shorts.get("set-upstream"), None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn double_error() {
|
fn double_error() {
|
||||||
let schema = Schema::create::<(OutFile, OutFile2)>();
|
let schema = Schema::create::<(OutFile, OutFile2)>();
|
||||||
assert!(matches!(
|
assert!(schema.is_err());
|
||||||
schema,
|
|
||||||
// it doesn't matter which one gets reported first
|
|
||||||
Err(ArgError::NameAlreadyExists("output")) | Err(ArgError::NameAlreadyExists("o"))
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue