1
Fork 0
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:
Kourosh 2021-03-30 23:09:58 +04:30 committed by GitHub
parent a5402eed41
commit 775682508a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 56 deletions

View file

@ -15,7 +15,7 @@ edition = "2018"
path = "src/more.rs" path = "src/more.rs"
[dependencies] [dependencies]
getopts = "0.2.18" clap = "2.33"
uucore = { version = ">=0.0.7", package = "uucore", path = "../../uucore" } uucore = { version = ">=0.0.7", package = "uucore", path = "../../uucore" }
uucore_procs = { version = ">=0.0.5", package = "uucore_procs", path = "../../uucore_procs" } uucore_procs = { version = ">=0.0.5", package = "uucore_procs", path = "../../uucore_procs" }

View file

@ -10,9 +10,8 @@
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use getopts::Options;
use std::fs::File; 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")))] #[cfg(all(unix, not(target_os = "fuchsia")))]
extern crate nix; extern crate nix;
@ -24,70 +23,44 @@ extern crate redox_termios;
#[cfg(target_os = "redox")] #[cfg(target_os = "redox")]
extern crate syscall; extern crate syscall;
#[derive(Clone, Eq, PartialEq)] use clap::{App, Arg, ArgMatches};
pub enum Mode {
More, static VERSION: &str = env!("CARGO_PKG_VERSION");
Help, static ABOUT: &str = "A file perusal filter for CRT viewing.";
Version,
mod options {
pub const FILE: &str = "file";
} }
static NAME: &str = "more"; fn get_usage() -> String {
static VERSION: &str = env!("CARGO_PKG_VERSION"); format!("{} [options] <file>...", executable!())
}
pub fn uumain(args: impl uucore::Args) -> i32 { 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) // FixME: fail without panic for now; but `more` should work with no arguments (ie, for piped input)
if args.len() < 2 { if let None | Some("-") = matches.value_of(options::FILE) {
println!("{}: incorrect usage", args[0]); println!("{}: incorrect usage", executable!());
return 1; return 1;
} }
opts.optflag("h", "help", "display this help and exit"); more(matches);
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(),
}
0 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")))] #[cfg(all(unix, not(target_os = "fuchsia")))]
fn setup_term() -> termios::Termios { fn setup_term() -> termios::Termios {
let mut term = termios::tcgetattr(0).unwrap(); let mut term = termios::tcgetattr(0).unwrap();
@ -138,9 +111,11 @@ fn reset_term(term: &mut redox_termios::Termios) {
let _ = syscall::close(fd); let _ = syscall::close(fd);
} }
fn more(matches: getopts::Matches) { fn more(matches: ArgMatches) {
let files = matches.free; let mut f: Box<dyn BufRead> = match matches.value_of(options::FILE) {
let mut f = File::open(files.first().unwrap()).unwrap(); None | Some("-") => Box::new(BufReader::new(stdin())),
Some(filename) => Box::new(BufReader::new(File::open(filename).unwrap())),
};
let mut buffer = [0; 1024]; let mut buffer = [0; 1024];
let mut term = setup_term(); let mut term = setup_term();