Add clap for parsing command line arguments

This commit is contained in:
Dheepak Krishnamurthy 2020-07-30 19:41:11 -06:00
parent 58c0ee5cb8
commit 0b5fa70957
4 changed files with 94 additions and 8 deletions

66
Cargo.lock generated
View file

@ -15,12 +15,32 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "arc-swap" name = "arc-swap"
version = "0.4.7" version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "0.1.7" version = "0.1.7"
@ -76,6 +96,21 @@ dependencies = [
"time", "time",
] ]
[[package]]
name = "clap"
version = "2.33.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]] [[package]]
name = "cloudabi" name = "cloudabi"
version = "0.0.3" version = "0.0.3"
@ -220,6 +255,15 @@ version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724"
[[package]]
name = "hermit-abi"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "ident_case" name = "ident_case"
version = "1.0.1" version = "1.0.1"
@ -651,6 +695,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "syn" name = "syn"
version = "0.13.11" version = "0.13.11"
@ -706,6 +756,7 @@ name = "taskwarrior-tui"
version = "0.5.1" version = "0.5.1"
dependencies = [ dependencies = [
"chrono", "chrono",
"clap",
"crossterm", "crossterm",
"rand 0.7.3", "rand 0.7.3",
"serde", "serde",
@ -716,6 +767,15 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.43" version = "0.1.43"
@ -773,6 +833,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.9.0+wasi-snapshot-preview1" version = "0.9.0+wasi-snapshot-preview1"

View file

@ -20,6 +20,7 @@ crossterm-backend = ["tui/crossterm", "crossterm"]
[dependencies] [dependencies]
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
clap = "2.33"
task-hookrs = "0.7" task-hookrs = "0.7"
rand = "0.7" rand = "0.7"
shlex = "0.1" shlex = "0.1"

View file

@ -97,7 +97,7 @@ pub enum AppMode {
TaskError, TaskError,
} }
pub struct App { pub struct TaskwarriorTUIApp {
pub should_quit: bool, pub should_quit: bool,
pub state: TableState, pub state: TableState,
pub cursor_location: usize, pub cursor_location: usize,
@ -111,9 +111,9 @@ pub struct App {
pub mode: AppMode, pub mode: AppMode,
} }
impl App { impl TaskwarriorTUIApp {
pub fn new() -> App { pub fn new() -> TaskwarriorTUIApp {
let mut app = App { let mut app = TaskwarriorTUIApp {
should_quit: false, should_quit: false,
state: TableState::default(), state: TableState::default(),
tasks: vec![], tasks: vec![],
@ -724,7 +724,7 @@ impl App {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::app::App; use crate::app::TaskwarriorTUIApp;
use crate::util::setup_terminal; use crate::util::setup_terminal;
use std::io::stdin; use std::io::stdin;
@ -733,7 +733,7 @@ mod tests {
#[test] #[test]
fn test_app() { fn test_app() {
let mut app = App::new(); let mut app = TaskwarriorTUIApp::new();
app.update(); app.update();
//println!("{:?}", app.tasks); //println!("{:?}", app.tasks);

View file

@ -16,8 +16,9 @@ use std::process::Command;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use tui::backend::Backend; use tui::backend::Backend;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use clap::{Arg, App, SubCommand};
use app::{App, AppMode}; use app::{TaskwarriorTUIApp, AppMode};
use crate::util::Key; use crate::util::Key;
use std::sync::{ use std::sync::{
@ -30,6 +31,24 @@ const APP_NAME: &'static str = env!("CARGO_PKG_NAME");
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let matches = App::new(APP_NAME)
.version(APP_VERSION)
.author("Dheepak Krishnamurthy <@kdheepak>")
.about("A taskwarrior terminal user interface")
.arg(Arg::with_name("config")
.short("c")
.long("config")
.value_name("FILE")
.help("Sets a custom config file")
.takes_value(true))
.get_matches();
let config = matches.value_of("config").unwrap_or("default.conf");
tui_main(config)?;
Ok(())
}
fn tui_main(config: &str) -> Result<(), Box<dyn Error>> {
// Terminal initialization // Terminal initialization
let mut terminal = setup_terminal(); let mut terminal = setup_terminal();
terminal.clear()?; terminal.clear()?;
@ -39,7 +58,7 @@ fn main() -> Result<(), Box<dyn Error>> {
tick_rate: Duration::from_millis(250), tick_rate: Duration::from_millis(250),
}); });
let mut app = App::new(); let mut app = TaskwarriorTUIApp::new();
app.next(); app.next();
loop { loop {