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

id: update to clap 4

This commit is contained in:
Terts Diepraam 2022-09-29 18:50:00 +02:00
parent 0f642451e1
commit a3a50eb4ef
2 changed files with 42 additions and 37 deletions

View file

@ -15,7 +15,7 @@ edition = "2021"
path = "src/id.rs" path = "src/id.rs"
[dependencies] [dependencies]
clap = { version = "3.2", features = ["wrap_help", "cargo"] } clap = { version = "4.0", features = ["wrap_help", "cargo"] }
uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["entries", "process"] } uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["entries", "process"] }
selinux = { version="0.3", optional = true } selinux = { version="0.3", optional = true }

View file

@ -39,7 +39,7 @@
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use clap::{crate_version, Arg, Command}; use clap::{crate_version, Arg, ArgAction, Command};
use std::ffi::CStr; use std::ffi::CStr;
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::entries::{self, Group, Locate, Passwd}; use uucore::entries::{self, Group, Locate, Passwd};
@ -79,15 +79,13 @@ mod options {
pub const ARG_USERS: &str = "USER"; pub const ARG_USERS: &str = "USER";
} }
fn get_description() -> String { fn get_description() -> &'static str {
String::from( "The id utility displays the user and group names and numeric IDs, of the \
"The id utility displays the user and group names and numeric IDs, of the \ calling process, to the standard output. If the real and effective IDs are \
calling process, to the standard output. If the real and effective IDs are \ different, both are displayed, otherwise only the real ID is displayed.\n\n\
different, both are displayed, otherwise only the real ID is displayed.\n\n\ If a user (login name or user ID) is specified, the user and group IDs of \
If a user (login name or user ID) is specified, the user and group IDs of \ that user are displayed. In this case, the real and effective IDs are \
that user are displayed. In this case, the real and effective IDs are \ assumed to be the same."
assumed to be the same.",
)
} }
struct Ids { struct Ids {
@ -126,10 +124,8 @@ struct State {
#[uucore::main] #[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let after_help = get_description();
let matches = uu_app() let matches = uu_app()
.after_help(&after_help[..]) .after_help(get_description())
.try_get_matches_from(args)?; .try_get_matches_from(args)?;
let users: Vec<String> = matches let users: Vec<String> = matches
@ -138,13 +134,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.unwrap_or_default(); .unwrap_or_default();
let mut state = State { let mut state = State {
nflag: matches.contains_id(options::OPT_NAME), nflag: matches.get_flag(options::OPT_NAME),
uflag: matches.contains_id(options::OPT_EFFECTIVE_USER), uflag: matches.get_flag(options::OPT_EFFECTIVE_USER),
gflag: matches.contains_id(options::OPT_GROUP), gflag: matches.get_flag(options::OPT_GROUP),
gsflag: matches.contains_id(options::OPT_GROUPS), gsflag: matches.get_flag(options::OPT_GROUPS),
rflag: matches.contains_id(options::OPT_REAL_ID), rflag: matches.get_flag(options::OPT_REAL_ID),
zflag: matches.contains_id(options::OPT_ZERO), zflag: matches.get_flag(options::OPT_ZERO),
cflag: matches.contains_id(options::OPT_CONTEXT), cflag: matches.get_flag(options::OPT_CONTEXT),
selinux_supported: { selinux_supported: {
#[cfg(feature = "selinux")] #[cfg(feature = "selinux")]
@ -239,17 +235,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}; };
// GNU's `id` does not support the flags: -p/-P/-A. // GNU's `id` does not support the flags: -p/-P/-A.
if matches.contains_id(options::OPT_PASSWORD) { if matches.get_flag(options::OPT_PASSWORD) {
// BSD's `id` ignores all but the first specified user // BSD's `id` ignores all but the first specified user
pline(possible_pw.as_ref().map(|v| v.uid)); pline(possible_pw.as_ref().map(|v| v.uid));
return Ok(()); return Ok(());
}; };
if matches.contains_id(options::OPT_HUMAN_READABLE) { if matches.get_flag(options::OPT_HUMAN_READABLE) {
// BSD's `id` ignores all but the first specified user // BSD's `id` ignores all but the first specified user
pretty(possible_pw); pretty(possible_pw);
return Ok(()); return Ok(());
} }
if matches.contains_id(options::OPT_AUDIT) { if matches.get_flag(options::OPT_AUDIT) {
// BSD's `id` ignores specified users // BSD's `id` ignores specified users
auditid(); auditid();
return Ok(()); return Ok(());
@ -343,7 +339,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Ok(()) Ok(())
} }
pub fn uu_app<'a>() -> Command<'a> { pub fn uu_app() -> Command {
Command::new(uucore::util_name()) Command::new(uucore::util_name())
.version(crate_version!()) .version(crate_version!())
.about(ABOUT) .about(ABOUT)
@ -363,21 +359,24 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"Display the process audit user ID and other process audit properties,\n\ "Display the process audit user ID and other process audit properties,\n\
which requires privilege (not available on Linux).", which requires privilege (not available on Linux).",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::OPT_EFFECTIVE_USER) Arg::new(options::OPT_EFFECTIVE_USER)
.short('u') .short('u')
.long(options::OPT_EFFECTIVE_USER) .long(options::OPT_EFFECTIVE_USER)
.conflicts_with(options::OPT_GROUP) .conflicts_with(options::OPT_GROUP)
.help("Display only the effective user ID as a number."), .help("Display only the effective user ID as a number.")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::OPT_GROUP) Arg::new(options::OPT_GROUP)
.short('g') .short('g')
.long(options::OPT_GROUP) .long(options::OPT_GROUP)
.conflicts_with(options::OPT_EFFECTIVE_USER) .conflicts_with(options::OPT_EFFECTIVE_USER)
.help("Display only the effective group ID as a number"), .help("Display only the effective group ID as a number")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::OPT_GROUPS) Arg::new(options::OPT_GROUPS)
@ -394,12 +393,14 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"Display only the different group IDs as white-space separated numbers, \ "Display only the different group IDs as white-space separated numbers, \
in no particular order.", in no particular order.",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::OPT_HUMAN_READABLE) Arg::new(options::OPT_HUMAN_READABLE)
.short('p') .short('p')
.help("Make the output human-readable. Each display is on a separate line."), .help("Make the output human-readable. Each display is on a separate line.")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::OPT_NAME) Arg::new(options::OPT_NAME)
@ -409,12 +410,14 @@ pub fn uu_app<'a>() -> Command<'a> {
"Display the name of the user or group ID for the -G, -g and -u options \ "Display the name of the user or group ID for the -G, -g and -u options \
instead of the number.\nIf any of the ID numbers cannot be mapped into \ instead of the number.\nIf any of the ID numbers cannot be mapped into \
names, the number will be displayed as usual.", names, the number will be displayed as usual.",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::OPT_PASSWORD) Arg::new(options::OPT_PASSWORD)
.short('P') .short('P')
.help("Display the id as a password file entry."), .help("Display the id as a password file entry.")
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::OPT_REAL_ID) Arg::new(options::OPT_REAL_ID)
@ -423,7 +426,8 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"Display the real ID for the -G, -g and -u options instead of \ "Display the real ID for the -G, -g and -u options instead of \
the effective ID.", the effective ID.",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::OPT_ZERO) Arg::new(options::OPT_ZERO)
@ -432,19 +436,20 @@ pub fn uu_app<'a>() -> Command<'a> {
.help( .help(
"delimit entries with NUL characters, not whitespace;\n\ "delimit entries with NUL characters, not whitespace;\n\
not permitted in default format", not permitted in default format",
), )
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::OPT_CONTEXT) Arg::new(options::OPT_CONTEXT)
.short('Z') .short('Z')
.long(options::OPT_CONTEXT) .long(options::OPT_CONTEXT)
.conflicts_with_all(&[options::OPT_GROUP, options::OPT_EFFECTIVE_USER]) .conflicts_with_all(&[options::OPT_GROUP, options::OPT_EFFECTIVE_USER])
.help(CONTEXT_HELP_TEXT), .help(CONTEXT_HELP_TEXT)
.action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new(options::ARG_USERS) Arg::new(options::ARG_USERS)
.multiple_occurrences(true) .action(ArgAction::Append)
.takes_value(true)
.value_name(options::ARG_USERS) .value_name(options::ARG_USERS)
.value_hint(clap::ValueHint::Username), .value_hint(clap::ValueHint::Username),
) )