71 lines
2.4 KiB
Rust
71 lines
2.4 KiB
Rust
|
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()
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|