1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
//
// Copyright (c) 2023 Piotr Stolarz
// GNU-like Command line options parser.
//
// Distributed under the 2-clause BSD License (the License)
// see accompanying file LICENSE for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the License for more information.
//
use cmdopts::{parse_opts, InfoCode, ProcessCode, ParseError, CmdOpt};
fn print_usage()
{
eprintln!("Usage: {} [OPTION]... [--] FILE1 [FILE2]",
std::env::args().next().unwrap());
println!(r"
Available options:
-a, --long_a No value option
-b, --long_b=INT Option with integer value, range [-10..10]
-c, --long_c=STR Option with string value
-d Short option with string value
--long_e=STR Long option with string value
-h, --help Print this help
All options are optional. FILE1 is required, FILE2 is not.
Use -- separator to avoid ambiguity if file name starts with '-'.
")
}
#[derive(Debug)]
struct Options {
a_opt: Option<()>,
b_opt: Option<i32>,
c_opt: Option<String>,
d_opt: Option<String>,
e_opt: Option<String>,
file1: Option<String>,
file2: Option<String>,
help: Option<()>,
}
impl Default for Options {
fn default() -> Self {
Options {
a_opt: None,
b_opt: None,
c_opt: None,
d_opt: None,
e_opt: None,
file1: None,
file2: None,
help: None,
}
}
}
#[allow(unused_imports)]
fn process_cmdopts(opts: &mut Options) -> Result<(), ParseError>
{
use CmdOpt::*;
use InfoCode::*;
use ProcessCode::*;
use ParseError::*;
use OptId::*;
use std::cell::Cell;
#[derive(Clone)]
#[derive(Copy)]
enum OptId {
OptA,
OptB,
OptC,
OptD,
OptE,
OptHelp,
OptSwitch,
}
impl Default for OptId { fn default() -> Self { OptHelp } }
// processed option id
let opt_id: Cell<OptId> = Default::default();
// file parsing index (1-based), 0 for option parsing mode
let mut file_i = 0;
parse_opts(
|opt, constr| {
match opt {
Short(c) => {
match c {
'a' => { opt_id.set(OptA); NoValueOpt },
'b' => { opt_id.set(OptB); ValueOpt },
'c' => { opt_id.set(OptC); ValueOpt },
'd' => { opt_id.set(OptD); ValueOpt },
'h' => { opt_id.set(OptHelp); NoValueOpt },
'-' => {
constr.not_in_group();
opt_id.set(OptSwitch);
NoValueOpt
},
_ => InfoCode::InvalidOpt,
}
},
Long(s) => {
match s.as_str() {
"long_a" => { opt_id.set(OptA); NoValueOpt },
"long_b" => { opt_id.set(OptB); ValueOpt },
"long_c" => { opt_id.set(OptC); ValueOpt },
"long_e" => { opt_id.set(OptE); ValueOpt },
"help" => { opt_id.set(OptHelp); NoValueOpt },
_ => InfoCode::InvalidOpt,
}
},
}
},
|opt, val| {
// if true, mode needs to be switched at the return of the handler
let mut switch_mode = false;
// 1st standalone value switches the parser into files parsing mode
if file_i <= 0 && opt.is_none() {
file_i = 1;
switch_mode = true;
}
if file_i <= 0 {
//
// Options parser
//
// Options w/o associated value
//
let mut handled = true;
match opt_id.get() {
// print help and exit
OptHelp => {
opts.help = Some(());
return Ok(ProcessCode::Break);
},
OptSwitch => {
file_i = 1;
return Ok(ProcessCode::ToggleParsingMode);
},
OptA => {
opts.a_opt = Some(());
},
_ => {
handled = false;
},
}
if handled {
return Ok(ProcessCode::Continue);
}
// Options w/associated string value
//
let val_str = &val.as_ref().unwrap().val;
handled = true;
match opt_id.get() {
OptC => {
opts.c_opt = Some(val_str.clone());
},
OptD => {
opts.d_opt = Some(val_str.clone());
},
OptE => {
opts.e_opt = Some(val_str.clone());
},
_ => {
handled = false;
},
}
if handled {
return Ok(ProcessCode::Continue);
}
// Options w/associated int value
//
let opt_ref = opt.as_ref().unwrap();
let val_i: i32 = val_str.parse().map_err(|_| {
ParseError::InvalidOpt(opt_ref.clone(),
"Integer expected".to_string())
})?;
match opt_id.get() {
OptB => {
if val_i >= -10 && val_i <= 10 {
opts.b_opt = Some(val_i);
} else {
return Err(ParseError::InvalidOpt(opt_ref.clone(),
"Integer in range [-10..10] required".to_string()));
}
},
_ => {},
}
Ok(ProcessCode::Continue)
} else {
//
// Files parser
//
let val_str = &val.as_ref().unwrap().val;
match file_i {
1 => {
opts.file1 = Some(val_str.clone());
},
2 => {
opts.file2 = Some(val_str.clone());
},
_ => {
return Err(ParseError::GenericErr(
"Invalid number of files".to_string()));
},
}
file_i += 1;
if switch_mode {
Ok(ToggleParsingMode)
} else {
Ok(Continue)
}
}
})
}
pub fn main() -> Result<(), i32>
{
// CLI provided options
let mut opts: Options = Default::default();
let rc = process_cmdopts(&mut opts);
if rc.is_ok() {
if std::env::args().len() <= 1 || opts.help.is_some() {
print_usage();
} else if opts.file1.is_none() {
eprintln!("[ERROR] FILE1 required");
return Err(2);
} else {
println!("{:?}", opts);
}
} else {
eprintln!("[ERROR] {}", rc.unwrap_err());
return Err(1);
}
Ok(())
}