clean parsing

This commit is contained in:
lastlink 2019-06-13 21:40:37 -04:00
parent 4240b6411d
commit 585048dace
2 changed files with 25 additions and 139 deletions

View File

@ -4,4 +4,11 @@ compare https://git.rootprojects.org/root/sclient.go/src/branch/master
## getting started ## getting started
* `cargo run -- -servername example -k example.com 3000` * run directly
* `cargo run -- -servername example -k example.com 3000`
* `cargo run test`
* `cargo run -- --help`
* build
* `cargo build`
* `target/debug/sclient --help` run from build
* `.\target\debug\sclient.exe --help` windows cmd

View File

@ -1,45 +1,33 @@
use std::env; use std::env;
fn increase(number: i32) {
println!("{}", number + 1);
}
fn decrease(number: i32) {
println!("{}", number - 1);
}
fn help() { fn help() {
println!( println!(
"Usage: sclient [--servername <string>] [-k | --insecure] <remote> <port> "Usage: sclient [--servername <string>] [-k | --insecure] [ssh|rsync] <remote> <port>
ex: sclient whatever.com 3000 ex: sclient whatever.com 3000
(whatever.com:443 localhost:3000) (whatever.com:443 localhost:3000)
ex: sclient whatever.com:4080 0.0.0.0:3000" ex: sclient whatever.com:4080 0.0.0.0:3000"
); );
} }
// use clap::*;
fn main() { fn main() {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
println!("{:?}", args);
println!("{:?}", args); if args
.iter()
.any(|element| element == "-h" || element == "--help")
{
help(); help();
return;
}
let mut flags = Flags::default(); let mut flags = Flags::default();
// let mut flags = vec![];
let v = ["10", "40", "30"];
assert!(v.contains(&"30"));
assert!(!v.contains(&"50"));
// try!()
if args if args
.iter() .iter()
.any(|element| element == "-k" || element == "--insecure") .any(|element| element == "-k" || element == "--insecure")
{ {
flags.insecure = true; flags.insecure = true;
// flags.push(value: "insecure");
// println!("k exists");
} else {
println!("k doesn't exists");
} }
let serverNameArg = args let serverNameArg = args
@ -47,128 +35,30 @@ fn main() {
.enumerate() .enumerate()
.find(|(index, element)| *element == "-servername"); .find(|(index, element)| *element == "-servername");
dbg!(serverNameArg);
if let Some((index, _)) = serverNameArg { if let Some((index, _)) = serverNameArg {
dbg!(index); flags.servername = args[index + 1].clone();
// if index.is_some() {
dbg!(&args[index + 1]);
flags.servername = args[index + 1].clone(); //.into();
// }
} }
print!("canqdy:");
// print!("{}",args.len()-1);
print!("{}", args[args.len() - 1]);
print!("{:?}", args[args.len() - 1].parse::<i32>());
// dbg!(&args[args.len()-1]);
// dbg!(args[args.len()-1].clone().parse::<i32>());
print!("::canqdy:");
// get port
let maybe_port = args[args.len() - 1].parse::<i32>(); let maybe_port = args[args.len() - 1].parse::<i32>();
if maybe_port.is_ok() { if maybe_port.is_ok() {
// pull out of maybe option error // pull out of maybe option error
flags.port = maybe_port.unwrap(); flags.port = maybe_port.unwrap();
} else {
help();
print!("<port> needs to be an integer");
} }
// get remote
flags.remote = args[args.len() - 2].clone(); flags.remote = args[args.len() - 2].clone();
if args[args.len() - 3] == "ssh" || args[args.len() - 3] == "rsync" { if args[args.len() - 3] == "ssh" || args[args.len() - 3] == "rsync" {
flags.custom = args[args.len() - 3].clone(); flags.custom = args[args.len() - 3].clone();
} }
// if serverNameIndex != &-1 {
// }
dbg!(flags); dbg!(flags);
// println!(flags); // tls port forward functionality
// .contains(x: &"-k");// .contains(&"-k");
// println!(args.contains(&"-k"));
for argument in env::args() {
println!("{} {}", argument, "test");
}
// let pattern = std::env::args().nth(1).expect("no pattern given");
// let path = std::env::args().nth(2).expect("no path given");
// let args2 = Cli {
// pattern: pattern,
// // path: std::path::PathBuf::from(path),
// };
// println!("{}", args2.pattern)
// match args.len() {
// // no arguments passed
// 1 => {
// println!("My name is 'match_args'. Try passing some arguments!");
// },
// // one argument passed
// 2 => {
// match args[1].parse() {
// Ok(42) => println!("This is the answer!"),
// _ => println!("This is not the answer."),
// }
// },
// // one command and one argument passed
// 3 => {
// let cmd = &args[1];
// let num = &args[2];
// // parse the number
// let number: i32 = match num.parse() {
// Ok(n) => {
// n
// },
// Err(_) => {
// eprintln!("error: second argument not an integer");
// help();
// return;
// },
// };
// // parse the command
// match &cmd[..] {
// "increase" => increase(number),
// "decrease" => decrease(number),
// _ => {
// eprintln!("error: invalid command");
// help();
// },
// }
// },
// // all the other cases
// _ => {
// // show a help message
// help();
// }
// }
// let app = App::new("sclient")
// .arg(
// Arg::with_name("pattern")
// .long("pattern")
// .takes_value(true)
// .required(true),
// )
// .arg(
// Arg::with_name("servername")
// .long("servername")
// .takes_value(true)
// .required(true),
// )
// .arg(
// Arg::with_name("k")
// .short("k")
// .takes_value(true) // remove if t/f
// .required(true),
// );
// println!("Hello, world!");
//
// let matches = app.get_matches();
// println!("{}", matches.value_of("pattern").unwrap());
} }
// cargo run test
// cargo build
// cargo run -- --help
// target/debug/sclient --help
// target/debug/sclient --pattern=kevin
#[derive(Default, Debug)] #[derive(Default, Debug)]
struct Flags { struct Flags {
insecure: bool, insecure: bool,
@ -177,14 +67,3 @@ struct Flags {
custom: String, custom: String,
port: i32, port: i32,
} }
struct Cli {
pattern: String,
// path: std::path::PathBuf,
}
// insecure := flag.Bool("k", false, "ignore bad TLS/SSL/HTTPS certificates")
// servername := flag.String("servername", "", "specify a servername different from <remote> (to disable SNI use an IP as <remote> and do use this option)")
// flag.BoolVar(insecure, "insecure", false, "ignore bad TLS/SSL/HTTPS certificates")
// flag.Parse()
// remotestr := flag.Arg(0)
// localstr := flag.Arg(1)