Improve argument parsing
This commit is contained in:
parent
c57e2e645d
commit
50ade80ab9
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
31
src/lib.rs
31
src/lib.rs
|
@ -11,6 +11,7 @@ use std::error::Error;
|
|||
pub struct State {
|
||||
cmd: Option<Cmd>,
|
||||
url: Option<String>,
|
||||
name: Option<String>,
|
||||
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,23 +71,27 @@ impl State {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if let None = state.cmd {
|
||||
// add command
|
||||
else if x == "add" {
|
||||
if x == "add" {
|
||||
let name = args.next();
|
||||
match name {
|
||||
Some(x) => {
|
||||
state.name = Some(x);
|
||||
let url = args.next();
|
||||
match url {
|
||||
Some(x) => match state.cmd {
|
||||
None => {
|
||||
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 {
|
||||
|
@ -104,6 +110,7 @@ impl State {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
arg = args.next();
|
||||
}
|
||||
|
||||
|
@ -126,10 +133,16 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
|
|||
println!("{}", config.to_string().unwrap());
|
||||
}
|
||||
Some(Cmd::Add) => {
|
||||
let repo = Repository::new(match state.url {
|
||||
let repo = Repository::new(
|
||||
match state.name {
|
||||
Some(x) => x,
|
||||
None => String::new(),
|
||||
});
|
||||
_ => 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()?;
|
||||
|
|
Loading…
Reference in New Issue