chcon: update to clap 4

This commit is contained in:
Terts Diepraam 2022-09-29 15:24:52 +02:00
parent 717402b46a
commit 7a3cb35352
2 changed files with 41 additions and 35 deletions

View file

@ -14,7 +14,7 @@ edition = "2021"
path = "src/chcon.rs"
[dependencies]
clap = { version = "3.2", features = ["wrap_help", "cargo"] }
clap = { version = "4.0", features = ["wrap_help", "cargo"] }
uucore = { version = ">=0.0.9", package="uucore", path="../../uucore", features=["entries", "fs", "perms"] }
selinux = { version = "0.3" }
fts-sys = { version = "0.2" }

View file

@ -7,7 +7,7 @@ use uucore::error::{UResult, USimpleError, UUsageError};
use uucore::format_usage;
use uucore::{display::Quotable, show_error, show_warning};
use clap::{Arg, Command};
use clap::{Arg, ArgAction, Command};
use selinux::{OpaqueSecurityContext, SecurityContext};
use std::borrow::Cow;
@ -68,7 +68,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Err(r) => {
if let Error::CommandLine(r) = &r {
match r.kind() {
clap::ErrorKind::DisplayHelp | clap::ErrorKind::DisplayVersion => {
clap::error::ErrorKind::DisplayHelp
| clap::error::ErrorKind::DisplayVersion => {
println!("{}", r);
return Ok(());
}
@ -106,7 +107,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
CommandLineMode::ContextBased { context } => {
let c_context = match os_str_to_c_string(context) {
let c_context = match os_str_to_c_string(&context) {
Ok(context) => context,
Err(_r) => {
@ -156,7 +157,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Err(libc::EXIT_FAILURE.into())
}
pub fn uu_app<'a>() -> Command<'a> {
pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.version(VERSION)
.about(ABOUT)
@ -165,7 +166,8 @@ pub fn uu_app<'a>() -> Command<'a> {
.arg(
Arg::new(options::HELP)
.long(options::HELP)
.help("Print help information."),
.help("Print help information.")
.action(ArgAction::Help),
)
.arg(
Arg::new(options::dereference::DEREFERENCE)
@ -174,29 +176,32 @@ pub fn uu_app<'a>() -> Command<'a> {
.help(
"Affect the referent of each symbolic link (this is the default), \
rather than the symbolic link itself.",
),
)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::dereference::NO_DEREFERENCE)
.short('h')
.long(options::dereference::NO_DEREFERENCE)
.help("Affect symbolic links instead of any referenced file."),
.help("Affect symbolic links instead of any referenced file.")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::preserve_root::PRESERVE_ROOT)
.long(options::preserve_root::PRESERVE_ROOT)
.conflicts_with(options::preserve_root::NO_PRESERVE_ROOT)
.help("Fail to operate recursively on '/'."),
.help("Fail to operate recursively on '/'.")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::preserve_root::NO_PRESERVE_ROOT)
.long(options::preserve_root::NO_PRESERVE_ROOT)
.help("Do not treat '/' specially (the default)."),
.help("Do not treat '/' specially (the default).")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::REFERENCE)
.long(options::REFERENCE)
.takes_value(true)
.value_name("RFILE")
.value_hint(clap::ValueHint::FilePath)
.conflicts_with_all(&[options::USER, options::ROLE, options::TYPE, options::RANGE])
@ -210,7 +215,6 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::USER)
.short('u')
.long(options::USER)
.takes_value(true)
.value_name("USER")
.value_hint(clap::ValueHint::Username)
.help("Set user USER in the target security context.")
@ -220,7 +224,6 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::ROLE)
.short('r')
.long(options::ROLE)
.takes_value(true)
.value_name("ROLE")
.help("Set role ROLE in the target security context.")
.value_parser(ValueParser::os_string()),
@ -229,7 +232,6 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::TYPE)
.short('t')
.long(options::TYPE)
.takes_value(true)
.value_name("TYPE")
.help("Set type TYPE in the target security context.")
.value_parser(ValueParser::os_string()),
@ -238,7 +240,6 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::RANGE)
.short('l')
.long(options::RANGE)
.takes_value(true)
.value_name("RANGE")
.help("Set range RANGE in the target security context.")
.value_parser(ValueParser::os_string()),
@ -247,7 +248,8 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::RECURSIVE)
.short('R')
.long(options::RECURSIVE)
.help("Operate on files and directories recursively."),
.help("Operate on files and directories recursively.")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::sym_links::FOLLOW_ARG_DIR_SYM_LINK)
@ -260,7 +262,8 @@ pub fn uu_app<'a>() -> Command<'a> {
.help(
"If a command line argument is a symbolic link to a directory, \
traverse it. Only valid when -R is specified.",
),
)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::sym_links::FOLLOW_DIR_SYM_LINKS)
@ -273,7 +276,8 @@ pub fn uu_app<'a>() -> Command<'a> {
.help(
"Traverse every symbolic link to a directory encountered. \
Only valid when -R is specified.",
),
)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::sym_links::NO_FOLLOW_SYM_LINKS)
@ -286,19 +290,21 @@ pub fn uu_app<'a>() -> Command<'a> {
.help(
"Do not traverse any symbolic links (default). \
Only valid when -R is specified.",
),
)
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::VERBOSE)
.short('v')
.long(options::VERBOSE)
.help("Output a diagnostic for every file processed."),
.help("Output a diagnostic for every file processed.")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("FILE")
.multiple_occurrences(true)
.action(ArgAction::Append)
.value_hint(clap::ValueHint::FilePath)
.min_values(1)
.num_args(1..)
.value_parser(ValueParser::os_string()),
)
}
@ -316,11 +322,11 @@ struct Options {
fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<Options> {
let matches = config.try_get_matches_from(args)?;
let verbose = matches.contains_id(options::VERBOSE);
let verbose = matches.get_flag(options::VERBOSE);
let (recursive_mode, affect_symlink_referent) = if matches.contains_id(options::RECURSIVE) {
if matches.contains_id(options::sym_links::FOLLOW_DIR_SYM_LINKS) {
if matches.contains_id(options::dereference::NO_DEREFERENCE) {
let (recursive_mode, affect_symlink_referent) = if matches.get_flag(options::RECURSIVE) {
if matches.get_flag(options::sym_links::FOLLOW_DIR_SYM_LINKS) {
if matches.get_flag(options::dereference::NO_DEREFERENCE) {
return Err(Error::ArgumentsMismatch(format!(
"'--{}' with '--{}' require '-P'",
options::RECURSIVE,
@ -329,8 +335,8 @@ fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<
}
(RecursiveMode::RecursiveAndFollowAllDirSymLinks, true)
} else if matches.contains_id(options::sym_links::FOLLOW_ARG_DIR_SYM_LINK) {
if matches.contains_id(options::dereference::NO_DEREFERENCE) {
} else if matches.get_flag(options::sym_links::FOLLOW_ARG_DIR_SYM_LINK) {
if matches.get_flag(options::dereference::NO_DEREFERENCE) {
return Err(Error::ArgumentsMismatch(format!(
"'--{}' with '--{}' require '-P'",
options::RECURSIVE,
@ -340,7 +346,7 @@ fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<
(RecursiveMode::RecursiveAndFollowArgDirSymLinks, true)
} else {
if matches.contains_id(options::dereference::DEREFERENCE) {
if matches.get_flag(options::dereference::DEREFERENCE) {
return Err(Error::ArgumentsMismatch(format!(
"'--{}' with '--{}' require either '-H' or '-L'",
options::RECURSIVE,
@ -351,12 +357,12 @@ fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<
(RecursiveMode::RecursiveButDoNotFollowSymLinks, false)
}
} else {
let no_dereference = matches.contains_id(options::dereference::NO_DEREFERENCE);
let no_dereference = matches.get_flag(options::dereference::NO_DEREFERENCE);
(RecursiveMode::NotRecursive, !no_dereference)
};
// By default, do not preserve root.
let preserve_root = matches.contains_id(options::preserve_root::PRESERVE_ROOT);
let preserve_root = matches.get_flag(options::preserve_root::PRESERVE_ROOT);
let mut files = matches.get_many::<OsString>("FILE").unwrap_or_default();
@ -364,10 +370,10 @@ fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<
CommandLineMode::ReferenceBased {
reference: PathBuf::from(path),
}
} else if matches.contains_id(options::USER)
|| matches.contains_id(options::ROLE)
|| matches.contains_id(options::TYPE)
|| matches.contains_id(options::RANGE)
} else if matches.get_flag(options::USER)
|| matches.get_flag(options::ROLE)
|| matches.get_flag(options::TYPE)
|| matches.get_flag(options::RANGE)
{
CommandLineMode::Custom {
user: matches.get_one::<OsString>(options::USER).map(Into::into),