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

Merge pull request #6761 from cakebaker/users_use_option_instead_of_vec

users: use `Option` instead of `Vec` for file arg
This commit is contained in:
Sylvestre Ledru 2024-10-03 08:50:29 +02:00 committed by GitHub
commit 50f99580b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -24,7 +24,7 @@ const USAGE: &str = help_usage!("users.md");
#[cfg(target_os = "openbsd")] #[cfg(target_os = "openbsd")]
const OPENBSD_UTMP_FILE: &str = "/var/run/utmp"; const OPENBSD_UTMP_FILE: &str = "/var/run/utmp";
static ARG_FILES: &str = "files"; static ARG_FILE: &str = "file";
fn get_long_usage() -> String { fn get_long_usage() -> String {
#[cfg(not(target_os = "openbsd"))] #[cfg(not(target_os = "openbsd"))]
@ -43,21 +43,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.after_help(get_long_usage()) .after_help(get_long_usage())
.try_get_matches_from(args)?; .try_get_matches_from(args)?;
let files: Vec<&Path> = matches let maybe_file: Option<&Path> = matches.get_one::<OsString>(ARG_FILE).map(AsRef::as_ref);
.get_many::<OsString>(ARG_FILES)
.map(|v| v.map(AsRef::as_ref).collect())
.unwrap_or_default();
let mut users: Vec<String>; let mut users: Vec<String>;
// OpenBSD uses the Unix version 1 UTMP, all other Unixes use the newer UTMPX // OpenBSD uses the Unix version 1 UTMP, all other Unixes use the newer UTMPX
#[cfg(target_os = "openbsd")] #[cfg(target_os = "openbsd")]
{ {
let filename = if files.is_empty() { let filename = maybe_file.unwrap_or(Path::new(OPENBSD_UTMP_FILE));
Path::new(OPENBSD_UTMP_FILE)
} else {
files[0]
};
let entries = parse_from_path(filename).unwrap_or(Vec::new()); let entries = parse_from_path(filename).unwrap_or(Vec::new());
users = Vec::new(); users = Vec::new();
for entry in entries { for entry in entries {
@ -76,11 +69,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}; };
#[cfg(not(target_os = "openbsd"))] #[cfg(not(target_os = "openbsd"))]
{ {
let filename = if files.is_empty() { let filename = maybe_file.unwrap_or(utmpx::DEFAULT_FILE.as_ref());
utmpx::DEFAULT_FILE.as_ref()
} else {
files[0]
};
users = Utmpx::iter_all_records_from(filename) users = Utmpx::iter_all_records_from(filename)
.filter(Utmpx::is_user_process) .filter(Utmpx::is_user_process)
@ -103,7 +92,7 @@ pub fn uu_app() -> Command {
.override_usage(format_usage(USAGE)) .override_usage(format_usage(USAGE))
.infer_long_args(true) .infer_long_args(true)
.arg( .arg(
Arg::new(ARG_FILES) Arg::new(ARG_FILE)
.num_args(1) .num_args(1)
.value_hint(clap::ValueHint::FilePath) .value_hint(clap::ValueHint::FilePath)
.value_parser(ValueParser::os_string()), .value_parser(ValueParser::os_string()),