summaryrefslogtreecommitdiffstats
path: root/libimagtag
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-02-23 11:02:59 +0100
committerMatthias Beyer <mail@beyermatthias.de>2016-02-23 11:02:59 +0100
commit3d54431acf13cffe954a33c0d98c3e2a655ce2cb (patch)
treec17f8b6d01eccb86760dce5a3cb62ff7d95e4ede /libimagtag
parent38ca44d10d6d87a3d18fafff84322d77e1d5fd89 (diff)
Add UI helpers for tagging
Diffstat (limited to 'libimagtag')
-rw-r--r--libimagtag/Cargo.toml1
-rw-r--r--libimagtag/src/lib.rs3
-rw-r--r--libimagtag/src/ui.rs70
3 files changed, 74 insertions, 0 deletions
diff --git a/libimagtag/Cargo.toml b/libimagtag/Cargo.toml
index 001efb43..0fd71402 100644
--- a/libimagtag/Cargo.toml
+++ b/libimagtag/Cargo.toml
@@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
[dependencies]
+clap = "2.1.1"
log = "0.3.5"
regex = "0.1.47"
toml = "0.1.25"
diff --git a/libimagtag/src/lib.rs b/libimagtag/src/lib.rs
index 2db44143..f4d1c543 100644
--- a/libimagtag/src/lib.rs
+++ b/libimagtag/src/lib.rs
@@ -1,3 +1,4 @@
+extern crate clap;
#[macro_use] extern crate log;
extern crate regex;
extern crate toml;
@@ -9,3 +10,5 @@ pub mod result;
pub mod tag;
pub mod tagable;
pub mod util;
+pub mod ui;
+
diff --git a/libimagtag/src/ui.rs b/libimagtag/src/ui.rs
new file mode 100644
index 00000000..e889e224
--- /dev/null
+++ b/libimagtag/src/ui.rs
@@ -0,0 +1,70 @@
+use clap::{Arg, ArgMatches, App, SubCommand};
+
+use tag::Tag;
+
+/// Generates a clap::SubCommand to be integrated in the commandline-ui builder for building a
+/// "tags --add foo --remove bar" subcommand to do tagging action.
+pub fn tag_subcommand<'a, 'b>() -> App<'a, 'b> {
+ SubCommand::with_name("tags")
+ .author("Matthias Beyer <mail@beyermatthias.de>")
+ .version("0.1")
+ .about("Add or remove tags")
+
+ .arg(Arg::with_name("add-tags")
+ .short("a")
+ .long("add")
+ .takes_value(true)
+ .multiple(true)
+ .help("Add tags, seperated by comma or by specifying multiple times"))
+
+ .arg(Arg::with_name("remove-tags")
+ .short("r")
+ .long("remove")
+ .takes_value(true)
+ .multiple(true)
+ .help("Remove tags, seperated by comma or by specifying multiple times"))
+}
+
+/// Generates a clap::Arg which can be integrated into the commandline-ui builder for building a
+/// "-t" or "--tags" argument which takes values for tagging actions (add, remove)
+pub fn tag_argument<'a, 'b>() -> Arg<'a, 'b> {
+ Arg::with_name("specify-tags")
+ .short("t")
+ .long("tags")
+ .takes_value(true)
+ .multiple(true)
+ .help("Add or remove tags, prefixed by '+' (for adding) or '-' (for removing)")
+}
+
+/// Get the tags which should be added from the commandline
+///
+/// Returns none if the argument was not specified
+pub fn get_add_tags(matches: &ArgMatches) -> Option<Vec<Tag>> {
+ extract_tags(matches, "add-tags", '+')
+}
+
+/// 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<Vec<Tag>> {
+ extract_tags(matches, "remove-tags", '-')
+}
+
+fn extract_tags(matches: &ArgMatches, specifier: &str, specchar: char) -> Option<Vec<Tag>> {
+ if let Some(submatch) = matches.subcommand_matches("tags") {
+ submatch.values_of(specifier)
+ .map(|values| values.map(String::from).collect())
+ } else {
+ matches.values_of("specify-tags")
+ .map(|argmatches| {
+ argmatches
+ .map(String::from)
+ .filter(|s| s.chars().next() == Some(specchar))
+ .map(|s| {
+ String::from(s.split_at(1).1)
+ })
+ .collect()
+ })
+ }
+}
+