From 50ade80ab9dc8837fcfd5aa67b3a6252c04e43a7 Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Sat, 18 Dec 2021 23:13:02 -0500 Subject: [PATCH] Improve argument parsing --- src/config.rs | 4 +-- src/lib.rs | 75 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/config.rs b/src/config.rs index a7b9b33..3c63823 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,9 +21,9 @@ pub struct Repository { } impl Repository { - pub fn new(url: String) -> Repository { + pub fn new(name: String, url: String) -> Repository { Repository { - name: url.clone(), + name, url, } } diff --git a/src/lib.rs b/src/lib.rs index e3c355f..c1e6db0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ use std::error::Error; pub struct State { cmd: Option, url: Option, + name: Option, pub config: String, } @@ -38,6 +39,7 @@ impl State { let mut state = State { cmd: None, url: None, + name: None, config: format!("{}/.config/shepherd/config.toml", env::var("HOME").unwrap()), }; @@ -69,39 +71,44 @@ impl State { _ => {} } } - } - // add command - else if x == "add" { - let url = args.next(); - match url { - Some(x) => match state.cmd { + } else if let None = state.cmd { + // add command + if x == "add" { + let name = args.next(); + match name { + Some(x) => { + state.name = Some(x); + let url = args.next(); + match url { + Some(x) => { + state.cmd = Some(Cmd::Add); + state.url = Some(x); + } + None => { + eprintln!("No URL provided for command\n"); + } + } + } + None => eprintln!("No Name provided for command\n"), + } + } + // fetch command + else if x == "fetch" { + match state.cmd { None => { - state.cmd = Some(Cmd::Add); - state.url = Some(x); + state.cmd = Some(Cmd::Fetch); } _ => {} - }, - None => { - eprintln!("No URL provided for command\n"); } } - } - // fetch command - else if x == "fetch" { - match state.cmd { - None => { - state.cmd = Some(Cmd::Fetch); + // list command + else if x == "list" { + match state.cmd { + None => { + state.cmd = Some(Cmd::List); + } + _ => {} } - _ => {} - } - } - // list command - else if x == "list" { - match state.cmd { - None => { - state.cmd = Some(Cmd::List); - } - _ => {} } } arg = args.next(); @@ -126,10 +133,16 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box> { println!("{}", config.to_string().unwrap()); } Some(Cmd::Add) => { - let repo = Repository::new(match state.url { - Some(x) => x, - None => String::new(), - }); + let repo = Repository::new( + match state.name { + Some(x) => x, + _ => String::new(), + }, + match state.url { + Some(x) => x, + _ => String::new(), + }, + ); if let None = config.repositories.iter().find(|x| **x == repo) { config.repositories.push(repo); config.save_config()?;