pub struct OptConstr(_);
Expand description
Option constraints.
Mutable reference to an OptConstr
object is passed to the opt_i()
hander to provide ability to define additional constraints on a parsed
option. The handler should call OptConstr
public methods for the object
to specify required constraints or remain the object untouched if no
constraints are required.
Implementations§
source§impl OptConstr
impl OptConstr
sourcepub fn not_in_group(&mut self)
pub fn not_in_group(&mut self)
Option may not occur inside a group. Applies only for short options.
Examples found in repository?
examples/advanced.rs (line 100)
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)
}
}
})
}
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for OptConstr
impl Send for OptConstr
impl Sync for OptConstr
impl Unpin for OptConstr
impl UnwindSafe for OptConstr
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more