From 4240b6411d9fbf5e2b6cd93d66e37b3522abdd2d Mon Sep 17 00:00:00 2001 From: lastlink Date: Thu, 13 Jun 2019 21:29:11 -0400 Subject: [PATCH] remove clap dep --- .gitignore | 1 + README.md | 6 +- src/main.rs | 199 +++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 179 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 62bd1a4..aff3337 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk +grrs \ No newline at end of file diff --git a/README.md b/README.md index 2cac11a..be924c5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # sclient.rust -compare https://git.rootprojects.org/root/sclient.go/src/branch/master \ No newline at end of file +compare https://git.rootprojects.org/root/sclient.go/src/branch/master + +## getting started + +* `cargo run -- -servername example -k example.com 3000` \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f6eea1e..ff3ff5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,166 @@ -use clap::*; -fn main() { - 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), +use std::env; + +fn increase(number: i32) { + println!("{}", number + 1); +} + +fn decrease(number: i32) { + println!("{}", number - 1); +} + +fn help() { + println!( + "Usage: sclient [--servername ] [-k | --insecure] + ex: sclient whatever.com 3000 + (whatever.com:443 localhost:3000) + ex: sclient whatever.com:4080 0.0.0.0:3000" ); - // println!("Hello, world!"); - // - let matches = app.get_matches(); - println!("{}", matches.value_of("pattern").unwrap()); +} + +// use clap::*; +fn main() { + let args: Vec = env::args().collect(); + println!("{:?}", args); + println!("{:?}", args); + help(); + + let mut flags = Flags::default(); + // let mut flags = vec![]; + + let v = ["10", "40", "30"]; + assert!(v.contains(&"30")); + assert!(!v.contains(&"50")); + + if args + .iter() + .any(|element| element == "-k" || element == "--insecure") + { + flags.insecure = true; + // flags.push(value: "insecure"); + // println!("k exists"); + } else { + println!("k doesn't exists"); + } + + let serverNameArg = args + .iter() + .enumerate() + .find(|(index, element)| *element == "-servername"); + + dbg!(serverNameArg); + + if let Some((index, _)) = serverNameArg { + dbg!(index); + // 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::()); + // dbg!(&args[args.len()-1]); + // dbg!(args[args.len()-1].clone().parse::()); + print!("::canqdy:"); + + let maybe_port = args[args.len() - 1].parse::(); + if maybe_port.is_ok() { + // pull out of maybe option error + flags.port = maybe_port.unwrap(); + } + flags.remote = args[args.len() - 2].clone(); + + if args[args.len() - 3] == "ssh" || args[args.len() - 3] == "rsync" { + flags.custom = args[args.len() - 3].clone(); + } + // if serverNameIndex != &-1 { + + // } + + dbg!(flags); + // println!(flags); + // .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 @@ -31,9 +169,18 @@ fn main() { // target/debug/sclient --help // target/debug/sclient --pattern=kevin +#[derive(Default, Debug)] +struct Flags { + insecure: bool, + servername: String, + remote: String, + custom: String, + port: i32, +} + struct Cli { - pattern: String, - path: std::path::PathBuf, + 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 (to disable SNI use an IP as and do use this option)")