1use flood_tide::parse_simple_gnu_style;
3use flood_tide::HelpVersion;
4use flood_tide::{Arg, NameVal, Opt, OptNum};
5use flood_tide::{OptParseError, OptParseErrors};
6
7use crate::util::OptSortOrder;
8use crate::util::OptUcXParam;
9use std::str::FromStr;
10
11include!("cmd.help.rs.txt");
13
14const DESCRIPTIONS_TEXT: &str = r#"print processes memory by sort,
16or print one processe memory
17"#;
18const EXAMPLES_TEXT: &str = r#"Examples:
22 Show all prosesses memory:
23 ps-mem --all
24 Show one prosess memory:
25 ps-mem --pid 1234
26 Show invoked one prosess memory:
27 ps-mem -- find / -type f
28"#;
29#[rustfmt::skip]
33fn version_message(_program: &str) -> String {
34 format!( "{} {}",
35 env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
36}
37
38#[rustfmt::skip]
39fn usage_message(program: &str) -> String {
40 let usage1 = format!(" {} {}", program, "[options]");
41 let usage2 = format!(
42 " {} {}",
43 program, "[options] <command> {<arg1> <arg2> ...}"
44 );
45 format!("Usage:\n{usage1}\n{usage2}\n")
46}
47
48#[rustfmt::skip]
49fn help_message(program: &str) -> String {
50 let ver = version_message(program);
51 let usa = usage_message(env!("CARGO_PKG_NAME"));
52 [ &ver, "", &usa, DESCRIPTIONS_TEXT, OPTIONS_TEXT, EXAMPLES_TEXT].join("\n")
53}
54
55#[rustfmt::skip]
56fn opt_uc_x_help_message(_program: &str) -> String {
57 let z_opts = concat!(
58 "Options:\n",
59 " -X rust-version-info display package version info and exit\n",
60 " -X base_dir=<path> set <path> is base directory\n",
61 );
62 z_opts.to_string()
63}
64
65#[rustfmt::skip]
66fn opt_uc_x_package_version_info(_program: &str) -> String {
67 #[cfg(feature = "debian_build")]
68 {
69 use std::io::Read;
70 let mut string = String::new();
71 let fnm = format!("/usr/share/doc/{}/rust-version-info.txt", env!("CARGO_PKG_NAME"));
72 let file = std::fs::File::open(&fnm);
73 match file {
74 Ok(mut f) => {
75 f.read_to_string(&mut string).unwrap();
76 string
77 },
78 Err(err) => {
79 format!("ERROR: {}: '{}'", err, fnm)
80 },
81 }
82 }
83 #[cfg(not(feature = "debian_build"))]
84 {
85 const VS: &str = include_str!(concat!(env!("OUT_DIR"), "/rust-version-info.txt"));
86 VS.to_string()
87 }
88}
89
90fn parse_match(conf: &mut CmdOptConf, nv: &NameVal<'_>) -> Result<(), OptParseError> {
92 include!("cmd.match.rs.txt");
93 Ok(())
94}
95
96pub fn parse_cmdopts(a_prog_name: &str, args: &[&str]) -> Result<CmdOptConf, OptParseErrors> {
97 let mut conf = CmdOptConf {
99 prog_name: a_prog_name.to_string(),
100 opt_sleep: 10,
101 ..Default::default()
102 };
103 let (opt_free, r_errs) =
104 parse_simple_gnu_style(&mut conf, &OPT_ARY, &OPT_ARY_SHO_IDX, args, parse_match);
105 if conf.is_help() {
107 let mut errs = OptParseErrors::new();
108 errs.push(OptParseError::help_message(&help_message(&conf.prog_name)));
109 return Err(errs);
110 }
111 if conf.is_version() {
112 let mut errs = OptParseErrors::new();
113 errs.push(OptParseError::version_message(&version_message(
114 &conf.prog_name,
115 )));
116 return Err(errs);
117 }
118 if !conf.opt_uc_x.is_empty() {
119 if conf.is_opt_uc_x_help() {
120 let mut errs = OptParseErrors::new();
121 errs.push(OptParseError::help_message(&opt_uc_x_help_message(
122 &conf.prog_name,
123 )));
124 return Err(errs);
125 }
126 if conf.is_opt_uc_x_package_version_info() {
127 let mut errs = OptParseErrors::new();
128 errs.push(OptParseError::help_message(&opt_uc_x_package_version_info(
129 &conf.prog_name,
130 )));
131 return Err(errs);
132 }
133 }
134 {
136 let errs = if let Err(errs) = r_errs {
137 errs
138 } else {
139 OptParseErrors::new()
140 };
141 if let Some(free) = opt_free {
143 if !free.is_empty() {
144 conf.arg_params.extend(free.iter().map(|s| s.to_string()));
145 }
146 };
147 if !errs.is_empty() {
148 return Err(errs);
149 }
150 }
151 Ok(conf)
153}