Skip to content

Commit

Permalink
Rewrote argument handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLostLambda committed Nov 13, 2017
1 parent fab12d2 commit e3b6b50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ftc_http"
version = "1.1.1"
version = "1.2.0"
authors = ["Brooks J Rady <[email protected]>"]

[lib]
Expand Down
62 changes: 21 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,38 @@
extern crate ftc_http;

use std::iter::Peekable;
use std::process;
use std::env;

static VERISON_STR: &'static str = "v1.1.1";
static VERISON_STR: &'static str = "v1.2.0";
static STARTUP_OPTS: &'static str = "hv";

fn main() {
let mut args = env::args().skip(1).peekable();
let pwd = env::current_dir().expect("It is not possible to access the current directory");

loop {
match args.next() {
Some(arg) => if is_option(&arg) {
handle_options(&mut args, arg)
} else {
handle_options(&mut args, "-h".to_string())
},
None => break,
};
fn is_option(arg: &str) -> bool {
arg.chars().nth(0).unwrap_or(' ') == '-'
}
}

fn is_option(arg: &str) -> bool {
arg.chars().nth(0).unwrap_or(' ') == '-'
}
let mut args = env::args().skip(1).filter(|arg| !is_option(arg));

fn handle_options<I: Iterator<Item = String>>(args: &mut Peekable<I>, options: String) {
let pwd = env::current_dir().expect("It is not possible to access the current directory");
let empty_peek = &String::new();
for option in options.chars().skip(1) {
match option {
let mut opts: String = env::args()
.filter(|arg| is_option(arg))
.map(|arg| arg[1..].to_string())
.collect();

if opts.chars().any(|c| STARTUP_OPTS.contains(c)) {
opts = opts.chars().filter(|&c| STARTUP_OPTS.contains(c)).collect();
}

for opt in opts.chars() {
match opt {
'd' => {
{
let next_arg = args.peek().unwrap_or(empty_peek);
if !is_option(&next_arg) {
ftc_http::down(&pwd.join(&next_arg));
} else {
ftc_http::down(&pwd);
continue;
}
}
args.next();
let next_arg = args.next().unwrap_or(String::new());
ftc_http::down(&pwd.join(&next_arg));
}
'u' => {
{
let next_arg = args.peek().unwrap_or(empty_peek);
if !is_option(&next_arg) {
ftc_http::up(&pwd.join(&next_arg));
} else {
ftc_http::up(&pwd);
continue;
}
}
args.next();
let next_arg = args.next().unwrap_or(String::new());
ftc_http::up(&pwd.join(&next_arg));
}
'b' => ftc_http::build(),
'w' => ftc_http::wipe(),
Expand Down

0 comments on commit e3b6b50

Please sign in to comment.