1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

pathchk: move from getopts to clap (#1897)

This commit is contained in:
Sivachandran 2021-03-25 15:57:34 +05:30 committed by GitHub
parent 99da9ea6ec
commit 52997b63fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 79 deletions

View file

@ -15,7 +15,7 @@ edition = "2018"
path = "src/pathchk.rs" path = "src/pathchk.rs"
[dependencies] [dependencies]
getopts = "0.2.18" clap = "2.33"
libc = "0.2.42" libc = "0.2.42"
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

@ -12,7 +12,7 @@
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use getopts::Options; use clap::{App, Arg};
use std::fs; use std::fs;
use std::io::{ErrorKind, Write}; use std::io::{ErrorKind, Write};
@ -22,83 +22,88 @@ enum Mode {
Basic, // check basic compatibility with POSIX Basic, // check basic compatibility with POSIX
Extra, // check for leading dashes and empty names Extra, // check for leading dashes and empty names
Both, // a combination of `Basic` and `Extra` Both, // a combination of `Basic` and `Extra`
Help, // show help
Version, // show version information
} }
static NAME: &str = "pathchk"; static NAME: &str = "pathchk";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "Check whether file names are valid or portable";
mod options {
pub const POSIX: &str = "posix";
pub const POSIX_SPECIAL: &str = "posix-special";
pub const PORTABILITY: &str = "portability";
pub const PATH: &str = "path";
}
// a few global constants as used in the GNU implementation // a few global constants as used in the GNU implementation
const POSIX_PATH_MAX: usize = 256; const POSIX_PATH_MAX: usize = 256;
const POSIX_NAME_MAX: usize = 14; const POSIX_NAME_MAX: usize = 14;
pub fn uumain(args: impl uucore::Args) -> i32 { fn get_usage() -> String {
let args = args.collect_str(); format!("{0} [OPTION]... NAME...", executable!())
}
// add options pub fn uumain(args: impl uucore::Args) -> i32 {
let mut opts = Options::new(); let usage = get_usage();
opts.optflag("p", "posix", "check for (most) POSIX systems");
opts.optflag( let matches = App::new(executable!())
"P", .version(VERSION)
"posix-special", .about(ABOUT)
"check for empty names and leading \"-\"", .usage(&usage[..])
); .arg(
opts.optflag( Arg::with_name(options::POSIX)
"", .short("p")
"portability", .help("check for most POSIX systems"),
"check for all POSIX systems (equivalent to -p -P)", )
); .arg(
opts.optflag("h", "help", "display this help text and exit"); Arg::with_name(options::POSIX_SPECIAL)
opts.optflag("V", "version", "output version information and exit"); .short("P")
let matches = match opts.parse(&args[1..]) { .help(r#"check for empty names and leading "-""#),
Ok(m) => m, )
Err(e) => crash!(1, "{}", e), .arg(
}; Arg::with_name(options::PORTABILITY)
.long(options::PORTABILITY)
.help("check for all POSIX systems (equivalent to -p -P)"),
)
.arg(Arg::with_name(options::PATH).hidden(true).multiple(true))
.get_matches_from(args);
// set working mode // set working mode
let mode = if matches.opt_present("version") { let is_posix = matches.values_of(options::POSIX).is_some();
Mode::Version let is_posix_special = matches.values_of(options::POSIX_SPECIAL).is_some();
} else if matches.opt_present("help") { let is_portability = matches.values_of(options::PORTABILITY).is_some();
Mode::Help
} else if (matches.opt_present("posix") && matches.opt_present("posix-special")) let mode = if (is_posix && is_posix_special) || is_portability {
|| matches.opt_present("portability")
{
Mode::Both Mode::Both
} else if matches.opt_present("posix") { } else if is_posix {
Mode::Basic Mode::Basic
} else if matches.opt_present("posix-special") { } else if is_posix_special {
Mode::Extra Mode::Extra
} else { } else {
Mode::Default Mode::Default
}; };
// take necessary actions // take necessary actions
match mode { let paths = matches.values_of(options::PATH);
Mode::Help => { let mut res = if paths.is_none() {
help(opts);
0
}
Mode::Version => {
version();
0
}
_ => {
let mut res = if matches.free.is_empty() {
show_error!("missing operand\nTry {} --help for more information", NAME); show_error!("missing operand\nTry {} --help for more information", NAME);
false false
} else { } else {
true true
}; };
if res {
// free strings are path operands // free strings are path operands
// FIXME: TCS, seems inefficient and overly verbose (?) // FIXME: TCS, seems inefficient and overly verbose (?)
for p in matches.free { for p in paths.unwrap() {
let mut path = Vec::new(); let mut path = Vec::new();
for path_segment in p.split('/') { for path_segment in p.split('/') {
path.push(path_segment.to_string()); path.push(path_segment.to_string());
} }
res &= check_path(&mode, &path); res &= check_path(&mode, &path);
} }
}
// determine error code // determine error code
if res { if res {
0 0
@ -106,24 +111,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
1 1
} }
} }
}
}
// print help
fn help(opts: Options) {
let msg = format!(
"Usage: {} [OPTION]... NAME...\n\n\
Diagnose invalid or unportable file names.",
NAME
);
print!("{}", opts.usage(&msg));
}
// print version information
fn version() {
println!("{} {}", NAME, VERSION);
}
// check a path, given as a slice of it's components and an operating mode // check a path, given as a slice of it's components and an operating mode
fn check_path(mode: &Mode, path: &[String]) -> bool { fn check_path(mode: &Mode, path: &[String]) -> bool {