|
|
|
@ -65,93 +65,135 @@ pub fn parse(raw_args: &Vec<String>) -> Result<Args, Error> {
|
|
|
|
|
return Err(Error::new("no arguments")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
match raw_args[1].to_lowercase().as_str() { |
|
|
|
|
"help" => { |
|
|
|
|
parsed_args.help = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"version" => { |
|
|
|
|
parsed_args.version = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"verbose" => { |
|
|
|
|
parsed_args.verbose_output = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"send" => { |
|
|
|
|
if arg_amount < 5 { |
|
|
|
|
// not enough arguments !
|
|
|
|
|
return Err(Error::new("not enough arguments for send mode")); |
|
|
|
|
|
|
|
|
|
let mut arg_index: usize = 1; |
|
|
|
|
loop { |
|
|
|
|
match raw_args[arg_index].to_lowercase().as_str() { |
|
|
|
|
"help" => { |
|
|
|
|
parsed_args.help = true; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parsed_args.mode = RunMode::SEND; |
|
|
|
|
|
|
|
|
|
// DataType
|
|
|
|
|
match raw_args[2].to_lowercase().as_str() { |
|
|
|
|
"text" => { |
|
|
|
|
parsed_args.send_type = DataType::TEXT; |
|
|
|
|
parsed_args.text_to_send = String::from(&raw_args[3]); |
|
|
|
|
|
|
|
|
|
"version" => { |
|
|
|
|
parsed_args.version = true; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"verbose" => { |
|
|
|
|
parsed_args.verbose_output = true; |
|
|
|
|
arg_index += 1; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"send" => { |
|
|
|
|
if arg_amount < 5 && !parsed_args.verbose_output { |
|
|
|
|
// not enough arguments !
|
|
|
|
|
return Err(Error::new("not enough arguments for send mode")); |
|
|
|
|
} else if arg_amount < 6 && parsed_args.verbose_output { |
|
|
|
|
return Err(Error::new("not enough arguments for send mode")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"file" => { |
|
|
|
|
parsed_args.send_type = DataType::FILE; |
|
|
|
|
parsed_args.send_path = std::path::Path::new(&raw_args[3]).to_path_buf(); |
|
|
|
|
|
|
|
|
|
parsed_args.mode = RunMode::SEND; |
|
|
|
|
|
|
|
|
|
match parsed_args.verbose_output { |
|
|
|
|
true => { |
|
|
|
|
arg_index = 3; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
false => { |
|
|
|
|
arg_index = 2; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"dir" => { |
|
|
|
|
parsed_args.send_type = DataType::DIR; |
|
|
|
|
parsed_args.send_path = std::path::Path::new(&raw_args[3]).to_path_buf(); |
|
|
|
|
// DataType
|
|
|
|
|
match raw_args[arg_index].to_lowercase().as_str() { |
|
|
|
|
"text" => { |
|
|
|
|
parsed_args.send_type = DataType::TEXT; |
|
|
|
|
parsed_args.text_to_send = String::from(&raw_args[arg_index + 1]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"file" => { |
|
|
|
|
parsed_args.send_type = DataType::FILE; |
|
|
|
|
parsed_args.send_path = std::path::Path::new(&raw_args[arg_index + 1]).to_path_buf(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"dir" => { |
|
|
|
|
parsed_args.send_type = DataType::DIR; |
|
|
|
|
parsed_args.send_path = std::path::Path::new(&raw_args[arg_index + 1]).to_path_buf(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_ => { |
|
|
|
|
return Err(Error::new(format!("invalid DataType {}", raw_args[arg_index]).as_str())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// addr
|
|
|
|
|
arg_index += 1; |
|
|
|
|
parsed_args.address_str = raw_args[arg_index + 1].to_string(); |
|
|
|
|
|
|
|
|
|
_ => { |
|
|
|
|
return Err(Error::new(format!("invalid DataType {}", raw_args[2]).as_str())); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// src
|
|
|
|
|
if parsed_args.send_type == DataType::FILE || parsed_args.send_type == DataType::DIR { |
|
|
|
|
// check if exists
|
|
|
|
|
match path::Path::new(raw_args[3].as_str()).exists() { |
|
|
|
|
|
|
|
|
|
"recv" => { |
|
|
|
|
parsed_args.mode = RunMode::RECEIVE;
|
|
|
|
|
|
|
|
|
|
let arg_index: usize; |
|
|
|
|
match parsed_args.verbose_output { |
|
|
|
|
true => { |
|
|
|
|
parsed_args.send_path = path::Path::new(raw_args[3].as_str()).to_path_buf(); |
|
|
|
|
arg_index = 3; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
false => { |
|
|
|
|
return Err(Error::new(format!("\"{}\" does not exist or unreachable", raw_args[3]).as_str())); |
|
|
|
|
arg_index = 2; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if arg_amount > 2 && !parsed_args.verbose_output { |
|
|
|
|
parsed_args.save_directory = path::Path::new(raw_args[arg_index].as_str()).to_path_buf(); |
|
|
|
|
} else if arg_amount > 3 && parsed_args.verbose_output { |
|
|
|
|
parsed_args.save_directory = path::Path::new(raw_args[arg_index].as_str()).to_path_buf(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// addr
|
|
|
|
|
parsed_args.address_str = raw_args[4].to_string(); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"sync" => { |
|
|
|
|
parsed_args.mode = RunMode::SYNC; |
|
|
|
|
|
|
|
|
|
if arg_amount < 3 && !parsed_args.verbose_output { |
|
|
|
|
return Err(Error::new("not enough arguments for syncing")); |
|
|
|
|
} else if arg_amount < 4 && !parsed_args.verbose_output { |
|
|
|
|
return Err(Error::new("not enough arguments for syncing")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let arg_index: usize; |
|
|
|
|
match parsed_args.verbose_output { |
|
|
|
|
true => { |
|
|
|
|
arg_index = 3; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
false => { |
|
|
|
|
arg_index = 2; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let sync_path: &path::Path = path::Path::new(raw_args[arg_index].as_str()); |
|
|
|
|
if !sync_path.exists() || !sync_path.is_dir() { |
|
|
|
|
return Err(Error::new(format!("{} does not exist or is not a directory", sync_path.display()).as_str())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"recv" => { |
|
|
|
|
parsed_args.mode = RunMode::RECEIVE;
|
|
|
|
|
if arg_amount > 2 { |
|
|
|
|
parsed_args.save_directory = path::Path::new(raw_args[2].as_str()).to_path_buf(); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"sync" => { |
|
|
|
|
parsed_args.mode = RunMode::SYNC; |
|
|
|
|
if arg_amount < 3 { |
|
|
|
|
return Err(Error::new("not enough arguments for syncing")); |
|
|
|
|
|
|
|
|
|
"daemon" => { |
|
|
|
|
parsed_args.mode = RunMode::DAEMON; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let sync_path: &path::Path = path::Path::new(raw_args[2].as_str()); |
|
|
|
|
if !sync_path.exists() || !sync_path.is_dir() { |
|
|
|
|
return Err(Error::new(format!("{} does not exist or is not a directory", sync_path.display()).as_str())); |
|
|
|
|
|
|
|
|
|
_ => { |
|
|
|
|
return Err(Error::new(format!("invalid argument {}", raw_args[arg_index]).as_str())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"daemon" => { |
|
|
|
|
parsed_args.mode = RunMode::DAEMON; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_ => { |
|
|
|
|
return Err(Error::new(format!("invalid argument {}", raw_args[1]).as_str())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Ok(parsed_args); |
|
|
|
|