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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
use std::any::TypeId;
use crate::opt::Action;
#[allow(unused)]
use crate::opt::Cmd;
#[allow(unused)]
use crate::opt::Creator;
use crate::opt::Help;
use crate::opt::Index;
#[allow(unused)]
use crate::opt::Main;
use crate::opt::Opt;
#[allow(unused)]
use crate::opt::Pos;
use crate::opt::Style;
use crate::value::ErasedValue;
use crate::value::ValAccessor;
use crate::Error;
use crate::Str;
use crate::Uid;
/// A multiple features option type.
///
/// Some types support by default, they all implement [`Infer`](crate::value::Infer).
/// When create the option with `creator` using [`add_opt`](crate::set::OptSet::add_opt), the [`Creator`] will retrieve
/// other informations from the `infer type` list on the table.
/// When create the option from type using [`add_opt_i`](crate::set::OptSet::add_opt_i), the [`Creator`] will retrieve
/// other informations from given type.
///
/// | type | action | ignore name | styles | index | alias | default value | creator | infer type |
/// | -- | -- | - | -- | - | - | -- | - | -- |
/// | [`bool`] | [`Action::Set`] | `false` | [`Style::Combined`] [`Style::Boolean`] | no | true | false | `b` | [`bool`] |
/// | [`i32`] | [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | None | None |
/// | [`i64`] | [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | `i` | [`i64`] |
/// | [`u32`] | [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | None | None |
/// | [`u64`] | [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | `u` | [`u64`] |
/// | [`f32`] | [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | None | None |
/// | [`f64`] | [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | `f` | [`f64`] |
/// | [`usize`] | [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | None | None |
/// | [`isize`] | [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | None | None |
/// | [`String`]| [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | `s` | [`String`] |
/// | [`OsString`](std::ffi::OsString) | [`Action::App`] | `false` | [`Style::Argument`] | no | true | None | `r` | [`OsString`](std::ffi::OsString) |
/// | [`Cmd`] | [`Action::Set`] | `false` | [`Style::Cmd`] | [`Forward(1)`](Index::Forward) | true |false | `c` | [`Cmd`] |
/// | [`Pos`] | [`Action::App`] | `true` | [`Style::Pos`] | yes | false | None | `p` | [`Pos`] |
/// | [`Main`] | [`Action::Null`] | `true` | [`Style::Main`] | [`AnyWhere`](Index::AnyWhere) | false | None | `m` | [`Main`] |
/// | [`Stdin`](std::io::Stdin) | [`Action::Set`] | [`true`] | [`Style::Pos`] | [`AnyWhere`](Index::AnyWhere) | true | None | None | None |
///
///
/// For the value parser support, see [`RawValParser`](crate::value::RawValParser).
#[derive(Debug)]
pub struct AOpt {
uid: Uid,
name: Str,
r#type: TypeId,
help: Help,
styles: Vec<Style>,
index: Option<Index>,
accessor: ValAccessor,
alias: Option<Vec<Str>>,
action: Action,
matched: bool,
force: bool,
ignore_name: bool,
ignore_alias: bool,
ignore_index: bool,
}
impl AOpt {
pub fn new(name: Str, type_id: TypeId, accessor: ValAccessor) -> Self {
Self {
uid: 0,
name,
r#type: type_id,
help: Default::default(),
matched: false,
force: false,
action: Default::default(),
styles: vec![],
index: None,
accessor,
alias: None,
ignore_name: false,
ignore_alias: false,
ignore_index: false,
}
}
/// Set the unique identifier of option.
pub fn with_uid(mut self, uid: Uid) -> Self {
self.uid = uid;
self
}
/// Set the name of option.
pub fn with_name(mut self, name: Str) -> Self {
self.name = name;
self
}
/// Set the type of option, see [`Ctor`](crate::set::Ctor).
pub fn with_type(mut self, r#type: TypeId) -> Self {
self.r#type = r#type;
self
}
/// If the option will matching the name.
pub fn with_ignore_name(mut self, ignore_name: bool) -> Self {
self.ignore_name = ignore_name;
self
}
/// If the option will matching the alias.
pub fn with_ignore_alias(mut self, ignore_alias: bool) -> Self {
self.ignore_alias = ignore_alias;
self
}
/// If the option will matching the alias.
pub fn with_ignore_index(mut self, ignore_index: bool) -> Self {
self.ignore_index = ignore_index;
self
}
/// Set the hint of option, such as `--option`.
pub fn with_hint(mut self, hint: Str) -> Self {
self.help.set_hint(hint);
self
}
/// Set the help message of option.
pub fn with_help(mut self, help: Str) -> Self {
self.help.set_help(help);
self
}
/// Set the value action of option.
pub fn with_action(mut self, action: Action) -> Self {
self.action = action;
self
}
/// Set the help of option.
pub fn with_opt_help(mut self, help: Help) -> Self {
self.help = help;
self
}
/// Set the [`Style`] of option.
pub fn with_style(mut self, styles: Vec<Style>) -> Self {
self.styles = styles;
self
}
/// Set the NOA index of option.
pub fn with_idx(mut self, index: Option<Index>) -> Self {
self.index = index;
self
}
/// If the option is force required.
pub fn with_force(mut self, force: bool) -> Self {
self.force = force;
self
}
/// Set the alias of option.
pub fn with_alias(mut self, alias: Option<Vec<Str>>) -> Self {
self.alias = alias;
self
}
/// Set the value accessor of option, it will used by [`Policy`](crate::parser::Policy);
pub fn with_accessor(mut self, value: ValAccessor) -> Self {
self.accessor = value;
self
}
}
impl AOpt {
pub fn set_name(&mut self, name: Str) -> &mut Self {
self.name = name;
self
}
pub fn set_type(&mut self, r#type: TypeId) -> &mut Self {
self.r#type = r#type;
self
}
pub fn set_value(&mut self, value: ValAccessor) -> &mut Self {
self.accessor = value;
self
}
pub fn set_hint(&mut self, hint: Str) -> &mut Self {
self.help.set_hint(hint);
self
}
pub fn set_help(&mut self, help: Str) -> &mut Self {
self.help.set_help(help);
self
}
pub fn set_action(&mut self, action: Action) -> &mut Self {
self.action = action;
self
}
pub fn set_style(&mut self, styles: Vec<Style>) -> &mut Self {
self.styles = styles;
self
}
pub fn set_index(&mut self, index: Option<Index>) -> &mut Self {
self.index = index;
self
}
pub fn set_force(&mut self, force: bool) -> &mut Self {
self.force = force;
self
}
pub fn add_alias(&mut self, name: Str) -> &mut Self {
if let Some(alias) = &mut self.alias {
alias.push(name);
}
self
}
pub fn rem_alias(&mut self, name: &Str) -> &mut Self {
if let Some(alias) = &mut self.alias {
if let Some((i, _)) = alias.iter().enumerate().find(|(_, v)| v == &name) {
alias.remove(i);
}
}
self
}
}
impl Opt for AOpt {
fn reset(&mut self) {
self.set_matched(false);
}
fn uid(&self) -> Uid {
self.uid
}
fn name(&self) -> &Str {
&self.name
}
fn r#type(&self) -> &TypeId {
&self.r#type
}
fn hint(&self) -> &Str {
self.help.hint()
}
fn help(&self) -> &Str {
self.help.help()
}
fn valid(&self) -> bool {
!self.force() || self.matched()
}
fn matched(&self) -> bool {
self.matched
}
fn force(&self) -> bool {
self.force
}
fn action(&self) -> &Action {
&self.action
}
fn index(&self) -> Option<&Index> {
self.index.as_ref()
}
fn alias(&self) -> Option<&Vec<Str>> {
self.alias.as_ref()
}
fn accessor(&self) -> &ValAccessor {
&self.accessor
}
fn accessor_mut(&mut self) -> &mut ValAccessor {
&mut self.accessor
}
fn ignore_alias(&self) -> bool {
self.ignore_alias
}
fn ignore_name(&self) -> bool {
self.ignore_name
}
fn ignore_index(&self) -> bool {
self.ignore_index
}
fn set_uid(&mut self, uid: Uid) {
self.uid = uid;
}
fn set_matched(&mut self, matched: bool) {
self.matched = matched;
}
fn mat_style(&self, style: Style) -> bool {
self.styles.iter().any(|v| v == &style)
}
fn mat_force(&self, force: bool) -> bool {
self.force() == force
}
fn mat_name(&self, name: Option<&Str>) -> bool {
name.iter().all(|&v| v == self.name())
}
fn mat_alias(&self, name: &Str) -> bool {
if let Some(alias) = &self.alias {
alias.iter().any(|v| v == name)
} else {
false
}
}
fn mat_index(&self, index: Option<(usize, usize)>) -> bool {
if let Some((index, total)) = index {
if let Some(realindex) = self.index() {
if let Some(realindex) = realindex.calc_index(index, total) {
return realindex == index;
}
}
}
false
}
fn init(&mut self) -> Result<(), Error> {
self.accessor.initialize()
}
}