Improve CLI argument handling for subcommand use

This commit is contained in:
timvisee 2018-03-08 19:10:24 +01:00
parent 188b0cb2e1
commit fb7fd69400
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
7 changed files with 110 additions and 45 deletions

35
src/cmd/handler.rs Normal file
View file

@ -0,0 +1,35 @@
use super::clap::{App, ArgMatches};
use app::*;
use super::cmd_upload::CmdUpload;
/// CLI argument handler.
pub struct Handler<'a> {
/// The CLI matches.
matches: ArgMatches<'a>,
}
impl<'a: 'b, 'b> Handler<'a> {
/// Build the application CLI definition.
pub fn build() -> App<'a, 'b> {
App::new(APP_NAME)
.version(APP_VERSION)
.author(APP_AUTHOR)
.about(APP_ABOUT)
.subcommand(CmdUpload::build().display_order(1))
}
/// Parse CLI arguments.
pub fn parse() -> Handler<'a> {
// Build the application CLI definition, get the matches
Handler {
matches: Handler::build().get_matches(),
}
}
/// Get the upload sub command, if matched.
pub fn upload(&'a self) -> Option<CmdUpload<'a>> {
CmdUpload::parse(&self.matches)
}
}