Function cmdopts::parse_opts
source · pub fn parse_opts<Fi, Fh>(opt_i: Fi, opt_h: Fh) -> Result<(), ParseError>where
Fi: FnMut(&CmdOpt, &mut OptConstr) -> InfoCode,
Fh: FnMut(&Option<CmdOpt>, &Option<OptVal>) -> Result<ProcessCode, ParseError>,
Expand description
Parse process command line options.
For every parsed option user’s provided option opt_i()
is called-back to
retrieve information about parsed option followed by call to opt_h()
to
handle that option.
Examples found in repository?
examples/basic.rs (lines 31-72)
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
pub fn main()
{
use CmdOpt::*;
use InfoCode::*;
use ProcessCode::*;
use ParseError::*;
use OptId::*;
use std::cell::Cell;
#[derive(Clone)]
#[derive(Copy)]
enum OptId { OptA, OptB }
impl Default for OptId { fn default() -> Self { OptA } }
let opt_id: Cell<OptId> = Default::default();
let rc = parse_opts(
// Options info provider
//
|opt, _| {
match opt {
Short(c) => {
match c {
// -a, option w/o value
'a' => { opt_id.set(OptA); NoValueOpt },
// -b, option w/value
'b' => { opt_id.set(OptB); ValueOpt },
_ => InfoCode::InvalidOpt,
}
},
Long(s) => {
match s.as_str() {
// long counterpart of the short options
"long_a" => { opt_id.set(OptA); NoValueOpt },
"long_b" => { opt_id.set(OptB); ValueOpt },
_ => InfoCode::InvalidOpt,
}
},
}
},
// Options handler
//
|opt, val| {
// all parsed tokens must form options
opt.as_ref().ok_or(GenericErr("Non-option detected".to_string()))?;
match opt_id.get() {
OptA => {
println!("A option");
}
OptB => {
println!("B option with {}", val.as_ref().unwrap().val);
},
};
Ok(ProcessCode::Continue)
});
if rc.is_err() {
eprintln!("[ERROR] {}", rc.unwrap_err());
}
}
More examples
examples/advanced.rs (lines 89-231)
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
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)
}
}
})
}