-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
474f442
commit fc11e37
Showing
2 changed files
with
83 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
} |