Improve argument parsing
This commit is contained in:
parent
c57e2e645d
commit
50ade80ab9
|
@ -21,9 +21,9 @@ pub struct Repository {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Repository {
|
impl Repository {
|
||||||
pub fn new(url: String) -> Repository {
|
pub fn new(name: String, url: String) -> Repository {
|
||||||
Repository {
|
Repository {
|
||||||
name: url.clone(),
|
name,
|
||||||
url,
|
url,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
75
src/lib.rs
75
src/lib.rs
|
@ -11,6 +11,7 @@ use std::error::Error;
|
||||||
pub struct State {
|
pub struct State {
|
||||||
cmd: Option<Cmd>,
|
cmd: Option<Cmd>,
|
||||||
url: Option<String>,
|
url: Option<String>,
|
||||||
|
name: Option<String>,
|
||||||
pub config: String,
|
pub config: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +39,7 @@ impl State {
|
||||||
let mut state = State {
|
let mut state = State {
|
||||||
cmd: None,
|
cmd: None,
|
||||||
url: None,
|
url: None,
|
||||||
|
name: None,
|
||||||
config: format!("{}/.config/shepherd/config.toml", env::var("HOME").unwrap()),
|
config: format!("{}/.config/shepherd/config.toml", env::var("HOME").unwrap()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -69,39 +71,44 @@ impl State {
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if let None = state.cmd {
|
||||||
// add command
|
// add command
|
||||||
else if x == "add" {
|
if x == "add" {
|
||||||
let url = args.next();
|
let name = args.next();
|
||||||
match url {
|
match name {
|
||||||
Some(x) => match state.cmd {
|
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 => {
|
None => {
|
||||||
state.cmd = Some(Cmd::Add);
|
state.cmd = Some(Cmd::Fetch);
|
||||||
state.url = Some(x);
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
|
||||||
None => {
|
|
||||||
eprintln!("No URL provided for command\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
// list command
|
||||||
// fetch command
|
else if x == "list" {
|
||||||
else if x == "fetch" {
|
match state.cmd {
|
||||||
match state.cmd {
|
None => {
|
||||||
None => {
|
state.cmd = Some(Cmd::List);
|
||||||
state.cmd = Some(Cmd::Fetch);
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// list command
|
|
||||||
else if x == "list" {
|
|
||||||
match state.cmd {
|
|
||||||
None => {
|
|
||||||
state.cmd = Some(Cmd::List);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
arg = args.next();
|
arg = args.next();
|
||||||
|
@ -126,10 +133,16 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
println!("{}", config.to_string().unwrap());
|
println!("{}", config.to_string().unwrap());
|
||||||
}
|
}
|
||||||
Some(Cmd::Add) => {
|
Some(Cmd::Add) => {
|
||||||
let repo = Repository::new(match state.url {
|
let repo = Repository::new(
|
||||||
Some(x) => x,
|
match state.name {
|
||||||
None => String::new(),
|
Some(x) => x,
|
||||||
});
|
_ => String::new(),
|
||||||
|
},
|
||||||
|
match state.url {
|
||||||
|
Some(x) => x,
|
||||||
|
_ => String::new(),
|
||||||
|
},
|
||||||
|
);
|
||||||
if let None = config.repositories.iter().find(|x| **x == repo) {
|
if let None = config.repositories.iter().find(|x| **x == repo) {
|
||||||
config.repositories.push(repo);
|
config.repositories.push(repo);
|
||||||
config.save_config()?;
|
config.save_config()?;
|
||||||
|
|
Loading…
Reference in New Issue