1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

Merge pull request #1767 from FelipeLema/mod_opt

Group OPT_ARGUMENTS into mods
This commit is contained in:
Sylvestre Ledru 2021-03-09 22:57:30 +01:00 committed by GitHub
commit 517b2a6a22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 326 additions and 283 deletions

View file

@ -27,20 +27,30 @@ use std::path::Path;
static ABOUT: &str = "change file owner and group"; static ABOUT: &str = "change file owner and group";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static OPT_CHANGES: &str = "changes"; pub mod options {
static OPT_DEREFERENCE: &str = "dereference"; pub mod verbosity {
static OPT_NO_DEREFERENCE: &str = "no-dereference"; pub static CHANGES: &str = "changes";
static OPT_FROM: &str = "from"; pub static QUIET: &str = "quiet";
static OPT_PRESERVE_ROOT: &str = "preserve-root"; pub static SILENT: &str = "silent";
static OPT_NO_PRESERVE_ROOT: &str = "no-preserve-root"; pub static VERBOSE: &str = "verbose";
static OPT_QUIET: &str = "quiet"; }
static OPT_RECURSIVE: &str = "recursive"; pub mod preserve_root {
static OPT_REFERENCE: &str = "reference"; pub static PRESERVE: &str = "preserve-root";
static OPT_SILENT: &str = "silent"; pub static NO_PRESERVE: &str = "no-preserve-root";
static OPT_TRAVERSE: &str = "H"; }
static OPT_NO_TRAVERSE: &str = "P"; pub mod dereference {
static OPT_TRAVERSE_EVERY: &str = "L"; pub static DEREFERENCE: &str = "dereference";
static OPT_VERBOSE: &str = "verbose"; pub static NO_DEREFERENCE: &str = "no-dereference";
}
pub static FROM: &str = "from";
pub static RECURSIVE: &str = "recursive";
pub mod traverse {
pub static TRAVERSE: &str = "H";
pub static NO_TRAVERSE: &str = "P";
pub static EVERY: &str = "L";
}
pub static REFERENCE: &str = "reference";
}
static ARG_OWNER: &str = "owner"; static ARG_OWNER: &str = "owner";
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -66,80 +76,80 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.about(ABOUT) .about(ABOUT)
.usage(&usage[..]) .usage(&usage[..])
.arg( .arg(
Arg::with_name(OPT_CHANGES) Arg::with_name(options::verbosity::CHANGES)
.short("c") .short("c")
.long(OPT_CHANGES) .long(options::verbosity::CHANGES)
.help("like verbose but report only when a change is made"), .help("like verbose but report only when a change is made"),
) )
.arg(Arg::with_name(OPT_DEREFERENCE).long(OPT_DEREFERENCE).help( .arg(Arg::with_name(options::dereference::DEREFERENCE).long(options::dereference::DEREFERENCE).help(
"affect the referent of each symbolic link (this is the default), rather than the symbolic link itself", "affect the referent of each symbolic link (this is the default), rather than the symbolic link itself",
)) ))
.arg( .arg(
Arg::with_name(OPT_NO_DEREFERENCE) Arg::with_name(options::dereference::NO_DEREFERENCE)
.short("h") .short("h")
.long(OPT_NO_DEREFERENCE) .long(options::dereference::NO_DEREFERENCE)
.help( .help(
"affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)", "affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)",
), ),
) )
.arg( .arg(
Arg::with_name(OPT_FROM) Arg::with_name(options::FROM)
.long(OPT_FROM) .long(options::FROM)
.help( .help(
"change the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute", "change the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute",
) )
.value_name("CURRENT_OWNER:CURRENT_GROUP"), .value_name("CURRENT_OWNER:CURRENT_GROUP"),
) )
.arg( .arg(
Arg::with_name(OPT_PRESERVE_ROOT) Arg::with_name(options::preserve_root::PRESERVE)
.long(OPT_PRESERVE_ROOT) .long(options::preserve_root::PRESERVE)
.help("fail to operate recursively on '/'"), .help("fail to operate recursively on '/'"),
) )
.arg( .arg(
Arg::with_name(OPT_NO_PRESERVE_ROOT) Arg::with_name(options::preserve_root::NO_PRESERVE)
.long(OPT_NO_PRESERVE_ROOT) .long(options::preserve_root::NO_PRESERVE)
.help("do not treat '/' specially (the default)"), .help("do not treat '/' specially (the default)"),
) )
.arg( .arg(
Arg::with_name(OPT_QUIET) Arg::with_name(options::verbosity::QUIET)
.long(OPT_QUIET) .long(options::verbosity::QUIET)
.help("suppress most error messages"), .help("suppress most error messages"),
) )
.arg( .arg(
Arg::with_name(OPT_RECURSIVE) Arg::with_name(options::RECURSIVE)
.short("R") .short("R")
.long(OPT_RECURSIVE) .long(options::RECURSIVE)
.help("operate on files and directories recursively"), .help("operate on files and directories recursively"),
) )
.arg( .arg(
Arg::with_name(OPT_REFERENCE) Arg::with_name(options::REFERENCE)
.long(OPT_REFERENCE) .long(options::REFERENCE)
.help("use RFILE's owner and group rather than specifying OWNER:GROUP values") .help("use RFILE's owner and group rather than specifying OWNER:GROUP values")
.value_name("RFILE") .value_name("RFILE")
.min_values(1), .min_values(1),
) )
.arg(Arg::with_name(OPT_SILENT).short("f").long(OPT_SILENT)) .arg(Arg::with_name(options::verbosity::SILENT).short("f").long(options::verbosity::SILENT))
.arg( .arg(
Arg::with_name(OPT_TRAVERSE) Arg::with_name(options::traverse::TRAVERSE)
.short(OPT_TRAVERSE) .short(options::traverse::TRAVERSE)
.help("if a command line argument is a symbolic link to a directory, traverse it") .help("if a command line argument is a symbolic link to a directory, traverse it")
.overrides_with_all(&[OPT_TRAVERSE_EVERY, OPT_NO_TRAVERSE]), .overrides_with_all(&[options::traverse::EVERY, options::traverse::NO_TRAVERSE]),
) )
.arg( .arg(
Arg::with_name(OPT_TRAVERSE_EVERY) Arg::with_name(options::traverse::EVERY)
.short(OPT_TRAVERSE_EVERY) .short(options::traverse::EVERY)
.help("traverse every symbolic link to a directory encountered") .help("traverse every symbolic link to a directory encountered")
.overrides_with_all(&[OPT_TRAVERSE, OPT_NO_TRAVERSE]), .overrides_with_all(&[options::traverse::TRAVERSE, options::traverse::NO_TRAVERSE]),
) )
.arg( .arg(
Arg::with_name(OPT_NO_TRAVERSE) Arg::with_name(options::traverse::NO_TRAVERSE)
.short(OPT_NO_TRAVERSE) .short(options::traverse::NO_TRAVERSE)
.help("do not traverse any symbolic links (default)") .help("do not traverse any symbolic links (default)")
.overrides_with_all(&[OPT_TRAVERSE, OPT_TRAVERSE_EVERY]), .overrides_with_all(&[options::traverse::TRAVERSE, options::traverse::EVERY]),
) )
.arg( .arg(
Arg::with_name(OPT_VERBOSE) Arg::with_name(options::verbosity::VERBOSE)
.long(OPT_VERBOSE) .long(options::verbosity::VERBOSE)
.help("output a diagnostic for every file processed"), .help("output a diagnostic for every file processed"),
) )
.arg( .arg(
@ -166,23 +176,23 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.map(|v| v.map(ToString::to_string).collect()) .map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default(); .unwrap_or_default();
let preserve_root = matches.is_present(OPT_PRESERVE_ROOT); let preserve_root = matches.is_present(options::preserve_root::PRESERVE);
let mut derefer = if matches.is_present(OPT_NO_DEREFERENCE) { let mut derefer = if matches.is_present(options::dereference::NO_DEREFERENCE) {
1 1
} else { } else {
0 0
}; };
let mut bit_flag = if matches.is_present(OPT_TRAVERSE) { let mut bit_flag = if matches.is_present(options::traverse::TRAVERSE) {
FTS_COMFOLLOW | FTS_PHYSICAL FTS_COMFOLLOW | FTS_PHYSICAL
} else if matches.is_present(OPT_TRAVERSE_EVERY) { } else if matches.is_present(options::traverse::EVERY) {
FTS_LOGICAL FTS_LOGICAL
} else { } else {
FTS_PHYSICAL FTS_PHYSICAL
}; };
let recursive = matches.is_present(OPT_RECURSIVE); let recursive = matches.is_present(options::RECURSIVE);
if recursive { if recursive {
if bit_flag == FTS_PHYSICAL { if bit_flag == FTS_PHYSICAL {
if derefer == 1 { if derefer == 1 {
@ -195,17 +205,19 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
bit_flag = FTS_PHYSICAL; bit_flag = FTS_PHYSICAL;
} }
let verbosity = if matches.is_present(OPT_CHANGES) { let verbosity = if matches.is_present(options::verbosity::CHANGES) {
Verbosity::Changes Verbosity::Changes
} else if matches.is_present(OPT_SILENT) || matches.is_present(OPT_QUIET) { } else if matches.is_present(options::verbosity::SILENT)
|| matches.is_present(options::verbosity::QUIET)
{
Verbosity::Silent Verbosity::Silent
} else if matches.is_present(OPT_VERBOSE) { } else if matches.is_present(options::verbosity::VERBOSE) {
Verbosity::Verbose Verbosity::Verbose
} else { } else {
Verbosity::Normal Verbosity::Normal
}; };
let filter = if let Some(spec) = matches.value_of(OPT_FROM) { let filter = if let Some(spec) = matches.value_of(options::FROM) {
match parse_spec(&spec) { match parse_spec(&spec) {
Ok((Some(uid), None)) => IfFrom::User(uid), Ok((Some(uid), None)) => IfFrom::User(uid),
Ok((None, Some(gid))) => IfFrom::Group(gid), Ok((None, Some(gid))) => IfFrom::Group(gid),
@ -222,7 +234,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let dest_uid: Option<u32>; let dest_uid: Option<u32>;
let dest_gid: Option<u32>; let dest_gid: Option<u32>;
if let Some(file) = matches.value_of(OPT_REFERENCE) { if let Some(file) = matches.value_of(options::REFERENCE) {
match fs::metadata(&file) { match fs::metadata(&file) {
Ok(meta) => { Ok(meta) => {
dest_gid = Some(meta.gid()); dest_gid = Some(meta.gid());

View file

@ -87,11 +87,13 @@ macro_rules! print_adjusted {
static ABOUT: &str = "Display file or file system status."; static ABOUT: &str = "Display file or file system status.";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static OPT_DEREFERENCE: &str = "dereference"; pub mod options {
static OPT_FILE_SYSTEM: &str = "file-system"; pub static DEREFERENCE: &str = "dereference";
static OPT_FORMAT: &str = "format"; pub static FILE_SYSTEM: &str = "file-system";
static OPT_PRINTF: &str = "printf"; pub static FORMAT: &str = "format";
static OPT_TERSE: &str = "terse"; pub static PRINTF: &str = "printf";
pub static TERSE: &str = "terse";
}
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -464,15 +466,17 @@ impl Stater {
.map(|v| v.map(ToString::to_string).collect()) .map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default(); .unwrap_or_default();
let fmtstr = if matches.is_present(OPT_PRINTF) { let fmtstr = if matches.is_present(options::PRINTF) {
matches.value_of(OPT_PRINTF).expect("Invalid format string") matches
.value_of(options::PRINTF)
.expect("Invalid format string")
} else { } else {
matches.value_of(OPT_FORMAT).unwrap_or("") matches.value_of(options::FORMAT).unwrap_or("")
}; };
let use_printf = matches.is_present(OPT_PRINTF); let use_printf = matches.is_present(options::PRINTF);
let terse = matches.is_present(OPT_TERSE); let terse = matches.is_present(options::TERSE);
let showfs = matches.is_present(OPT_FILE_SYSTEM); let showfs = matches.is_present(options::FILE_SYSTEM);
let default_tokens = if fmtstr.is_empty() { let default_tokens = if fmtstr.is_empty() {
Stater::generate_tokens(&Stater::default_fmt(showfs, terse, false), use_printf).unwrap() Stater::generate_tokens(&Stater::default_fmt(showfs, terse, false), use_printf).unwrap()
@ -501,7 +505,7 @@ impl Stater {
}; };
Ok(Stater { Ok(Stater {
follow: matches.is_present(OPT_DEREFERENCE), follow: matches.is_present(options::DEREFERENCE),
showfs, showfs,
from_user: !fmtstr.is_empty(), from_user: !fmtstr.is_empty(),
files, files,
@ -955,27 +959,27 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.usage(&usage[..]) .usage(&usage[..])
.after_help(&long_usage[..]) .after_help(&long_usage[..])
.arg( .arg(
Arg::with_name(OPT_DEREFERENCE) Arg::with_name(options::DEREFERENCE)
.short("L") .short("L")
.long(OPT_DEREFERENCE) .long(options::DEREFERENCE)
.help("follow links"), .help("follow links"),
) )
.arg( .arg(
Arg::with_name(OPT_FILE_SYSTEM) Arg::with_name(options::FILE_SYSTEM)
.short("f") .short("f")
.long(OPT_FILE_SYSTEM) .long(options::FILE_SYSTEM)
.help("display file system status instead of file status"), .help("display file system status instead of file status"),
) )
.arg( .arg(
Arg::with_name(OPT_TERSE) Arg::with_name(options::TERSE)
.short("t") .short("t")
.long(OPT_TERSE) .long(options::TERSE)
.help("print the information in terse form"), .help("print the information in terse form"),
) )
.arg( .arg(
Arg::with_name(OPT_FORMAT) Arg::with_name(options::FORMAT)
.short("c") .short("c")
.long(OPT_FORMAT) .long(options::FORMAT)
.help( .help(
"use the specified FORMAT instead of the default; "use the specified FORMAT instead of the default;
output a newline after each use of FORMAT", output a newline after each use of FORMAT",
@ -983,8 +987,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.value_name("FORMAT"), .value_name("FORMAT"),
) )
.arg( .arg(
Arg::with_name(OPT_PRINTF) Arg::with_name(options::PRINTF)
.long(OPT_PRINTF) .long(options::PRINTF)
.value_name("FORMAT") .value_name("FORMAT")
.help( .help(
"like --format, but interpret backslash escapes, "like --format, but interpret backslash escapes,

View file

@ -19,8 +19,10 @@ static EXIT_ERR: i32 = 1;
static ABOUT: &str = "Synchronize cached writes to persistent storage"; static ABOUT: &str = "Synchronize cached writes to persistent storage";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static OPT_FILE_SYSTEM: &str = "file-system"; pub mod options {
static OPT_DATA: &str = "data"; pub static FILE_SYSTEM: &str = "file-system";
pub static DATA: &str = "data";
}
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -170,17 +172,17 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.about(ABOUT) .about(ABOUT)
.usage(&usage[..]) .usage(&usage[..])
.arg( .arg(
Arg::with_name(OPT_FILE_SYSTEM) Arg::with_name(options::FILE_SYSTEM)
.short("f") .short("f")
.long(OPT_FILE_SYSTEM) .long(options::FILE_SYSTEM)
.conflicts_with(OPT_DATA) .conflicts_with(options::DATA)
.help("sync the file systems that contain the files (Linux and Windows only)"), .help("sync the file systems that contain the files (Linux and Windows only)"),
) )
.arg( .arg(
Arg::with_name(OPT_DATA) Arg::with_name(options::DATA)
.short("d") .short("d")
.long(OPT_DATA) .long(options::DATA)
.conflicts_with(OPT_FILE_SYSTEM) .conflicts_with(options::FILE_SYSTEM)
.help("sync only file data, no unneeded metadata (Linux only)"), .help("sync only file data, no unneeded metadata (Linux only)"),
) )
.arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true))
@ -197,10 +199,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
} }
} }
if matches.is_present(OPT_FILE_SYSTEM) { if matches.is_present(options::FILE_SYSTEM) {
#[cfg(any(target_os = "linux", target_os = "windows"))] #[cfg(any(target_os = "linux", target_os = "windows"))]
syncfs(files); syncfs(files);
} else if matches.is_present(OPT_DATA) { } else if matches.is_present(options::DATA) {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fdatasync(files); fdatasync(files);
} else { } else {

View file

@ -27,15 +27,19 @@ use std::path::Path;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
static OPT_BYTES: &str = "bytes"; pub mod options {
static OPT_FOLLOW: &str = "follow"; pub mod verbosity {
static OPT_LINES: &str = "lines"; pub static QUIET: &str = "quiet";
static OPT_PID: &str = "pid"; pub static SILENT: &str = "silent";
static OPT_QUIET: &str = "quiet"; pub static VERBOSE: &str = "verbose";
static OPT_SILENT: &str = "silent"; }
static OPT_SLEEP_INT: &str = "sleep-interval"; pub static BYTES: &str = "bytes";
static OPT_VERBOSE: &str = "verbose"; pub static FOLLOW: &str = "follow";
static OPT_ZERO_TERM: &str = "zero-terminated"; pub static LINES: &str = "lines";
pub static PID: &str = "pid";
pub static SLEEP_INT: &str = "sleep-interval";
pub static ZERO_TERM: &str = "zero-terminated";
}
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -72,58 +76,58 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.version(crate_version!()) .version(crate_version!())
.about("output the last part of files") .about("output the last part of files")
.arg( .arg(
Arg::with_name(OPT_BYTES) Arg::with_name(options::BYTES)
.short("c") .short("c")
.long(OPT_BYTES) .long(options::BYTES)
.takes_value(true) .takes_value(true)
.help("Number of bytes to print"), .help("Number of bytes to print"),
) )
.arg( .arg(
Arg::with_name(OPT_FOLLOW) Arg::with_name(options::FOLLOW)
.short("f") .short("f")
.long(OPT_FOLLOW) .long(options::FOLLOW)
.help("Print the file as it grows"), .help("Print the file as it grows"),
) )
.arg( .arg(
Arg::with_name(OPT_LINES) Arg::with_name(options::LINES)
.short("n") .short("n")
.long(OPT_LINES) .long(options::LINES)
.takes_value(true) .takes_value(true)
.help("Number of lines to print"), .help("Number of lines to print"),
) )
.arg( .arg(
Arg::with_name(OPT_PID) Arg::with_name(options::PID)
.long(OPT_PID) .long(options::PID)
.takes_value(true) .takes_value(true)
.help("with -f, terminate after process ID, PID dies"), .help("with -f, terminate after process ID, PID dies"),
) )
.arg( .arg(
Arg::with_name(OPT_QUIET) Arg::with_name(options::verbosity::QUIET)
.short("q") .short("q")
.long(OPT_QUIET) .long(options::verbosity::QUIET)
.help("never output headers giving file names"), .help("never output headers giving file names"),
) )
.arg( .arg(
Arg::with_name(OPT_SILENT) Arg::with_name(options::verbosity::SILENT)
.long(OPT_SILENT) .long(options::verbosity::SILENT)
.help("synonym of --quiet"), .help("synonym of --quiet"),
) )
.arg( .arg(
Arg::with_name(OPT_SLEEP_INT) Arg::with_name(options::SLEEP_INT)
.short("s") .short("s")
.long(OPT_SLEEP_INT) .long(options::SLEEP_INT)
.help("Number or seconds to sleep between polling the file when running with -f"), .help("Number or seconds to sleep between polling the file when running with -f"),
) )
.arg( .arg(
Arg::with_name(OPT_VERBOSE) Arg::with_name(options::verbosity::VERBOSE)
.short("v") .short("v")
.long(OPT_VERBOSE) .long(options::verbosity::VERBOSE)
.help("always output headers giving file names"), .help("always output headers giving file names"),
) )
.arg( .arg(
Arg::with_name(OPT_ZERO_TERM) Arg::with_name(options::ZERO_TERM)
.short("z") .short("z")
.long(OPT_ZERO_TERM) .long(options::ZERO_TERM)
.help("Line delimiter is NUL, not newline"), .help("Line delimiter is NUL, not newline"),
) )
.arg( .arg(
@ -135,9 +139,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let matches = app.get_matches_from(args); let matches = app.get_matches_from(args);
settings.follow = matches.is_present(OPT_FOLLOW); settings.follow = matches.is_present(options::FOLLOW);
if settings.follow { if settings.follow {
if let Some(n) = matches.value_of(OPT_SLEEP_INT) { if let Some(n) = matches.value_of(options::SLEEP_INT) {
let parsed: Option<u32> = n.parse().ok(); let parsed: Option<u32> = n.parse().ok();
if let Some(m) = parsed { if let Some(m) = parsed {
settings.sleep_msec = m * 1000 settings.sleep_msec = m * 1000
@ -145,7 +149,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
} }
} }
if let Some(pid_str) = matches.value_of(OPT_PID) { if let Some(pid_str) = matches.value_of(options::PID) {
if let Ok(pid) = pid_str.parse() { if let Ok(pid) = pid_str.parse() {
settings.pid = pid; settings.pid = pid;
if pid != 0 { if pid != 0 {
@ -161,7 +165,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
} }
} }
match matches.value_of(OPT_LINES) { match matches.value_of(options::LINES) {
Some(n) => { Some(n) => {
let mut slice: &str = n; let mut slice: &str = n;
if slice.chars().next().unwrap_or('_') == '+' { if slice.chars().next().unwrap_or('_') == '+' {
@ -177,7 +181,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
} }
} }
None => { None => {
if let Some(n) = matches.value_of(OPT_BYTES) { if let Some(n) = matches.value_of(options::BYTES) {
let mut slice: &str = n; let mut slice: &str = n;
if slice.chars().next().unwrap_or('_') == '+' { if slice.chars().next().unwrap_or('_') == '+' {
settings.beginning = true; settings.beginning = true;
@ -194,14 +198,15 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
} }
}; };
if matches.is_present(OPT_ZERO_TERM) { if matches.is_present(options::ZERO_TERM) {
if let FilterMode::Lines(count, _) = settings.mode { if let FilterMode::Lines(count, _) = settings.mode {
settings.mode = FilterMode::Lines(count, 0); settings.mode = FilterMode::Lines(count, 0);
} }
} }
let verbose = matches.is_present(OPT_VERBOSE); let verbose = matches.is_present(options::verbosity::VERBOSE);
let quiet = matches.is_present(OPT_QUIET) || matches.is_present(OPT_SILENT); let quiet = matches.is_present(options::verbosity::QUIET)
|| matches.is_present(options::verbosity::SILENT);
let files: Vec<String> = matches let files: Vec<String> = matches
.values_of(ARG_FILES) .values_of(ARG_FILES)

View file

@ -21,14 +21,18 @@ use std::path::Path;
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "Update the access and modification times of each FILE to the current time."; static ABOUT: &str = "Update the access and modification times of each FILE to the current time.";
static OPT_ACCESS: &str = "access"; pub mod options {
static OPT_CURRENT: &str = "current"; pub mod sources {
static OPT_DATE: &str = "date"; pub static DATE: &str = "date";
static OPT_MODIFICATION: &str = "modification"; pub static REFERENCE: &str = "reference";
static OPT_NO_CREATE: &str = "no-create"; pub static CURRENT: &str = "current";
static OPT_NO_DEREF: &str = "no-dereference"; }
static OPT_REFERENCE: &str = "reference"; pub static ACCESS: &str = "access";
static OPT_TIME: &str = "time"; pub static MODIFICATION: &str = "modification";
pub static NO_CREATE: &str = "no-create";
pub static NO_DEREF: &str = "no-dereference";
pub static TIME: &str = "time";
}
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -62,54 +66,54 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.about(ABOUT) .about(ABOUT)
.usage(&usage[..]) .usage(&usage[..])
.arg( .arg(
Arg::with_name(OPT_ACCESS) Arg::with_name(options::ACCESS)
.short("a") .short("a")
.help("change only the access time"), .help("change only the access time"),
) )
.arg( .arg(
Arg::with_name(OPT_CURRENT) Arg::with_name(options::sources::CURRENT)
.short("t") .short("t")
.help("use [[CC]YY]MMDDhhmm[.ss] instead of the current time") .help("use [[CC]YY]MMDDhhmm[.ss] instead of the current time")
.value_name("STAMP") .value_name("STAMP")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
Arg::with_name(OPT_DATE) Arg::with_name(options::sources::DATE)
.short("d") .short("d")
.long(OPT_DATE) .long(options::sources::DATE)
.help("parse argument and use it instead of current time") .help("parse argument and use it instead of current time")
.value_name("STRING"), .value_name("STRING"),
) )
.arg( .arg(
Arg::with_name(OPT_MODIFICATION) Arg::with_name(options::MODIFICATION)
.short("m") .short("m")
.help("change only the modification time"), .help("change only the modification time"),
) )
.arg( .arg(
Arg::with_name(OPT_NO_CREATE) Arg::with_name(options::NO_CREATE)
.short("c") .short("c")
.long(OPT_NO_CREATE) .long(options::NO_CREATE)
.help("do not create any files"), .help("do not create any files"),
) )
.arg( .arg(
Arg::with_name(OPT_NO_DEREF) Arg::with_name(options::NO_DEREF)
.short("h") .short("h")
.long(OPT_NO_DEREF) .long(options::NO_DEREF)
.help( .help(
"affect each symbolic link instead of any referenced file \ "affect each symbolic link instead of any referenced file \
(only for systems that can change the timestamps of a symlink)", (only for systems that can change the timestamps of a symlink)",
), ),
) )
.arg( .arg(
Arg::with_name(OPT_REFERENCE) Arg::with_name(options::sources::REFERENCE)
.short("r") .short("r")
.long(OPT_REFERENCE) .long(options::sources::REFERENCE)
.help("use this file's times instead of the current time") .help("use this file's times instead of the current time")
.value_name("FILE"), .value_name("FILE"),
) )
.arg( .arg(
Arg::with_name(OPT_TIME) Arg::with_name(options::TIME)
.long(OPT_TIME) .long(options::TIME)
.help( .help(
"change only the specified time: \"access\", \"atime\", or \ "change only the specified time: \"access\", \"atime\", or \
\"use\" are equivalent to -a; \"modify\" or \"mtime\" are \ \"use\" are equivalent to -a; \"modify\" or \"mtime\" are \
@ -132,26 +136,36 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.map(|v| v.map(ToString::to_string).collect()) .map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default(); .unwrap_or_default();
if matches.is_present(OPT_DATE) if matches.is_present(options::sources::DATE)
&& (matches.is_present(OPT_REFERENCE) || matches.is_present(OPT_CURRENT)) && (matches.is_present(options::sources::REFERENCE)
|| matches.is_present(OPT_REFERENCE) || matches.is_present(options::sources::CURRENT))
&& (matches.is_present(OPT_DATE) || matches.is_present(OPT_CURRENT)) || matches.is_present(options::sources::REFERENCE)
|| matches.is_present(OPT_CURRENT) && (matches.is_present(options::sources::DATE)
&& (matches.is_present(OPT_DATE) || matches.is_present(OPT_REFERENCE)) || matches.is_present(options::sources::CURRENT))
|| matches.is_present(options::sources::CURRENT)
&& (matches.is_present(options::sources::DATE)
|| matches.is_present(options::sources::REFERENCE))
{ {
panic!("Invalid options: cannot specify reference time from more than one source"); panic!("Invalid options: cannot specify reference time from more than one source");
} }
let (mut atime, mut mtime) = if matches.is_present(OPT_REFERENCE) { let (mut atime, mut mtime) = if matches.is_present(options::sources::REFERENCE) {
stat( stat(
&matches.value_of(OPT_REFERENCE).unwrap()[..], &matches.value_of(options::sources::REFERENCE).unwrap()[..],
!matches.is_present(OPT_NO_DEREF), !matches.is_present(options::NO_DEREF),
) )
} else if matches.is_present(OPT_DATE) || matches.is_present(OPT_CURRENT) { } else if matches.is_present(options::sources::DATE)
let timestamp = if matches.is_present(OPT_DATE) { || matches.is_present(options::sources::CURRENT)
parse_date(matches.value_of(OPT_DATE).unwrap().as_ref()) {
let timestamp = if matches.is_present(options::sources::DATE) {
parse_date(matches.value_of(options::sources::DATE).unwrap().as_ref())
} else { } else {
parse_timestamp(matches.value_of(OPT_CURRENT).unwrap().as_ref()) parse_timestamp(
matches
.value_of(options::sources::CURRENT)
.unwrap()
.as_ref(),
)
}; };
(timestamp, timestamp) (timestamp, timestamp)
} else { } else {
@ -164,7 +178,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
if !Path::new(path).exists() { if !Path::new(path).exists() {
// no-dereference included here for compatibility // no-dereference included here for compatibility
if matches.is_present(OPT_NO_CREATE) || matches.is_present(OPT_NO_DEREF) { if matches.is_present(options::NO_CREATE) || matches.is_present(options::NO_DEREF) {
continue; continue;
} }
@ -174,9 +188,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}; };
// Minor optimization: if no reference time was specified, we're done. // Minor optimization: if no reference time was specified, we're done.
if !(matches.is_present(OPT_DATE) if !(matches.is_present(options::sources::DATE)
|| matches.is_present(OPT_REFERENCE) || matches.is_present(options::sources::REFERENCE)
|| matches.is_present(OPT_CURRENT)) || matches.is_present(options::sources::CURRENT))
{ {
continue; continue;
} }
@ -184,14 +198,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
// If changing "only" atime or mtime, grab the existing value of the other. // If changing "only" atime or mtime, grab the existing value of the other.
// Note that "-a" and "-m" may be passed together; this is not an xor. // Note that "-a" and "-m" may be passed together; this is not an xor.
if matches.is_present(OPT_ACCESS) if matches.is_present(options::ACCESS)
|| matches.is_present(OPT_MODIFICATION) || matches.is_present(options::MODIFICATION)
|| matches.is_present(OPT_TIME) || matches.is_present(options::TIME)
{ {
let st = stat(path, !matches.is_present(OPT_NO_DEREF)); let st = stat(path, !matches.is_present(options::NO_DEREF));
let time = matches.value_of(OPT_TIME).unwrap_or(""); let time = matches.value_of(options::TIME).unwrap_or("");
if !(matches.is_present(OPT_ACCESS) if !(matches.is_present(options::ACCESS)
|| time.contains(&"access".to_owned()) || time.contains(&"access".to_owned())
|| time.contains(&"atime".to_owned()) || time.contains(&"atime".to_owned())
|| time.contains(&"use".to_owned())) || time.contains(&"use".to_owned()))
@ -199,7 +213,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
atime = st.0; atime = st.0;
} }
if !(matches.is_present(OPT_MODIFICATION) if !(matches.is_present(options::MODIFICATION)
|| time.contains(&"modify".to_owned()) || time.contains(&"modify".to_owned())
|| time.contains(&"mtime".to_owned())) || time.contains(&"mtime".to_owned()))
{ {
@ -207,7 +221,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
} }
} }
if matches.is_present(OPT_NO_DEREF) { if matches.is_present(options::NO_DEREF) {
if let Err(e) = set_symlink_file_times(path, atime, mtime) { if let Err(e) = set_symlink_file_times(path, atime, mtime) {
show_warning!("cannot touch '{}': {}", path, e); show_warning!("cannot touch '{}': {}", path, e);
} }

View file

@ -29,10 +29,12 @@ enum TruncateMode {
static ABOUT: &str = "Shrink or extend the size of each file to the specified size."; static ABOUT: &str = "Shrink or extend the size of each file to the specified size.";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static OPT_IO_BLOCKS: &str = "io-blocks"; pub mod options {
static OPT_NO_CREATE: &str = "no-create"; pub static IO_BLOCKS: &str = "io-blocks";
static OPT_REFERENCE: &str = "reference"; pub static NO_CREATE: &str = "no-create";
static OPT_SIZE: &str = "size"; pub static REFERENCE: &str = "reference";
pub static SIZE: &str = "size";
}
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -72,26 +74,26 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.usage(&usage[..]) .usage(&usage[..])
.after_help(&long_usage[..]) .after_help(&long_usage[..])
.arg( .arg(
Arg::with_name(OPT_IO_BLOCKS) Arg::with_name(options::IO_BLOCKS)
.short("o") .short("o")
.long(OPT_IO_BLOCKS) .long(options::IO_BLOCKS)
.help("treat SIZE as the number of I/O blocks of the file rather than bytes (NOT IMPLEMENTED)") .help("treat SIZE as the number of I/O blocks of the file rather than bytes (NOT IMPLEMENTED)")
) )
.arg( .arg(
Arg::with_name(OPT_NO_CREATE) Arg::with_name(options::NO_CREATE)
.short("c") .short("c")
.long(OPT_NO_CREATE) .long(options::NO_CREATE)
.help("do not create files that do not exist") .help("do not create files that do not exist")
) )
.arg( .arg(
Arg::with_name(OPT_REFERENCE) Arg::with_name(options::REFERENCE)
.short("r") .short("r")
.long(OPT_REFERENCE) .long(options::REFERENCE)
.help("base the size of each file on the size of RFILE") .help("base the size of each file on the size of RFILE")
.value_name("RFILE") .value_name("RFILE")
) )
.arg( .arg(
Arg::with_name(OPT_SIZE) Arg::with_name(options::SIZE)
.short("s") .short("s")
.long("size") .long("size")
.help("set or adjust the size of each file according to SIZE, which is in bytes unless --io-blocks is specified") .help("set or adjust the size of each file according to SIZE, which is in bytes unless --io-blocks is specified")
@ -109,10 +111,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
show_error!("Missing an argument"); show_error!("Missing an argument");
return 1; return 1;
} else { } else {
let io_blocks = matches.is_present(OPT_IO_BLOCKS); let io_blocks = matches.is_present(options::IO_BLOCKS);
let no_create = matches.is_present(OPT_NO_CREATE); let no_create = matches.is_present(options::NO_CREATE);
let reference = matches.value_of(OPT_REFERENCE).map(String::from); let reference = matches.value_of(options::REFERENCE).map(String::from);
let size = matches.value_of(OPT_SIZE).map(String::from); let size = matches.value_of(options::SIZE).map(String::from);
if reference.is_none() && size.is_none() { if reference.is_none() && size.is_none() {
crash!(1, "you must specify either --reference or --size"); crash!(1, "you must specify either --reference or --size");
} else { } else {

View file

@ -19,19 +19,17 @@ use platform_info::*;
const VERSION: &str = env!("CARGO_PKG_VERSION"); const VERSION: &str = env!("CARGO_PKG_VERSION");
const ABOUT: &str = "Print certain system information. With no OPTION, same as -s."; const ABOUT: &str = "Print certain system information. With no OPTION, same as -s.";
const OPT_ALL: &str = "all"; pub mod options {
const OPT_KERNELNAME: &str = "kernel-name"; pub static ALL: &str = "all";
const OPT_NODENAME: &str = "nodename"; pub static KERNELNAME: &str = "kernel-name";
const OPT_KERNELVERSION: &str = "kernel-version"; pub static NODENAME: &str = "nodename";
const OPT_KERNELRELEASE: &str = "kernel-release"; pub static KERNELVERSION: &str = "kernel-version";
const OPT_MACHINE: &str = "machine"; pub static KERNELRELEASE: &str = "kernel-release";
const OPT_PROCESSOR: &str = "processor"; pub static MACHINE: &str = "machine";
const OPT_HWPLATFORM: &str = "hardware-platform"; pub static PROCESSOR: &str = "processor";
pub static HWPLATFORM: &str = "hardware-platform";
//FIXME: unimplemented options pub static OS: &str = "operating-system";
//const OPT_PROCESSOR: &'static str = "processor"; }
//const OPT_HWPLATFORM: &'static str = "hardware-platform";
const OPT_OS: &str = "operating-system";
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
const HOST_OS: &str = "GNU/Linux"; const HOST_OS: &str = "GNU/Linux";
@ -54,58 +52,58 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.version(VERSION) .version(VERSION)
.about(ABOUT) .about(ABOUT)
.usage(&usage[..]) .usage(&usage[..])
.arg(Arg::with_name(OPT_ALL) .arg(Arg::with_name(options::ALL)
.short("a") .short("a")
.long(OPT_ALL) .long(options::ALL)
.help("Behave as though all of the options -mnrsv were specified.")) .help("Behave as though all of the options -mnrsv were specified."))
.arg(Arg::with_name(OPT_KERNELNAME) .arg(Arg::with_name(options::KERNELNAME)
.short("s") .short("s")
.long(OPT_KERNELNAME) .long(options::KERNELNAME)
.alias("sysname") // Obsolescent option in GNU uname .alias("sysname") // Obsolescent option in GNU uname
.help("print the kernel name.")) .help("print the kernel name."))
.arg(Arg::with_name(OPT_NODENAME) .arg(Arg::with_name(options::NODENAME)
.short("n") .short("n")
.long(OPT_NODENAME) .long(options::NODENAME)
.help("print the nodename (the nodename may be a name that the system is known by to a communications network).")) .help("print the nodename (the nodename may be a name that the system is known by to a communications network)."))
.arg(Arg::with_name(OPT_KERNELRELEASE) .arg(Arg::with_name(options::KERNELRELEASE)
.short("r") .short("r")
.long(OPT_KERNELRELEASE) .long(options::KERNELRELEASE)
.alias("release") // Obsolescent option in GNU uname .alias("release") // Obsolescent option in GNU uname
.help("print the operating system release.")) .help("print the operating system release."))
.arg(Arg::with_name(OPT_KERNELVERSION) .arg(Arg::with_name(options::KERNELVERSION)
.short("v") .short("v")
.long(OPT_KERNELVERSION) .long(options::KERNELVERSION)
.help("print the operating system version.")) .help("print the operating system version."))
.arg(Arg::with_name(OPT_HWPLATFORM) .arg(Arg::with_name(options::HWPLATFORM)
.short("i") .short("i")
.long(OPT_HWPLATFORM) .long(options::HWPLATFORM)
.help("print the hardware platform (non-portable)")) .help("print the hardware platform (non-portable)"))
.arg(Arg::with_name(OPT_MACHINE) .arg(Arg::with_name(options::MACHINE)
.short("m") .short("m")
.long(OPT_MACHINE) .long(options::MACHINE)
.help("print the machine hardware name.")) .help("print the machine hardware name."))
.arg(Arg::with_name(OPT_PROCESSOR) .arg(Arg::with_name(options::PROCESSOR)
.short("p") .short("p")
.long(OPT_PROCESSOR) .long(options::PROCESSOR)
.help("print the processor type (non-portable)")) .help("print the processor type (non-portable)"))
.arg(Arg::with_name(OPT_OS) .arg(Arg::with_name(options::OS)
.short("o") .short("o")
.long(OPT_OS) .long(options::OS)
.help("print the operating system name.")) .help("print the operating system name."))
.get_matches_from(args); .get_matches_from(args);
let uname = return_if_err!(1, PlatformInfo::new()); let uname = return_if_err!(1, PlatformInfo::new());
let mut output = String::new(); let mut output = String::new();
let all = matches.is_present(OPT_ALL); let all = matches.is_present(options::ALL);
let kernelname = matches.is_present(OPT_KERNELNAME); let kernelname = matches.is_present(options::KERNELNAME);
let nodename = matches.is_present(OPT_NODENAME); let nodename = matches.is_present(options::NODENAME);
let kernelrelease = matches.is_present(OPT_KERNELRELEASE); let kernelrelease = matches.is_present(options::KERNELRELEASE);
let kernelversion = matches.is_present(OPT_KERNELVERSION); let kernelversion = matches.is_present(options::KERNELVERSION);
let machine = matches.is_present(OPT_MACHINE); let machine = matches.is_present(options::MACHINE);
let processor = matches.is_present(OPT_PROCESSOR); let processor = matches.is_present(options::PROCESSOR);
let hwplatform = matches.is_present(OPT_HWPLATFORM); let hwplatform = matches.is_present(options::HWPLATFORM);
let os = matches.is_present(OPT_OS); let os = matches.is_present(options::OS);
let none = !(all let none = !(all
|| kernelname || kernelname

View file

@ -16,15 +16,17 @@ use std::str::FromStr;
static ABOUT: &str = "Report or omit repeated lines."; static ABOUT: &str = "Report or omit repeated lines.";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static OPT_ALL_REPEATED: &str = "all-repeated"; pub mod options {
static OPT_CHECK_CHARS: &str = "check-chars"; pub static ALL_REPEATED: &str = "all-repeated";
static OPT_COUNT: &str = "count"; pub static CHECK_CHARS: &str = "check-chars";
static OPT_IGNORE_CASE: &str = "ignore-case"; pub static COUNT: &str = "count";
static OPT_REPEATED: &str = "repeated"; pub static IGNORE_CASE: &str = "ignore-case";
static OPT_SKIP_FIELDS: &str = "skip-fields"; pub static REPEATED: &str = "repeated";
static OPT_SKIP_CHARS: &str = "skip-chars"; pub static SKIP_FIELDS: &str = "skip-fields";
static OPT_UNIQUE: &str = "unique"; pub static SKIP_CHARS: &str = "skip-chars";
static OPT_ZERO_TERMINATED: &str = "zero-terminated"; pub static UNIQUE: &str = "unique";
pub static ZERO_TERMINATED: &str = "zero-terminated";
}
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -233,63 +235,63 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.usage(&usage[..]) .usage(&usage[..])
.after_help(&long_usage[..]) .after_help(&long_usage[..])
.arg( .arg(
Arg::with_name(OPT_ALL_REPEATED) Arg::with_name(options::ALL_REPEATED)
.short("D") .short("D")
.long(OPT_ALL_REPEATED) .long(options::ALL_REPEATED)
.possible_values(&["none", "prepend", "separate"]) .possible_values(&["none", "prepend", "separate"])
.help("print all duplicate lines. Delimiting is done with blank lines") .help("print all duplicate lines. Delimiting is done with blank lines")
.value_name("delimit-method") .value_name("delimit-method")
.default_value("none"), .default_value("none"),
) )
.arg( .arg(
Arg::with_name(OPT_CHECK_CHARS) Arg::with_name(options::CHECK_CHARS)
.short("w") .short("w")
.long(OPT_CHECK_CHARS) .long(options::CHECK_CHARS)
.help("compare no more than N characters in lines") .help("compare no more than N characters in lines")
.value_name("N"), .value_name("N"),
) )
.arg( .arg(
Arg::with_name(OPT_COUNT) Arg::with_name(options::COUNT)
.short("c") .short("c")
.long(OPT_COUNT) .long(options::COUNT)
.help("prefix lines by the number of occurrences"), .help("prefix lines by the number of occurrences"),
) )
.arg( .arg(
Arg::with_name(OPT_IGNORE_CASE) Arg::with_name(options::IGNORE_CASE)
.short("i") .short("i")
.long(OPT_IGNORE_CASE) .long(options::IGNORE_CASE)
.help("ignore differences in case when comparing"), .help("ignore differences in case when comparing"),
) )
.arg( .arg(
Arg::with_name(OPT_REPEATED) Arg::with_name(options::REPEATED)
.short("d") .short("d")
.long(OPT_REPEATED) .long(options::REPEATED)
.help("only print duplicate lines"), .help("only print duplicate lines"),
) )
.arg( .arg(
Arg::with_name(OPT_SKIP_CHARS) Arg::with_name(options::SKIP_CHARS)
.short("s") .short("s")
.long(OPT_SKIP_CHARS) .long(options::SKIP_CHARS)
.help("avoid comparing the first N characters") .help("avoid comparing the first N characters")
.value_name("N"), .value_name("N"),
) )
.arg( .arg(
Arg::with_name(OPT_SKIP_FIELDS) Arg::with_name(options::SKIP_FIELDS)
.short("f") .short("f")
.long(OPT_SKIP_FIELDS) .long(options::SKIP_FIELDS)
.help("avoid comparing the first N fields") .help("avoid comparing the first N fields")
.value_name("N"), .value_name("N"),
) )
.arg( .arg(
Arg::with_name(OPT_UNIQUE) Arg::with_name(options::UNIQUE)
.short("u") .short("u")
.long(OPT_UNIQUE) .long(options::UNIQUE)
.help("only print unique lines"), .help("only print unique lines"),
) )
.arg( .arg(
Arg::with_name(OPT_ZERO_TERMINATED) Arg::with_name(options::ZERO_TERMINATED)
.short("z") .short("z")
.long(OPT_ZERO_TERMINATED) .long(options::ZERO_TERMINATED)
.help("end lines with 0 byte, not newline"), .help("end lines with 0 byte, not newline"),
) )
.arg( .arg(
@ -316,11 +318,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}; };
let uniq = Uniq { let uniq = Uniq {
repeats_only: matches.is_present(OPT_REPEATED) repeats_only: matches.is_present(options::REPEATED)
|| matches.occurrences_of(OPT_ALL_REPEATED) > 0, || matches.occurrences_of(options::ALL_REPEATED) > 0,
uniques_only: matches.is_present(OPT_UNIQUE), uniques_only: matches.is_present(options::UNIQUE),
all_repeated: matches.occurrences_of(OPT_ALL_REPEATED) > 0, all_repeated: matches.occurrences_of(options::ALL_REPEATED) > 0,
delimiters: match matches.value_of(OPT_ALL_REPEATED).map(String::from) { delimiters: match matches.value_of(options::ALL_REPEATED).map(String::from) {
Some(ref opt_arg) if opt_arg != "none" => match &(*opt_arg.as_str()) { Some(ref opt_arg) if opt_arg != "none" => match &(*opt_arg.as_str()) {
"prepend" => Delimiters::Prepend, "prepend" => Delimiters::Prepend,
"separate" => Delimiters::Separate, "separate" => Delimiters::Separate,
@ -328,12 +330,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}, },
_ => Delimiters::None, _ => Delimiters::None,
}, },
show_counts: matches.is_present(OPT_COUNT), show_counts: matches.is_present(options::COUNT),
skip_fields: opt_parsed(OPT_SKIP_FIELDS, &matches), skip_fields: opt_parsed(options::SKIP_FIELDS, &matches),
slice_start: opt_parsed(OPT_SKIP_CHARS, &matches), slice_start: opt_parsed(options::SKIP_CHARS, &matches),
slice_stop: opt_parsed(OPT_CHECK_CHARS, &matches), slice_stop: opt_parsed(options::CHECK_CHARS, &matches),
ignore_case: matches.is_present(OPT_IGNORE_CASE), ignore_case: matches.is_present(options::IGNORE_CASE),
zero_terminated: matches.is_present(OPT_ZERO_TERMINATED), zero_terminated: matches.is_present(options::ZERO_TERMINATED),
}; };
uniq.print_uniq( uniq.print_uniq(
&mut open_input_file(in_file_name), &mut open_input_file(in_file_name),

View file

@ -21,7 +21,9 @@ static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "Display the current time, the length of time the system has been up,\n\ static ABOUT: &str = "Display the current time, the length of time the system has been up,\n\
the number of users on the system, and the average number of jobs\n\ the number of users on the system, and the average number of jobs\n\
in the run queue over the last 1, 5 and 15 minutes."; in the run queue over the last 1, 5 and 15 minutes.";
static OPT_SINCE: &str = "since"; pub mod options {
pub static SINCE: &str = "since";
}
#[cfg(unix)] #[cfg(unix)]
use uucore::libc::getloadavg; use uucore::libc::getloadavg;
@ -42,9 +44,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.about(ABOUT) .about(ABOUT)
.usage(&usage[..]) .usage(&usage[..])
.arg( .arg(
Arg::with_name(OPT_SINCE) Arg::with_name(options::SINCE)
.short("s") .short("s")
.long(OPT_SINCE) .long(options::SINCE)
.help("system up since"), .help("system up since"),
) )
.get_matches_from(args); .get_matches_from(args);
@ -56,7 +58,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
1 1
} else { } else {
if matches.is_present(OPT_SINCE) { if matches.is_present(options::SINCE) {
let initial_date = Local.timestamp(Utc::now().timestamp() - uptime, 0); let initial_date = Local.timestamp(Utc::now().timestamp() - uptime, 0);
println!("{}", initial_date.format("%Y-%m-%d %H:%M:%S")); println!("{}", initial_date.format("%Y-%m-%d %H:%M:%S"));
return 0; return 0;

View file

@ -29,11 +29,11 @@ struct Settings {
impl Settings { impl Settings {
fn new(matches: &ArgMatches) -> Settings { fn new(matches: &ArgMatches) -> Settings {
let settings = Settings { let settings = Settings {
show_bytes: matches.is_present(OPT_BYTES), show_bytes: matches.is_present(options::BYTES),
show_chars: matches.is_present(OPT_CHAR), show_chars: matches.is_present(options::CHAR),
show_lines: matches.is_present(OPT_LINES), show_lines: matches.is_present(options::LINES),
show_words: matches.is_present(OPT_WORDS), show_words: matches.is_present(options::WORDS),
show_max_line_length: matches.is_present(OPT_MAX_LINE_LENGTH), show_max_line_length: matches.is_present(options::MAX_LINE_LENGTH),
}; };
if settings.show_bytes if settings.show_bytes
@ -68,11 +68,13 @@ static ABOUT: &str = "Display newline, word, and byte counts for each FILE, and
more than one FILE is specified."; more than one FILE is specified.";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static OPT_BYTES: &str = "bytes"; pub mod options {
static OPT_CHAR: &str = "chars"; pub static BYTES: &str = "bytes";
static OPT_LINES: &str = "lines"; pub static CHAR: &str = "chars";
static OPT_MAX_LINE_LENGTH: &str = "max-line-length"; pub static LINES: &str = "lines";
static OPT_WORDS: &str = "words"; pub static MAX_LINE_LENGTH: &str = "max-line-length";
pub static WORDS: &str = "words";
}
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -92,33 +94,33 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.about(ABOUT) .about(ABOUT)
.usage(&usage[..]) .usage(&usage[..])
.arg( .arg(
Arg::with_name(OPT_BYTES) Arg::with_name(options::BYTES)
.short("c") .short("c")
.long(OPT_BYTES) .long(options::BYTES)
.help("print the byte counts"), .help("print the byte counts"),
) )
.arg( .arg(
Arg::with_name(OPT_CHAR) Arg::with_name(options::CHAR)
.short("m") .short("m")
.long(OPT_CHAR) .long(options::CHAR)
.help("print the character counts"), .help("print the character counts"),
) )
.arg( .arg(
Arg::with_name(OPT_LINES) Arg::with_name(options::LINES)
.short("l") .short("l")
.long(OPT_LINES) .long(options::LINES)
.help("print the newline counts"), .help("print the newline counts"),
) )
.arg( .arg(
Arg::with_name(OPT_MAX_LINE_LENGTH) Arg::with_name(options::MAX_LINE_LENGTH)
.short("L") .short("L")
.long(OPT_MAX_LINE_LENGTH) .long(options::MAX_LINE_LENGTH)
.help("print the length of the longest line"), .help("print the length of the longest line"),
) )
.arg( .arg(
Arg::with_name(OPT_WORDS) Arg::with_name(options::WORDS)
.short("w") .short("w")
.long(OPT_WORDS) .long(options::WORDS)
.help("print the word counts"), .help("print the word counts"),
) )
.arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true))