mirror of
https://github.com/Noratrieb/badargs.git
synced 2026-01-14 19:55:08 +01:00
template things
This commit is contained in:
parent
26d52a306f
commit
2535e6c046
2 changed files with 65 additions and 5 deletions
24
examples/compiler.rs
Normal file
24
examples/compiler.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use badargs::{CliArg, CliArgInfo, Template};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Default)]
|
||||
struct OutFile;
|
||||
|
||||
fn main() {
|
||||
let args = badargs::badargs(Template {
|
||||
options: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(
|
||||
Box::new(OutFile),
|
||||
CliArgInfo {
|
||||
name: "output".to_string(),
|
||||
allow_short: true,
|
||||
takes_value: true,
|
||||
},
|
||||
);
|
||||
map
|
||||
},
|
||||
});
|
||||
|
||||
let outfile = args.get::<OutFile>();
|
||||
}
|
||||
46
src/lib.rs
46
src/lib.rs
|
|
@ -1,7 +1,43 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn it_works() {
|
||||
assert_eq!(2 + 2, 4);
|
||||
use std::any::{Any, TypeId};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum CliOption {
|
||||
Flag(bool),
|
||||
Value(String),
|
||||
}
|
||||
|
||||
pub trait CliArg {
|
||||
type Content;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct Template {
|
||||
pub options: HashMap<Box<dyn Any>, CliArgInfo>,
|
||||
}
|
||||
|
||||
pub struct CliArgInfo {
|
||||
pub name: String,
|
||||
pub allow_short: bool,
|
||||
pub takes_value: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct BadArgs {
|
||||
options: HashMap<TypeId, CliArgInfo>,
|
||||
}
|
||||
|
||||
impl BadArgs {
|
||||
pub fn get<T: Default>(&self) -> Option<&CliArgInfo> {
|
||||
self.options.get(&T::type_id())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn badargs(template: Template) -> BadArgs {
|
||||
let options = template
|
||||
.options
|
||||
.into_iter()
|
||||
.map(|(key, value)| (key.type_id(), value))
|
||||
.collect();
|
||||
BadArgs { options }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue