Skip to content

Commit

Permalink
Closes #28 - Changed App name, version, about, author, and usage to a…
Browse files Browse the repository at this point in the history
…llow lifetimes other than 'static
  • Loading branch information
kbknapp committed Mar 24, 2015
1 parent aed37da commit 6286d4b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
60 changes: 30 additions & 30 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use args::{PosArg, PosBuilder};
/// # use clap::{App, Arg};
/// let myprog = App::new("myprog")
/// .author("Me, [email protected]")
/// .version("1.0.2")
/// .version("1.0.2")
/// .about("Explains in brief what the program does")
/// .arg(
/// Arg::new("in_file").index(1)
Expand All @@ -35,19 +35,19 @@ use args::{PosArg, PosBuilder};
///
/// // Your pogram logic starts here...
/// ```
pub struct App {
pub struct App<'a, 'v, 'ab, 'u> {
// The name displayed to the user when showing version and help/usage information
name: &'static str,
name: String,
// A string of author(s) if desired. Displayed when showing help/usage information
author: Option<&'static str>,
author: Option<&'a str>,
// The version displayed to the user
version: Option<&'static str>,
version: Option<&'v str>,
// A brief explaination of the program that gets displayed to the user when shown help/usage information
about: Option<&'static str>,
about: Option<&'ab str>,
flags: HashMap<&'static str, FlagBuilder>,
opts: HashMap<&'static str, OptBuilder>,
positionals_idx: BTreeMap<u8, PosBuilder>,
subcommands: HashMap<&'static str, Box<App>>,
subcommands: HashMap<String, Box<App<'a, 'v, 'ab, 'u>>>,
needs_long_help: bool,
needs_long_version: bool,
needs_short_help: bool,
Expand All @@ -58,12 +58,12 @@ pub struct App {
short_list: HashSet<char>,
long_list: HashSet<&'static str>,
blacklist: HashSet<&'static str>,
usage_str: Option<&'static str>,
usage_str: Option<&'u str>,
bin_name: Option<String>

}

impl App {
impl<'a, 'v, 'ab, 'u> App<'a, 'v, 'ab, 'u>{
/// Creates a new instance of an application requiring a name (such as the binary). Will be displayed
/// to the user when they print version or help and usage information.
///
Expand All @@ -74,9 +74,9 @@ impl App {
/// let prog = App::new("myprog")
/// # .get_matches();
/// ```
pub fn new(n: &'static str) -> App {
pub fn new<'n>(n: &'n str) -> App<'a, 'v, 'ab, 'u> {
App {
name: n,
name: n.to_string(),
author: None,
about: None,
version: None,
Expand Down Expand Up @@ -109,7 +109,7 @@ impl App {
/// .author("Kevin <[email protected]>")
/// # .get_matches();
/// ```
pub fn author(mut self, a: &'static str) -> App {
pub fn author(mut self, a: &'a str) -> App<'a, 'v, 'ab, 'u> {
self.author = Some(a);
self
}
Expand All @@ -124,7 +124,7 @@ impl App {
/// .about("Does really amazing things to great people")
/// # .get_matches();
/// ```
pub fn about(mut self, a: &'static str) -> App {
pub fn about(mut self, a: &'ab str) -> App<'a, 'v, 'ab, 'u > {
self.about = Some(a);
self
}
Expand All @@ -139,7 +139,7 @@ impl App {
/// .version("v0.1.24")
/// # .get_matches();
/// ```
pub fn version(mut self, v: &'static str) -> App {
pub fn version(mut self, v: &'v str) -> App<'a, 'v, 'ab, 'u> {
self.version = Some(v);
self
}
Expand All @@ -161,7 +161,7 @@ impl App {
/// .usage("myapp [-clDas] <some_file>")
/// # .get_matches();
/// ```
pub fn usage(mut self, u: &'static str) -> App {
pub fn usage(mut self, u: &'u str) -> App<'a, 'v, 'ab, 'u> {
self.usage_str = Some(u);
self
}
Expand All @@ -179,7 +179,7 @@ impl App {
/// )
/// # .get_matches();
/// ```
pub fn arg(mut self, a: Arg) -> App {
pub fn arg(mut self, a: Arg) -> App<'a, 'v, 'ab, 'u> {
if self.arg_list.contains(a.name) {
panic!("Argument name must be unique, \"{}\" is already in use", a.name);
} else {
Expand Down Expand Up @@ -288,7 +288,7 @@ impl App {
/// Arg::new("debug").short("d")])
/// # .get_matches();
/// ```
pub fn args(mut self, args: Vec<Arg>) -> App {
pub fn args(mut self, args: Vec<Arg>) -> App<'a, 'v, 'ab, 'u> {
for arg in args.into_iter() {
self = self.arg(arg);
}
Expand All @@ -313,9 +313,9 @@ impl App {
/// // Additional subcommand configuration goes here, such as arguments...
/// # .get_matches();
/// ```
pub fn subcommand(mut self, subcmd: App) -> App {
pub fn subcommand(mut self, subcmd: App<'a, 'v, 'ab, 'u>) -> App<'a, 'v, 'ab, 'u> {
if subcmd.name == "help" { self.needs_subcmd_help = false; }
self.subcommands.insert(subcmd.name, Box::new(subcmd));
self.subcommands.insert(subcmd.name.clone(), Box::new(subcmd));
self
}

Expand All @@ -332,7 +332,7 @@ impl App {
/// SubCommand::new("debug").about("Controls debug functionality")])
/// # .get_matches();
/// ```
pub fn subcommands(mut self, subcmds: Vec<App>) -> App {
pub fn subcommands(mut self, subcmds: Vec<App<'a, 'v, 'ab, 'u>>) -> App<'a, 'v, 'ab, 'u> {
for subcmd in subcmds.into_iter() {
self = self.subcommand(subcmd);
}
Expand All @@ -357,7 +357,7 @@ impl App {
let opts = ! self.opts.is_empty();
let subcmds = ! self.subcommands.is_empty();

print!("\t{} {} {} {} {}", if let Some(ref name) = self.bin_name { &name[..] } else { self.name },
print!("\t{} {} {} {} {}", if let Some(ref name) = self.bin_name { name } else { &self.name },
if flags {"[FLAGS]"} else {""},
if opts {
if req_opts.is_empty() { "[OPTIONS]" } else { &req_opts[..] }
Expand Down Expand Up @@ -449,7 +449,7 @@ impl App {
}

pub fn get_matches(mut self) -> ArgMatches {
let mut matches = ArgMatches::new(self.name);
let mut matches = ArgMatches::new();

let args = env::args().collect::<Vec<_>>();
let mut it = args.into_iter();
Expand All @@ -471,7 +471,7 @@ impl App {
self.create_help_and_version();

let mut pos_only = false;
let mut subcmd_name: Option<&'static str> = None;
let mut subcmd_name: Option<String> = None;
let mut needs_val_of: Option<&'static str> = None;
let mut pos_counter = 1;
while let Some(arg) = it.next() {
Expand Down Expand Up @@ -513,11 +513,11 @@ impl App {
needs_val_of = self.parse_short_arg(matches, &arg);
} else {
// Positional or Subcommand
if let Some(sca) = self.subcommands.get(arg_slice) {
if sca.name == "help" {
if self.subcommands.contains_key(&arg) {
if arg_slice == "help" {
self.print_help();
}
subcmd_name = Some(sca.name);
subcmd_name = Some(arg.clone());
break;
}

Expand Down Expand Up @@ -578,11 +578,11 @@ impl App {
self.validate_blacklist(&matches);

if let Some(sc_name) = subcmd_name {
if let Some(ref mut sc) = self.subcommands.get_mut(sc_name) {
let mut new_matches = ArgMatches::new(sc_name);
if let Some(ref mut sc) = self.subcommands.get_mut(&sc_name) {
let mut new_matches = ArgMatches::new();
sc.get_matches_from(&mut new_matches, it);
matches.subcommand = Some(Box::new(SubCommand{
name: sc_name,
name: sc.name.clone(),
matches: new_matches}));
}
}
Expand Down Expand Up @@ -612,7 +612,7 @@ impl App {
});
}
if self.needs_subcmd_help && !self.subcommands.is_empty() {
self.subcommands.insert("help", Box::new(App::new("help").about("Prints this message")));
self.subcommands.insert("help".to_string(), Box::new(App::new("help").about("Prints this message")));
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/args/argmatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use args::posarg::PosArg;
/// }
/// }
pub struct ArgMatches {
pub matches_of: &'static str,
// pub matches_of: &'static str,
pub flags: HashMap<&'static str, FlagArg>,
pub opts: HashMap<&'static str, OptArg>,
pub positionals: HashMap<&'static str, PosArg>,
Expand All @@ -77,9 +77,9 @@ impl ArgMatches {
/// # use clap::{App, Arg};
/// let matches = App::new("myprog").get_matches();
/// ```
pub fn new(name: &'static str) -> ArgMatches {
pub fn new() -> ArgMatches {
ArgMatches {
matches_of: name,
// matches_of: name,
flags: HashMap::new(),
opts: HashMap::new(),
positionals: HashMap::new(),
Expand Down Expand Up @@ -230,9 +230,9 @@ impl ArgMatches {
/// _ => {}, // Either no subcommand or one not tested for...
/// }
/// ```
pub fn subcommand_name(&self) -> Option<&'static str> {
pub fn subcommand_name(&self) -> Option<&str> {
if let Some( ref sc ) = self.subcommand {
return Some(sc.name);
return Some(&sc.name[..]);
}
None
}
Expand Down
4 changes: 2 additions & 2 deletions src/args/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use ArgMatches;
/// .index(1))
/// # ).get_matches();
pub struct SubCommand {
pub name: &'static str,
pub name: String,
pub matches: ArgMatches
}

Expand All @@ -37,7 +37,7 @@ impl SubCommand {
/// SubCommand::new("config")
/// # ).get_matches();
/// ```
pub fn new(name: &'static str) -> App {
pub fn new<'a, 'v, 'ab, 'u>(name: &'a str) -> App<'a, 'v, 'ab, 'u> {
App::new(name)
}
}

0 comments on commit 6286d4b

Please sign in to comment.