Well, getting close to 200 lines of code. So I'm going to try and only post what is relevant. I have two functions that read lines of a file. Let's say the lines of the file are like this:
-u test -l 192.168.1.x -w cheese
-u user -l 192.168.1.x -w "superduper"
The arguments in the file are the same as for the program itself. I thought about using args[0], but if there are lots of lines I don't want to overload things with sub-processes. So I'm trying to get the parameters like -u, -l, -w, etc. and pass them to a function I already have that would do the same thing with these arguments. Below are the functions I'm currently using to get the lines of the file:
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn read_it(fname: &str, fexec: &str) {
let args: Vec<String> = env::args().collect();
if let Ok(lines) = read_lines(&fname) {
// Consumes the iterator, returns an (Optional) String
for line in lines {
if let Ok(ip) = line {
let split = ip.split(" ");
let more = fexec.split(" ");
let number = ip.split(" ");
// print values of options.
for s in split.skip(1).step_by(2) {
println!("{}", s);
}
// print commands
for m in more {
println!("{}", m);
}
}
}
}
If you run this with a similar file to read and pass it another argument of "date" you should get output like the following:
$ ./target/release/split_it -f hosts -e date
test
192.168.1.x
cheese
date
-u
-l
-w
ph33r
192.168.1.x
superduper
date
-u
-l
-w
As you may notice, I was able to do a split on empty spaces and then print out the values of -u, -l -w and -e with two loops. Then a third loop prints out the literal parmaters -u -l -w. I need to do this with multiple combinations of arguments, which will probably use a match statement to call different functions I already have.
What I actually need to do and having trouble with:
Some how I need to take the values from the output before and put them into variables or the indexes of a vector or array. Since the number of arguments could be different each time, I'm guessing a vector would be best. I'm just not sure if I need to pass the parameters like -u -l -w into a vector, or if I could have a match statement check those as-is. If they can't be used as-is I know I can read those values in with vec.push() within those for loops. I'm just unsure how to use the indexes of a vector with a match statement as well.
I also know there may be another solution to this I'm not thinking of. If anyone has suggestions on the best way to do this I would be grateful.
Thanks