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:
parent
99da9ea6ec
commit
52997b63fb
2 changed files with 66 additions and 79 deletions
|
@ -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" }
|
||||||
|
|
|
@ -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,107 +22,94 @@ 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);
|
show_error!("missing operand\nTry {} --help for more information", NAME);
|
||||||
0
|
false
|
||||||
}
|
} else {
|
||||||
Mode::Version => {
|
true
|
||||||
version();
|
};
|
||||||
0
|
|
||||||
}
|
if res {
|
||||||
_ => {
|
// free strings are path operands
|
||||||
let mut res = if matches.free.is_empty() {
|
// FIXME: TCS, seems inefficient and overly verbose (?)
|
||||||
show_error!("missing operand\nTry {} --help for more information", NAME);
|
for p in paths.unwrap() {
|
||||||
false
|
let mut path = Vec::new();
|
||||||
} else {
|
for path_segment in p.split('/') {
|
||||||
true
|
path.push(path_segment.to_string());
|
||||||
};
|
|
||||||
// free strings are path operands
|
|
||||||
// FIXME: TCS, seems inefficient and overly verbose (?)
|
|
||||||
for p in matches.free {
|
|
||||||
let mut path = Vec::new();
|
|
||||||
for path_segment in p.split('/') {
|
|
||||||
path.push(path_segment.to_string());
|
|
||||||
}
|
|
||||||
res &= check_path(&mode, &path);
|
|
||||||
}
|
|
||||||
// determine error code
|
|
||||||
if res {
|
|
||||||
0
|
|
||||||
} else {
|
|
||||||
1
|
|
||||||
}
|
}
|
||||||
|
res &= check_path(&mode, &path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// print help
|
// determine error code
|
||||||
fn help(opts: Options) {
|
if res {
|
||||||
let msg = format!(
|
0
|
||||||
"Usage: {} [OPTION]... NAME...\n\n\
|
} else {
|
||||||
Diagnose invalid or unportable file names.",
|
1
|
||||||
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue