split args

This commit is contained in:
phiresky 2019-06-11 13:43:01 +02:00
parent 0489a49d66
commit e9bd500c1e
3 changed files with 43 additions and 55 deletions

View File

@ -9,7 +9,7 @@ use crate::preproc::PreprocConfig;
use failure::*; use failure::*;
use log::*; use log::*;
use regex::{Regex, RegexSet}; use regex::{Regex, RegexSet};
use std::borrow::Borrow;
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::io::prelude::*; use std::io::prelude::*;
@ -203,8 +203,8 @@ pub fn adapter_matcher<T: AsRef<str>>(
eprintln!(" - {}", fname_regexes[*fmatch].1.metadata().name); eprintln!(" - {}", fname_regexes[*fmatch].1.metadata().name);
} }
} }
if mime_matches.len() == 0 { if mime_matches.is_empty() {
if fname_matches.len() == 0 { if fname_matches.is_empty() {
None None
} else { } else {
Some(fname_regexes[fname_matches[0]].1.clone()) Some(fname_regexes[fname_matches[0]].1.clone())

View File

@ -123,3 +123,41 @@ where
} }
} }
} }
/// Split arguments into the ones we care about and the ones rg cares about
pub fn split_args() -> Fallible<(RgaArgs, Vec<OsString>)> {
let mut app = RgaArgs::clap();
app.p.create_help_and_version();
let mut firstarg = true;
// debug!("{:#?}", app.p.flags);
let (our_args, mut passthrough_args): (Vec<OsString>, Vec<OsString>) = std::env::args_os()
.partition(|os_arg| {
if firstarg {
// hacky, but .enumerate() would be ugly because partition is too simplistic
firstarg = false;
return true;
}
if let Some(arg) = os_arg.to_str() {
arg.starts_with("--rga-")
|| arg.starts_with("--rg-")
|| arg == "--help"
|| arg == "-h"
|| arg == "--version"
|| arg == "-V"
} else {
// args that are not unicode can only be filenames, pass them to rg
false
}
});
debug!("our_args: {:?}", our_args);
let matches = parse_args(our_args)?;
if matches.rg_help {
passthrough_args.insert(0, "--help".into());
}
if matches.rg_version {
passthrough_args.insert(0, "--version".into());
}
debug!("passthrough_args: {:?}", passthrough_args);
Ok((matches, passthrough_args))
}

View File

@ -1,62 +1,12 @@
use failure::Fallible; use failure::Fallible;
use log::*;
use rga::adapters::spawning::map_exe_error; use rga::adapters::spawning::map_exe_error;
use rga::adapters::*; use rga::adapters::*;
use rga::args::*; use rga::args::*;
use std::ffi::OsString;
use std::process::Command; use std::process::Command;
use structopt::StructOpt;
fn split_args() -> Fallible<(RgaArgs, Vec<OsString>)> {
let mut app = RgaArgs::clap();
app.p.create_help_and_version();
let mut firstarg = true;
// debug!("{:#?}", app.p.flags);
let (our_args, mut passthrough_args): (Vec<OsString>, Vec<OsString>) = std::env::args_os()
.partition(|os_arg| {
if firstarg {
// hacky, but .enumerate() would be ugly because partition is too simplistic
firstarg = false;
return true;
}
if let Some(arg) = os_arg.to_str() {
for flag in app.p.flags() {
if let Some(s) = flag.s.short {
if arg == format!("-{}", s) {
return true;
}
}
if let Some(l) = flag.s.long {
if arg == format!("--{}", l) {
return true;
}
}
// println!("{}", flag.s.long);
}
for opt in app.p.opts() {
// only parse --x=... for now
if let Some(l) = opt.s.long {
if arg.starts_with(&format!("--{}", l)) {
return true;
}
}
}
}
false
});
debug!("our_args: {:?}", our_args);
let matches = parse_args(our_args)?;
if matches.rg_help {
passthrough_args.insert(0, "--help".into());
}
if matches.rg_version {
passthrough_args.insert(0, "--version".into());
}
debug!("passthrough_args: {:?}", passthrough_args);
Ok((matches, passthrough_args))
}
fn main() -> Fallible<()> { fn main() -> Fallible<()> {
env_logger::init(); env_logger::init();