grep, pkill: Move match-related clap args to process_matcher

This commit is contained in:
Tuomas Tynkkynen
2025-02-10 23:38:41 +02:00
parent 4d7505321f
commit b22415c847
3 changed files with 68 additions and 100 deletions
+5 -50
View File
@@ -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 <string> "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 <PGID> ... "match listed process group IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
// arg!(-G --group <GID> ... "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 <seconds> "select where older than seconds")
.value_parser(clap::value_parser!(u64)),
arg!(-P --parent <PPID> "match only child processes of the given parent")
.value_delimiter(',')
.value_parser(clap::value_parser!(u64)),
// arg!(-s --session <SID> "match session IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
arg!(--signal <sig> "signal to send (either number or name)")
.default_value("SIGTERM"),
arg!(-t --terminal <tty> "match by controlling terminal")
.value_delimiter(','),
// arg!(-u --euid <ID> ... "match by effective IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
// arg!(-U --uid <ID> ... "match by real IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
arg!(-x --exact "match exactly with the command name"),
// arg!(-F --pidfile <file> "read PIDs from file"),
// arg!(-L --logpidfile "fail if PID file is not locked"),
arg!(-r --runstates <state> "match runstates [D,S,Z,...]"),
// arg!(-A --"ignore-ancestors" "exclude our ancestors from results"),
// arg!(--cgroup <grp> "match by cgroup v2 names")
// .value_delimiter(','),
// arg!( --ns <PID> "match the processes that belong to the same namespace as <pid>"),
// arg!( --nslist <ns> ... "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,
))
}
+58 -1
View File
@@ -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<usize> {
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<Arg> {
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 <PGID> "match listed process group IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
// arg!(-G --group <GID> "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 <seconds> "select where older than seconds")
.value_parser(clap::value_parser!(u64)),
arg!(-P --parent <PPID> "match only child processes of the given parent")
.value_delimiter(',')
.value_parser(clap::value_parser!(u64)),
// arg!(-s --session <SID> "match session IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
arg!(--signal <sig> "signal to send (either number or name)")
.default_value("SIGTERM"),
arg!(-t --terminal <tty> "match by controlling terminal").value_delimiter(','),
// arg!(-u --euid <ID> "match by effective IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
// arg!(-U --uid <ID> "match by real IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
arg!(-x --exact "match exactly with the command name"),
// arg!(-F --pidfile <file> "read PIDs from file"),
// arg!(-L --logpidfile "fail if PID file is not locked"),
arg!(-r --runstates <state> "match runstates [D,S,Z,...]"),
// arg!(-A --"ignore-ancestors" "exclude our ancestors from results"),
// arg!(--cgroup <grp> "match by cgroup v2 names")
// .value_delimiter(','),
// arg!(--ns <PID> "match the processes that belong to the same namespace as <pid>"),
// arg!(--nslist <ns> "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),
]
}
+5 -49
View File
@@ -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!(-<sig> "signal to send (either number or name)"),
arg!(-H --"require-handler" "match only if signal handler is present"),
// arg!(-q --queue <value> "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 <PGID> "match listed process group IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
// arg!(-G --group <GID> "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 <seconds> "select where older than seconds")
.value_parser(clap::value_parser!(u64)),
arg!(-P --parent <PPID> "match only child processes of the given parent")
.value_delimiter(',')
.value_parser(clap::value_parser!(u64)),
// arg!(-s --session <SID> "match session IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
arg!(--signal <sig> "signal to send (either number or name)")
.default_value("SIGTERM"),
arg!(-t --terminal <tty> "match by controlling terminal").value_delimiter(','),
// arg!(-u --euid <ID> "match by effective IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
// arg!(-U --uid <ID> "match by real IDs")
// .value_delimiter(',')
// .value_parser(clap::value_parser!(u64)),
arg!(-x --exact "match exactly with the command name"),
// arg!(-F --pidfile <file> "read PIDs from file"),
// arg!(-L --logpidfile "fail if PID file is not locked"),
arg!(-r --runstates <state> "match runstates [D,S,Z,...]"),
// arg!(-A --"ignore-ancestors" "exclude our ancestors from results"),
// arg!(--cgroup <grp> "match by cgroup v2 names")
// .value_delimiter(','),
// arg!(--ns <PID> "match the processes that belong to the same namespace as <pid>"),
// arg!(--nslist <ns> "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,
))
}