diff --git a/imag-tag/src/main.rs b/imag-tag/src/main.rs index d653686f..fdac5ac9 100644 --- a/imag-tag/src/main.rs +++ b/imag-tag/src/main.rs @@ -14,8 +14,10 @@ use std::process::exit; use libimagrt::runtime::Runtime; use libimagrt::setup::generate_runtime_setup; use libimagentrytag::tagable::Tagable; +use libimagentrytag::tag::Tag; use libimagstore::storeid::build_entry_path; use libimagerror::trace::trace_error; +use libimagentrytag::ui::{get_add_tags, get_remove_tags}; mod ui; @@ -32,9 +34,8 @@ fn main() { .subcommand_name() .map_or_else( || { - let add = rt.cli().values_of("add").map(|o| o.map(String::from)); - let rem = rt.cli().values_of("remove").map(|o| o.map(String::from)); - + let add = get_add_tags(rt.cli()); + let rem = get_remove_tags(rt.cli()); alter(&rt, id, add, rem); }, |name| { @@ -49,7 +50,7 @@ fn main() { }); } -fn alter>(rt: &Runtime, id: &str, add: Option, rem: Option) { +fn alter(rt: &Runtime, id: &str, add: Option>, rem: Option>) { let path = { match build_entry_path(rt.store(), id) { Err(e) => { diff --git a/imag-tag/src/ui.rs b/imag-tag/src/ui.rs index 74916879..32e3383c 100644 --- a/imag-tag/src/ui.rs +++ b/imag-tag/src/ui.rs @@ -1,5 +1,7 @@ use clap::{Arg, App, ArgGroup, SubCommand}; +use libimagentrytag::ui::{tag_add_arg, tag_remove_arg}; + pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { app.arg(Arg::with_name("id") .long("id") @@ -8,21 +10,8 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .required(true) .help("Use this entry")) - .arg(Arg::with_name("add") - .long("add") - .short("a") - .takes_value(true) - .required(false) - .multiple(true) - .help("Add this tag")) - - .arg(Arg::with_name("remove") - .long("remove") - .short("r") - .takes_value(true) - .required(false) - .multiple(true) - .help("Remove this tag")) + .arg(tag_add_arg()) + .arg(tag_remove_arg()) .subcommand(SubCommand::with_name("list") .about("List tags (default)") diff --git a/libimagentrytag/src/ui.rs b/libimagentrytag/src/ui.rs index ecef19d6..29cf8bc8 100644 --- a/libimagentrytag/src/ui.rs +++ b/libimagentrytag/src/ui.rs @@ -9,20 +9,26 @@ pub fn tag_subcommand<'a, 'b>() -> App<'a, 'b> { .author("Matthias Beyer ") .version("0.1") .about("Add or remove tags") + .arg(tag_add_arg()) + .arg(tag_remove_arg()) +} - .arg(Arg::with_name(tag_subcommand_add_arg_name()) - .short("a") - .long("add") - .takes_value(true) - .multiple(true) - .help("Add tags, seperated by comma or by specifying multiple times")) +pub fn tag_add_arg<'a, 'b>() -> Arg<'a, 'b> { + Arg::with_name(tag_subcommand_add_arg_name()) + .short("a") + .long("add") + .takes_value(true) + .multiple(true) + .help("Add tags, seperated by comma or by specifying multiple times") +} - .arg(Arg::with_name(tag_subcommand_remove_arg_name()) - .short("r") - .long("remove") - .takes_value(true) - .multiple(true) - .help("Remove tags, seperated by comma or by specifying multiple times")) +pub fn tag_remove_arg<'a, 'b>() -> Arg<'a, 'b> { + Arg::with_name(tag_subcommand_remove_arg_name()) + .short("r") + .long("remove") + .takes_value(true) + .multiple(true) + .help("Remove tags, seperated by comma or by specifying multiple times") } pub fn tag_subcommand_name() -> &'static str { @@ -60,14 +66,18 @@ pub fn tag_argument_name() -> &'static str { /// /// Returns none if the argument was not specified pub fn get_add_tags(matches: &ArgMatches) -> Option> { - extract_tags(matches, "add-tags", '+') + let add = tag_subcommand_add_arg_name(); + extract_tags(matches, add, '+') + .or_else(|| matches.values_of(add).map(|values| values.map(String::from).collect())) } /// Get the tags which should be removed from the commandline /// /// Returns none if the argument was not specified pub fn get_remove_tags(matches: &ArgMatches) -> Option> { - extract_tags(matches, "remove-tags", '-') + let rem = tag_subcommand_remove_arg_name(); + extract_tags(matches, rem, '+') + .or_else(|| matches.values_of(rem).map(|values| values.map(String::from).collect())) } fn extract_tags(matches: &ArgMatches, specifier: &str, specchar: char) -> Option> {