From b22415c8471fed691a8a97cb564baf7eb83989b9 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Mon, 10 Feb 2025 23:38:41 +0200 Subject: [PATCH] grep, pkill: Move match-related clap args to process_matcher --- src/uu/pgrep/src/pgrep.rs | 55 +++------------------------ src/uu/pgrep/src/process_matcher.rs | 59 ++++++++++++++++++++++++++++- src/uu/pkill/src/pkill.rs | 54 +++----------------------- 3 files changed, 68 insertions(+), 100 deletions(-) diff --git a/src/uu/pgrep/src/pgrep.rs b/src/uu/pgrep/src/pgrep.rs index c947c2d..b1856ab 100644 --- a/src/uu/pgrep/src/pgrep.rs +++ b/src/uu/pgrep/src/pgrep.rs @@ -7,7 +7,7 @@ pub mod process; pub mod process_matcher; -use clap::{arg, crate_version, Arg, ArgAction, ArgGroup, Command}; +use clap::{arg, crate_version, Command}; use uucore::{error::UResult, format_usage, help_about, help_usage}; const ABOUT: &str = help_about!("pgrep.md"); @@ -76,61 +76,16 @@ pub fn uu_app() -> Command { .about(ABOUT) .override_usage(format_usage(USAGE)) .args_override_self(true) - .group(ArgGroup::new("oldest_newest").args(["oldest", "newest", "inverse"])) .args([ arg!(-d --delimiter "specify output delimiter") .default_value("\n") .hide_default_value(true), arg!(-l --"list-name" "list PID and process name"), arg!(-a --"list-full" "list PID and full command line"), - arg!(-H --"require-handler" "match only if signal handler is present"), - arg!(-v --inverse "negates the matching"), // arg!(-w --lightweight "list all TID"), - arg!(-c --count "count of matching processes"), - arg!(-f --full "use full process name to match"), - // arg!(-g --pgroup ... "match listed process group IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - // arg!(-G --group ... "match real group IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - arg!(-i --"ignore-case" "match case insensitively"), - arg!(-n --newest "select most recently started"), - arg!(-o --oldest "select least recently started"), - arg!(-O --older "select where older than seconds") - .value_parser(clap::value_parser!(u64)), - arg!(-P --parent "match only child processes of the given parent") - .value_delimiter(',') - .value_parser(clap::value_parser!(u64)), - // arg!(-s --session "match session IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - arg!(--signal "signal to send (either number or name)") - .default_value("SIGTERM"), - arg!(-t --terminal "match by controlling terminal") - .value_delimiter(','), - // arg!(-u --euid ... "match by effective IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - // arg!(-U --uid ... "match by real IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - arg!(-x --exact "match exactly with the command name"), - // arg!(-F --pidfile "read PIDs from file"), - // arg!(-L --logpidfile "fail if PID file is not locked"), - arg!(-r --runstates "match runstates [D,S,Z,...]"), - // arg!(-A --"ignore-ancestors" "exclude our ancestors from results"), - // arg!(--cgroup "match by cgroup v2 names") - // .value_delimiter(','), - // arg!( --ns "match the processes that belong to the same namespace as "), - // arg!( --nslist ... "list which namespaces will be considered for the --ns option.") - // .value_delimiter(',') - // .value_parser(["ipc", "mnt", "net", "pid", "user", "uts"]), ]) - .arg( - Arg::new("pattern") - .help("Name of the program to find the PID of") - .action(ArgAction::Append) - .index(1), - ) + .args(process_matcher::clap_args( + "Name of the program to find the PID of", + true, + )) } diff --git a/src/uu/pgrep/src/process_matcher.rs b/src/uu/pgrep/src/process_matcher.rs index 51a9b2b..e547246 100644 --- a/src/uu/pgrep/src/process_matcher.rs +++ b/src/uu/pgrep/src/process_matcher.rs @@ -7,7 +7,7 @@ use std::collections::HashSet; -use clap::ArgMatches; +use clap::{arg, Arg, ArgAction, ArgMatches}; use regex::Regex; use uucore::error::{UResult, USimpleError}; #[cfg(unix)] @@ -248,3 +248,60 @@ fn parse_signal_value(signal_name: &str) -> UResult { signal_by_name_or_value(signal_name) .ok_or_else(|| USimpleError::new(1, format!("Unknown signal {}", signal_name.quote()))) } + +#[allow(clippy::cognitive_complexity)] +pub fn clap_args(pattern_help: &'static str, enable_v_flag: bool) -> Vec { + vec![ + if enable_v_flag { + arg!(-v --inverse "negates the matching").group("oldest_newest_inverse") + } else { + arg!(--inverse "negates the matching").group("oldest_newest_inverse") + }, + arg!(-H --"require-handler" "match only if signal handler is present"), + arg!(-c --count "count of matching processes"), + arg!(-f --full "use full process name to match"), + // arg!(-g --pgroup "match listed process group IDs") + // .value_delimiter(',') + // .value_parser(clap::value_parser!(u64)), + // arg!(-G --group "match real group IDs") + // .value_delimiter(',') + // .value_parser(clap::value_parser!(u64)), + arg!(-i --"ignore-case" "match case insensitively"), + arg!(-n --newest "select most recently started") + .group("oldest_newest_inverse"), + arg!(-o --oldest "select least recently started") + .group("oldest_newest_inverse"), + arg!(-O --older "select where older than seconds") + .value_parser(clap::value_parser!(u64)), + arg!(-P --parent "match only child processes of the given parent") + .value_delimiter(',') + .value_parser(clap::value_parser!(u64)), + // arg!(-s --session "match session IDs") + // .value_delimiter(',') + // .value_parser(clap::value_parser!(u64)), + arg!(--signal "signal to send (either number or name)") + .default_value("SIGTERM"), + arg!(-t --terminal "match by controlling terminal").value_delimiter(','), + // arg!(-u --euid "match by effective IDs") + // .value_delimiter(',') + // .value_parser(clap::value_parser!(u64)), + // arg!(-U --uid "match by real IDs") + // .value_delimiter(',') + // .value_parser(clap::value_parser!(u64)), + arg!(-x --exact "match exactly with the command name"), + // arg!(-F --pidfile "read PIDs from file"), + // arg!(-L --logpidfile "fail if PID file is not locked"), + arg!(-r --runstates "match runstates [D,S,Z,...]"), + // arg!(-A --"ignore-ancestors" "exclude our ancestors from results"), + // arg!(--cgroup "match by cgroup v2 names") + // .value_delimiter(','), + // arg!(--ns "match the processes that belong to the same namespace as "), + // arg!(--nslist "list which namespaces will be considered for the --ns option.") + // .value_delimiter(',') + // .value_parser(["ipc", "mnt", "net", "pid", "user", "uts"]), + Arg::new("pattern") + .help(pattern_help) + .action(ArgAction::Append) + .index(1), + ] +} diff --git a/src/uu/pkill/src/pkill.rs b/src/uu/pkill/src/pkill.rs index 3c121e2..07b8b0e 100644 --- a/src/uu/pkill/src/pkill.rs +++ b/src/uu/pkill/src/pkill.rs @@ -4,7 +4,7 @@ // file that was distributed with this source code. // Pid utils -use clap::{arg, crate_version, Arg, ArgAction, ArgGroup, Command}; +use clap::{arg, crate_version, Command}; #[cfg(unix)] use nix::{ sys::signal::{self, Signal}, @@ -109,57 +109,13 @@ pub fn uu_app() -> Command { .about(ABOUT) .override_usage(format_usage(USAGE)) .args_override_self(true) - .group(ArgGroup::new("oldest_newest").args(["oldest", "newest", "inverse"])) .args([ // arg!(- "signal to send (either number or name)"), - arg!(-H --"require-handler" "match only if signal handler is present"), // arg!(-q --queue "integer value to be sent with the signal"), arg!(-e --echo "display what is killed"), - arg!(--inverse "negates the matching"), - arg!(-c --count "count of matching processes"), - arg!(-f --full "use full process name to match"), - // arg!(-g --pgroup "match listed process group IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - // arg!(-G --group "match real group IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - arg!(-i --"ignore-case" "match case insensitively"), - arg!(-n --newest "select most recently started"), - arg!(-o --oldest "select least recently started"), - arg!(-O --older "select where older than seconds") - .value_parser(clap::value_parser!(u64)), - arg!(-P --parent "match only child processes of the given parent") - .value_delimiter(',') - .value_parser(clap::value_parser!(u64)), - // arg!(-s --session "match session IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - arg!(--signal "signal to send (either number or name)") - .default_value("SIGTERM"), - arg!(-t --terminal "match by controlling terminal").value_delimiter(','), - // arg!(-u --euid "match by effective IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - // arg!(-U --uid "match by real IDs") - // .value_delimiter(',') - // .value_parser(clap::value_parser!(u64)), - arg!(-x --exact "match exactly with the command name"), - // arg!(-F --pidfile "read PIDs from file"), - // arg!(-L --logpidfile "fail if PID file is not locked"), - arg!(-r --runstates "match runstates [D,S,Z,...]"), - // arg!(-A --"ignore-ancestors" "exclude our ancestors from results"), - // arg!(--cgroup "match by cgroup v2 names") - // .value_delimiter(','), - // arg!(--ns "match the processes that belong to the same namespace as "), - // arg!(--nslist "list which namespaces will be considered for the --ns option.") - // .value_delimiter(',') - // .value_parser(["ipc", "mnt", "net", "pid", "user", "uts"]), ]) - .arg( - Arg::new("pattern") - .help("Name of the program to find the PID of") - .action(ArgAction::Append) - .index(1), - ) + .args(process_matcher::clap_args( + "Name of the process to kill", + false, + )) }