mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
more: move from getopts to clap (#1962)
This commit is contained in:
parent
a5402eed41
commit
775682508a
2 changed files with 31 additions and 56 deletions
|
@ -15,7 +15,7 @@ edition = "2018"
|
|||
path = "src/more.rs"
|
||||
|
||||
[dependencies]
|
||||
getopts = "0.2.18"
|
||||
clap = "2.33"
|
||||
uucore = { version = ">=0.0.7", package = "uucore", path = "../../uucore" }
|
||||
uucore_procs = { version = ">=0.0.5", package = "uucore_procs", path = "../../uucore_procs" }
|
||||
|
||||
|
|
|
@ -10,9 +10,8 @@
|
|||
#[macro_use]
|
||||
extern crate uucore;
|
||||
|
||||
use getopts::Options;
|
||||
use std::fs::File;
|
||||
use std::io::{stdout, Read, Write};
|
||||
use std::io::{stdin, stdout, BufRead, BufReader, Read, Write};
|
||||
|
||||
#[cfg(all(unix, not(target_os = "fuchsia")))]
|
||||
extern crate nix;
|
||||
|
@ -24,70 +23,44 @@ extern crate redox_termios;
|
|||
#[cfg(target_os = "redox")]
|
||||
extern crate syscall;
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub enum Mode {
|
||||
More,
|
||||
Help,
|
||||
Version,
|
||||
use clap::{App, Arg, ArgMatches};
|
||||
|
||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
static ABOUT: &str = "A file perusal filter for CRT viewing.";
|
||||
|
||||
mod options {
|
||||
pub const FILE: &str = "file";
|
||||
}
|
||||
|
||||
static NAME: &str = "more";
|
||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
fn get_usage() -> String {
|
||||
format!("{} [options] <file>...", executable!())
|
||||
}
|
||||
|
||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||
let args = args.collect_str();
|
||||
let usage = get_usage();
|
||||
|
||||
let mut opts = Options::new();
|
||||
let matches = App::new(executable!())
|
||||
.version(VERSION)
|
||||
.usage(&usage[..])
|
||||
.about(ABOUT)
|
||||
.arg(
|
||||
Arg::with_name(options::FILE)
|
||||
.number_of_values(1)
|
||||
.multiple(true),
|
||||
)
|
||||
.get_matches_from(args);
|
||||
|
||||
// FixME: fail without panic for now; but `more` should work with no arguments (ie, for piped input)
|
||||
if args.len() < 2 {
|
||||
println!("{}: incorrect usage", args[0]);
|
||||
if let None | Some("-") = matches.value_of(options::FILE) {
|
||||
println!("{}: incorrect usage", executable!());
|
||||
return 1;
|
||||
}
|
||||
|
||||
opts.optflag("h", "help", "display this help and exit");
|
||||
opts.optflag("v", "version", "output version information and exit");
|
||||
|
||||
let matches = match opts.parse(&args[1..]) {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
show_error!("{}", e);
|
||||
panic!()
|
||||
}
|
||||
};
|
||||
let usage = opts.usage("more TARGET.");
|
||||
let mode = if matches.opt_present("version") {
|
||||
Mode::Version
|
||||
} else if matches.opt_present("help") {
|
||||
Mode::Help
|
||||
} else {
|
||||
Mode::More
|
||||
};
|
||||
|
||||
match mode {
|
||||
Mode::More => more(matches),
|
||||
Mode::Help => help(&usage),
|
||||
Mode::Version => version(),
|
||||
}
|
||||
more(matches);
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
fn version() {
|
||||
println!("{} {}", NAME, VERSION);
|
||||
}
|
||||
|
||||
fn help(usage: &str) {
|
||||
let msg = format!(
|
||||
"{0} {1}\n\n\
|
||||
Usage: {0} TARGET\n \
|
||||
\n\
|
||||
{2}",
|
||||
NAME, VERSION, usage
|
||||
);
|
||||
println!("{}", msg);
|
||||
}
|
||||
|
||||
#[cfg(all(unix, not(target_os = "fuchsia")))]
|
||||
fn setup_term() -> termios::Termios {
|
||||
let mut term = termios::tcgetattr(0).unwrap();
|
||||
|
@ -138,9 +111,11 @@ fn reset_term(term: &mut redox_termios::Termios) {
|
|||
let _ = syscall::close(fd);
|
||||
}
|
||||
|
||||
fn more(matches: getopts::Matches) {
|
||||
let files = matches.free;
|
||||
let mut f = File::open(files.first().unwrap()).unwrap();
|
||||
fn more(matches: ArgMatches) {
|
||||
let mut f: Box<dyn BufRead> = match matches.value_of(options::FILE) {
|
||||
None | Some("-") => Box::new(BufReader::new(stdin())),
|
||||
Some(filename) => Box::new(BufReader::new(File::open(filename).unwrap())),
|
||||
};
|
||||
let mut buffer = [0; 1024];
|
||||
|
||||
let mut term = setup_term();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue