Skip to content

Commit

Permalink
fix all warnings (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts authored Jul 2, 2018
1 parent 474f442 commit fc11e37
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 97 deletions.
22 changes: 12 additions & 10 deletions examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
extern crate clap;
extern crate man;

use man::{Manual, ManualArg};
use clap::{App, AppSettings, Arg, SubCommand};
use man::Manual;

fn main() {
let mut a = App::new("testapp")
.about("Pointless application")
.setting(AppSettings::SubcommandRequiredElseHelp)
.author("Katharina Fey <[email protected]>")
// .author("Yosh Wuyts <[email protected]")
.long_about("Lorem Ipsum bla bla bla")
.arg(Arg::with_name("debug").short("d").help("Make program output debug messages"))
.arg(Arg::with_name("output").short("o").takes_value(true).help("Output File"))
.subcommand(SubCommand::with_name("foo").arg(Arg::with_name("bar").short("b").long("barr")));
let a = App::new("testapp")
.about("Pointless application")
.setting(AppSettings::SubcommandRequiredElseHelp)
.author("Katharina Fey <[email protected]>")
// .author("Yosh Wuyts <[email protected]")
.long_about("Lorem Ipsum bla bla bla")
.arg(Arg::with_name("debug").short("d").help("Make program output debug messages"))
.arg(Arg::with_name("output").short("o").takes_value(true).help("Output File"))
.subcommand(SubCommand::with_name("foo").arg(Arg::with_name("bar").short("b").long("barr")));

let manual = Manual::from_clap(&a);
println!("{:#?}", manual);
Expand Down
158 changes: 71 additions & 87 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,114 +5,98 @@

extern crate clap;

use clap::{App, AppSettings, Arg, ArgSettings, SubCommand};

fn main() {
let mut a = App::new("testapp")
.about("Pointless application")
.setting(AppSettings::SubcommandRequiredElseHelp)
.author("Katharina Fey <[email protected]>")
// .author("Yosh Wuyts <[email protected]")
.long_about("Lorem Ipsum bla bla bla")
.arg(Arg::with_name("debug").short("d").help("Make program output debug messages"))
.arg(Arg::with_name("output").short("o").takes_value(true).help("Output File"))
.subcommand(SubCommand::with_name("foo").arg(Arg::with_name("bar").short("b").long("barr")));

// println!("{:#?}", a);
let manual = Manual::from_clap(&a);
println!("{:#?}", manual);
}
use clap::{App, Arg, ArgSettings};

/// Describe an argument or option
#[derive(Debug)]
pub struct ManualArg {
short: Option<char>,
long: Option<String>,
descr: Option<String>,
short: Option<char>,
long: Option<String>,
descr: Option<String>,
}

#[derive(Debug)]
pub struct Manual {
/// Optionally children of this app
children: Vec<(String, Manual)>,
/// Application name
name: Option<String>,
/// Type description
description: Option<String>,
/// Type authors
authors: Option<String>,
/// Type flags
flags: Vec<ManualArg>,
/// Type options
options: Vec<ManualArg>,
/// Optionally children of this app
children: Vec<(String, Manual)>,
/// Application name
name: Option<String>,
/// Type description
description: Option<String>,
/// Type authors
authors: Option<String>,
/// Type flags
flags: Vec<ManualArg>,
/// Type options
options: Vec<ManualArg>,
}

impl<'a, 'b, 'c> From<&'c Arg<'a, 'b>> for ManualArg {
fn from(s: &'c Arg) -> Self {
ManualArg {
short: s.short,
long: match s.long {
Some(s) => Some(s.into()),
_ => None,
},
descr: match s.help {
Some(s) => Some(s.into()),
_ => None,
},
}
fn from(s: &'c Arg) -> Self {
ManualArg {
short: s.short,
long: match s.long {
Some(s) => Some(s.into()),
_ => None,
},
descr: match s.help {
Some(s) => Some(s.into()),
_ => None,
},
}
}
}

impl Manual {
fn new() -> Self {
Manual {
children: Vec::new(),
name: None,
description: None,
authors: None,
flags: Vec::new(),
options: Vec::new(),
}
fn new() -> Self {
Manual {
children: Vec::new(),
name: None,
description: None,
authors: None,
flags: Vec::new(),
options: Vec::new(),
}
}

// TODO: Make this less awful
fn add_empty_child(&mut self, name: &str) -> &mut Manual {
self.children.push((name.into(), Manual::new()));
let (_, ref mut manual) = self.children.last_mut().unwrap();
manual
}
// TODO: Make this less awful
fn add_empty_child(&mut self, name: &str) -> &mut Manual {
self.children.push((name.into(), Manual::new()));
let (_, ref mut manual) = self.children.last_mut().unwrap();
manual
}

fn recursive(manual: &mut Manual, app: &App) {
manual.name = app.name.clone().into();
manual.description = app.about.map(Into::into);
manual.authors = app.author.map(Into::into);
fn recursive(manual: &mut Manual, app: &App) {
manual.name = app.name.clone().into();
manual.description = app.about.map(Into::into);
manual.authors = app.author.map(Into::into);

let (flags, options): (Vec<ManualArg>, Vec<ManualArg>) = app.args.iter().fold(
(Vec::new(), Vec::new()),
|(mut f, mut o), i: &Arg| {
if i.is_set(ArgSettings::TakesValue) {
f.push(i.into());
} else {
o.push(i.into());
}
let (flags, options): (Vec<ManualArg>, Vec<ManualArg>) = app
.args
.iter()
.fold((Vec::new(), Vec::new()), |(mut f, mut o), i: &Arg| {
if i.is_set(ArgSettings::TakesValue) {
f.push(i.into());
} else {
o.push(i.into());
}

(f, o)
},
);
(f, o)
});

manual.flags = flags;
manual.options = options;
manual.flags = flags;
manual.options = options;

app.subcommands.iter().for_each(|app| {
let inner_name: String = app.name.clone();
let mut inner = manual.add_empty_child(&app.name);
Manual::recursive(&mut inner, app);
});
}
app.subcommands.iter().for_each(|app| {
let _inner_name: String = app.name.clone();
let mut inner = manual.add_empty_child(&app.name);
Manual::recursive(&mut inner, app);
});
}

pub fn from_clap<'a, 'b>(app: &App<'a, 'b>) -> Manual {
let mut man = Manual::new();
Manual::recursive(&mut man, app);
man
}
pub fn from_clap<'a, 'b>(app: &App<'a, 'b>) -> Manual {
let mut man = Manual::new();
Manual::recursive(&mut man, app);
man
}
}

0 comments on commit fc11e37

Please sign in to comment.