summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNora <nora.widdecke@tu-bs.de>2019-02-24 21:02:47 +0100
committerNora <nora.widdecke@tu-bs.de>2019-02-24 21:02:47 +0100
commitd7eb5bc24ce8db4ea679654ab33726dc924b7d53 (patch)
tree9555cfc1505760dbc908725a9602bc345eab5fcd
parent80cbd985236b5875b4b4c0b4233b793c8b34c3c1 (diff)
move cli to cli.rs
-rw-r--r--src/bin/khaleesi.rs68
-rw-r--r--src/cli.rs44
-rw-r--r--src/lib.rs1
3 files changed, 62 insertions, 51 deletions
diff --git a/src/bin/khaleesi.rs b/src/bin/khaleesi.rs
index e44a719..5ddfb89 100644
--- a/src/bin/khaleesi.rs
+++ b/src/bin/khaleesi.rs
@@ -5,46 +5,17 @@ use khaleesi::config::Config;
use khaleesi::KhResult;
use std::env;
-use structopt::clap::ArgMatches;
+use std::path::PathBuf;
use structopt::StructOpt;
-#[derive(Debug, StructOpt)]
-#[structopt(name = "khalessi", about = "Command line calendar tool.")]
-struct Khaleesi {
- /// Verbosity
- #[structopt(short = "v", parse(from_occurrences))]
- verbosity: u64,
- #[structopt(subcommand)]
- cmd: Command,
-}
-
-#[derive(Debug, StructOpt)]
-enum Command {
- /// Show agenda view
- #[structopt(name = "agenda")]
- Agenda {
- /// Rebuild index
- #[structopt(name = "args")]
- args: Vec<String>,
- },
- /// Rebuild index
- #[structopt(name = "index")]
- Index {
- /// Rebuild index
- #[structopt(short = "r", long = "reindex")]
- reindex: bool,
- /// index path
- #[structopt(parse(from_os_str))]
- path: Option<PathBuf>
- },
-}
+use khaleesi::cli;
fn main() {
- let args = Khaleesi::clap().get_matches();
- println!("{:?}", args);
+ //let clap_args = CommandLine::clap().get_matches();
+ //println!("{:?}", clap_args);
- //let opt = Khaleesi::from_args();
- //println!("{:?}", opt);
+ let args = cli::CommandLine::from_args();
+ println!("{:?}", args);
#[cfg(not(debug_assertions))]
{
@@ -52,41 +23,36 @@ fn main() {
use khaleesi::defaults;
defaults::set_khaleesi_dir(&dir);
}
- init_logger(1 + args.occurrences_of("verbosity"));
+ init_logger(1 + args.verbosity);
}
//set default log level to INFO in debug builds
#[cfg(debug_assertions)]
- init_logger(3 + args.occurrences_of("verbosity"));
+ init_logger(3 + args.verbosity);
let config = Config::read_config();
- //let args: Vec<String> = env::args().collect();
- //let binary_name = &args[0].split('/').last().unwrap();
- //let mut args = args[1..].iter().map(|s| s.as_str()).collect::<Vec<&str>>();
- //if *binary_name != "khaleesi" && binary_name.starts_with("kh") {
- // let command = &binary_name[2..];
- // args.push(command);
- // args.rotate_right(1);
- //}
let result = main_internal(&args, &config);
if let Err(error) = result {
error!("{}", error)
}
}
-fn main_internal(args: &ArgMatches, config: &Config) -> KhResult<()> {
- match args.subcommand() {
- ("agenda", Some(sub_args)) => {
- let args = sub_args.values_of("args").map_or_else(|| Vec::new(), |x| x.collect::<Vec<&str>>());
- agenda::show_events(&config, &args)
+fn main_internal(args: &cli::CommandLine, config: &Config) -> KhResult<()> {
+ match &args.cmd {
+ cli::Command::Agenda(x) => {
+ agenda::show_events(&config, &x.args.iter().map(|x| x.as_ref()).collect::<Vec<&str>>())
}
// "copy" => copy::do_copy(args),
// "cursor" => cursor::do_cursor(args),
// "delete" => delete::do_delete(args),
// "edit" => edit::do_edit(args),
// "get" => get::action_get(args),
- ("index", Some(sub_args)) => index::action_index(sub_args),
+ cli::Command::Index(x) => {
+ println!("{:?}", x);
+ //index::action_index(),
+ Ok(())
+ }
// "list" => list::list_by_args(args),
// "modify" => modify::do_modify(args),
// "new" => new::do_new(args),
diff --git a/src/cli.rs b/src/cli.rs
new file mode 100644
index 0000000..92441bb
--- /dev/null
+++ b/src/cli.rs
@@ -0,0 +1,44 @@
+use structopt::StructOpt;
+use std::path::PathBuf;
+
+#[derive(Debug, StructOpt)]
+#[structopt(
+ author = "me",
+ name = "khalessi",
+ about = "Command line calendar tool."
+)]
+pub struct CommandLine {
+ /// Verbosity
+ #[structopt(short = "v", parse(from_occurrences))]
+ pub verbosity: u64,
+ #[structopt(subcommand)]
+ pub cmd: Command,
+}
+
+#[derive(Debug, StructOpt)]
+pub enum Command {
+ /// Show agenda view
+ #[structopt(name = "agenda")]
+ Agenda(Agenda),
+ /// Rebuild index
+ #[structopt(name = "index")]
+ Index(Index)
+}
+
+#[derive(Debug, StructOpt)]
+pub struct Agenda {
+ /// Show agenda view
+ #[structopt(name = "args")]
+ pub args: Vec<String>,
+}
+
+#[derive(Debug, StructOpt)]
+pub struct Index {
+ /// Rebuild index
+ #[structopt(short = "r", long = "reindex")]
+ pub reindex: bool,
+ /// index path
+ #[structopt(name = "path", parse(from_os_str))]
+ //TODO for some reason, this is parsed as an &str
+ pub path: Option<PathBuf>,
+}
diff --git a/src/lib.rs b/src/lib.rs
index 751f604..65f78d1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,6 +6,7 @@ pub type KhResult<T> = Result<T,errors::KhError>;
pub mod actions;
pub mod backup;
pub mod calendars;
+pub mod cli;
pub mod config;
pub mod cursorfile;
pub mod defaults;