forked from bonsai/harakit
Makefile, fop(1): added argument parsing and -d option
This commit is contained in:
parent
91e129b813
commit
52def0a32d
2
Makefile
2
Makefile
@ -65,7 +65,7 @@ sysexits: build
|
|||||||
|
|
||||||
libgetopt: src/getopt-rs/lib.rs
|
libgetopt: src/getopt-rs/lib.rs
|
||||||
$(RUSTC) $(RUSTCFLAGS) --crate-type=lib --crate-name=getopt \
|
$(RUSTC) $(RUSTCFLAGS) --crate-type=lib --crate-name=getopt \
|
||||||
-o build/o/libgetopt.rlib src/lib.rs
|
-o build/o/libgetopt.rlib src/getopt-rs/lib.rs
|
||||||
|
|
||||||
false: src/false.rs build
|
false: src/false.rs build
|
||||||
$(RUSTC) $(RUSTFLAGS) -o build/bin/false src/false.rs
|
$(RUSTC) $(RUSTFLAGS) -o build/bin/false src/false.rs
|
||||||
|
37
src/fop.rs
37
src/fop.rs
@ -18,33 +18,54 @@
|
|||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
env::args,
|
env::args,
|
||||||
io::{ stdin, Write },
|
io::{ Read, stdin, Write },
|
||||||
process::{ Command, exit, Stdio },
|
process::{ Command, exit, Stdio },
|
||||||
};
|
};
|
||||||
|
|
||||||
extern crate sysexits;
|
extern crate sysexits;
|
||||||
|
extern crate getopt;
|
||||||
|
|
||||||
|
use getopt::{ Opt, Parser };
|
||||||
use sysexits::{ EX_DATAERR, EX_USAGE };
|
use sysexits::{ EX_DATAERR, EX_USAGE };
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<String>>();
|
||||||
|
let mut d = '␞';
|
||||||
|
let mut arg_parser = Parser::new(&argv, "d:");
|
||||||
|
|
||||||
argv.get(2).unwrap_or_else(|| {
|
while let Some(opt) = arg_parser.next() {
|
||||||
|
match opt {
|
||||||
|
Ok(Opt('d', Some(arg))) => {
|
||||||
|
let arg_char = arg.chars().collect::<Vec<char>>();
|
||||||
|
if arg_char.len() > 1 {
|
||||||
|
eprintln!("{}: {}: Not a character.", argv[0], arg);
|
||||||
|
exit(EX_USAGE);
|
||||||
|
} else { d = arg_char[0]; }
|
||||||
|
},
|
||||||
|
_ => {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let index_arg = arg_parser.index();
|
||||||
|
let command_arg = arg_parser.index() + 1;
|
||||||
|
|
||||||
|
argv.get(command_arg).unwrap_or_else(|| {
|
||||||
eprintln!("Usage: {} index command [args...]", argv[0]);
|
eprintln!("Usage: {} index command [args...]", argv[0]);
|
||||||
exit(EX_USAGE);
|
exit(EX_USAGE);
|
||||||
});
|
});
|
||||||
|
|
||||||
let index = argv[1].parse::<usize>().unwrap_or_else(|_| {
|
let index = argv[index_arg].parse::<usize>().unwrap_or_else(|_| {
|
||||||
eprintln!("{}: {}: Not an integer.", argv[0], argv[1]);
|
eprintln!("{}: {}: Not an integer.", argv[0], argv[1]);
|
||||||
exit(EX_DATAERR);
|
exit(EX_DATAERR);
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
stdin().read_line(&mut buf).unwrap();
|
stdin().read_to_string(&mut buf).unwrap();
|
||||||
let mut fields = buf.split('␞').collect::<Vec<&str>>();
|
let mut fields = buf.split(d).collect::<Vec<&str>>();
|
||||||
|
|
||||||
let opts = argv.iter().clone().skip(3).collect::<Vec<&String>>();
|
let opts = argv.iter().clone().skip(command_arg + 1).collect::<Vec<&String>>();
|
||||||
|
|
||||||
let mut spawned = Command::new(argv.get(2).unwrap())
|
let mut spawned = Command::new(argv.get(command_arg).unwrap())
|
||||||
.args(opts)
|
.args(opts)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
@ -71,5 +92,5 @@ fn main() {
|
|||||||
|
|
||||||
fields[index] = &new_field;
|
fields[index] = &new_field;
|
||||||
|
|
||||||
print!("{}", fields.join("␞"));
|
print!("{}", fields.join(&d.to_string()));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user