use std::process::exit;
struct Args {
operands: Vec<String>,
}
fn print_usage() {
eprintln!("Usage: penv [pid | core]...");
eprintln!("Print process environment variables.");
eprintln!();
eprintln!("Options:");
eprintln!(" -h, --help Print help");
eprintln!(" -V, --version Print version");
}
fn parse_args() -> Args {
use lexopt::prelude::*;
let mut args = Args {
operands: Vec::new(),
};
let mut parser = lexopt::Parser::from_env();
while let Some(arg) = parser.next().unwrap_or_else(|e| {
eprintln!("penv: {e}");
exit(2);
}) {
match arg {
Short('h') | Long("help") => {
print_usage();
exit(0);
}
Short('V') | Long("version") => {
println!("penv {}", env!("CARGO_PKG_VERSION"));
exit(0);
}
Value(val) => {
args.operands.push(val.to_string_lossy().into_owned());
}
_ => {
eprintln!("penv: unexpected argument: {arg:?}");
exit(2);
}
}
}
if args.operands.is_empty() {
eprintln!("penv: at least one PID or core required");
exit(2);
}
args
}
fn main() {
ptools::reset_sigpipe();
let args = parse_args();
let mut error = false;
let mut first = true;
for operand in &args.operands {
if ptools::proc::is_non_pid_proc_path(operand) {
continue;
}
if !first {
println!();
}
first = false;
let handle = match ptools::proc::resolve_operand(operand) {
Ok(h) => h,
Err(e) => {
eprintln!("penv: {e}");
error = true;
continue;
}
};
if let Err(e) = ptools::display::print_env_from(&handle) {
eprintln!("penv: {}: {e}", handle.pid());
error = true;
}
}
if error {
exit(1);
}
}