diff --git a/examples/getargv.rs b/examples/getargv.rs index 72aab04..aded4d2 100755 --- a/examples/getargv.rs +++ b/examples/getargv.rs @@ -1,9 +1,9 @@ +use clap::{command, value_parser, Arg, ArgAction, ArgMatches}; use getargv::get_argv_of_pid; -use clap::{Arg, command, ArgAction, ArgMatches, value_parser}; use libc::pid_t; use std::{ + ffi::c_uint as uint, io::{stdout, Write}, - ffi::c_uint as uint }; const fn arg_max() -> usize { @@ -14,30 +14,45 @@ const fn arg_max() -> usize { } } -const PID_MAX: pid_t = env!("DEP_GETARGV_PID_MAX").parse::().unwrap(); +#[cfg(feature = "const_int_from_str")] +const fn pid_max() -> pid_t { + match pid_t::from_str_radix(env!("DEP_GETARGV_PID_MAX"), 10) { + Ok(p) => p, + _ => panic!("DEP_GETARGV_PID_MAX env var was missing or unparseable"), + } +} + +#[cfg(not(feature = "const_int_from_str"))] +fn pid_max() -> pid_t { + env!("DEP_GETARGV_PID_MAX").parse::().unwrap() +} fn parse_args() -> (pid_t, usize, bool) { let matches: ArgMatches = command!() - .arg(Arg::new("pid") - .required(true) - .help("The pid of the process for which to get the arguments") - .value_name("PID") - .value_parser(value_parser!(u64).range(0..=PID_MAX)) - .index(1) + .arg( + Arg::new("pid") + .required(true) + .help("The pid of the process for which to get the arguments") + .value_name("PID") + .value_parser(value_parser!(u64).range(0..=pid_max() as u64)) + .index(1), + ) + .arg( + Arg::new("skip") + .short('s') + .value_name("skip") + .value_parser(value_parser!(u64).range(0..=arg_max() as u64)) + .default_value("0") + .required(false) + .help("Number of arguments to skip"), ) - .arg(Arg::new("skip") - .short('s') - .value_name("skip") - .value_parser(value_parser!(u64).range(0..arg_max() as u64)) - .default_value("0") - .required(false) - .help("Number of arguments to skip"), + .arg( + Arg::new("nuls") + .short('0') + .help("Output args NUL separated") + .required(false) + .action(ArgAction::SetTrue), ) - .arg(Arg::new("nuls") - .short('0') - .help("Output args NUL separated") - .required(false) - .action(ArgAction::SetTrue)) .get_matches(); let pid = *matches.get_one::("pid").expect("PID is required") as pid_t; @@ -50,6 +65,8 @@ fn main() { let (pid, skip, nuls) = parse_args(); let argv = get_argv_of_pid(pid, nuls, skip as uint); - argv.expect("failed to get args").print().expect("failed to print"); + argv.expect("failed to get args") + .print() + .expect("failed to print"); let _ = stdout().flush(); }