|
|
|
@ -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; |
|
|
|
|
} |
|
|
|
|
let mut arg_index: usize = 1; |
|
|
|
|
loop { |
|
|
|
|
match raw_args[arg_index].to_lowercase().as_str() { |
|
|
|
|
"help" => { |
|
|
|
|
parsed_args.help = true; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"verbose" => { |
|
|
|
|
parsed_args.verbose_output = true; |
|
|
|
|
} |
|
|
|
|
"version" => { |
|
|
|
|
parsed_args.version = true; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"send" => { |
|
|
|
|
if arg_amount < 5 { |
|
|
|
|
// not enough arguments !
|
|
|
|
|
return Err(Error::new("not enough arguments for send mode")); |
|
|
|
|
"verbose" => { |
|
|
|
|
parsed_args.verbose_output = true; |
|
|
|
|
arg_index += 1; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parsed_args.mode = RunMode::SEND; |
|
|
|
|
"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")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parsed_args.mode = RunMode::SEND; |
|
|
|
|
|
|
|
|
|
match parsed_args.verbose_output { |
|
|
|
|
true => { |
|
|
|
|
arg_index = 3; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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]); |
|
|
|
|
false => { |
|
|
|
|
arg_index = 2; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"file" => { |
|
|
|
|
parsed_args.send_type = DataType::FILE; |
|
|
|
|
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())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"dir" => { |
|
|
|
|
parsed_args.send_type = DataType::DIR; |
|
|
|
|
parsed_args.send_path = std::path::Path::new(&raw_args[3]).to_path_buf(); |
|
|
|
|
// addr
|
|
|
|
|
arg_index += 1; |
|
|
|
|
parsed_args.address_str = raw_args[arg_index + 1].to_string(); |
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
"recv" => { |
|
|
|
|
parsed_args.mode = RunMode::RECEIVE;
|
|
|
|
|
|
|
|
|
|
let arg_index: usize; |
|
|
|
|
match parsed_args.verbose_output { |
|
|
|
|
true => { |
|
|
|
|
arg_index = 3; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
false => { |
|
|
|
|
arg_index = 2; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_ => { |
|
|
|
|
return Err(Error::new(format!("invalid DataType {}", raw_args[2]).as_str())); |
|
|
|
|
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(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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() { |
|
|
|
|
"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 => { |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// addr
|
|
|
|
|
parsed_args.address_str = raw_args[4].to_string(); |
|
|
|
|
} |
|
|
|
|
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); |
|
|
|
|