mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
Moving oof flags to dedicated flags.rs
This commit is contained in:
parent
5b37a117f1
commit
b2d064bbb3
@ -1,4 +1,74 @@
|
|||||||
use std::ffi::OsStr;
|
use std::{
|
||||||
|
collections::{BTreeMap, BTreeSet},
|
||||||
|
ffi::{OsStr, OsString},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Shallow type, created to indicate a `Flag` that accepts a argument.
|
||||||
|
///
|
||||||
|
/// ArgFlag::long(), is actually a Flag::long(), but sets a internal attribute.
|
||||||
|
///
|
||||||
|
/// Examples in here pls
|
||||||
|
pub struct ArgFlag;
|
||||||
|
|
||||||
|
#[allow(clippy::new_ret_no_self)]
|
||||||
|
impl ArgFlag {
|
||||||
|
pub fn long(name: &'static str) -> Flag {
|
||||||
|
Flag {
|
||||||
|
long: name,
|
||||||
|
short: None,
|
||||||
|
takes_value: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub struct Flag {
|
||||||
|
// Also the name
|
||||||
|
pub long: &'static str,
|
||||||
|
pub short: Option<char>,
|
||||||
|
pub takes_value: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Flag {
|
||||||
|
pub fn long(name: &'static str) -> Self {
|
||||||
|
Self {
|
||||||
|
long: name,
|
||||||
|
short: None,
|
||||||
|
takes_value: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn short(mut self, short_flag_char: char) -> Self {
|
||||||
|
self.short = Some(short_flag_char);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Flags {
|
||||||
|
pub boolean_flags: BTreeSet<&'static str>,
|
||||||
|
pub argument_flags: BTreeMap<&'static str, OsString>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Flags {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Flags {
|
||||||
|
pub fn is_present(&self, flag_name: &str) -> bool {
|
||||||
|
self.boolean_flags.contains(flag_name) || self.argument_flags.contains_key(flag_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn arg(&self, flag_name: &str) -> Option<&OsString> {
|
||||||
|
self.argument_flags.get(flag_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn take_arg(&mut self, flag_name: &str) -> Option<OsString> {
|
||||||
|
self.argument_flags.remove(flag_name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub enum FlagType {
|
pub enum FlagType {
|
||||||
None,
|
None,
|
||||||
|
@ -8,13 +8,12 @@ mod flags;
|
|||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::{BTreeMap, BTreeSet},
|
collections::BTreeMap,
|
||||||
ffi::{OsStr, OsString},
|
ffi::{OsStr, OsString},
|
||||||
str,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use error::OofError;
|
pub use error::OofError;
|
||||||
pub use flags::FlagType;
|
pub use flags::{ArgFlag, Flag, FlagType, Flags};
|
||||||
use util::trim_double_hyphen;
|
use util::trim_double_hyphen;
|
||||||
|
|
||||||
/// Pop leading application `subcommand`, if valid.
|
/// Pop leading application `subcommand`, if valid.
|
||||||
@ -42,73 +41,6 @@ where
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shallow type, created to indicate a `Flag` that accepts a argument.
|
|
||||||
///
|
|
||||||
/// ArgFlag::long(), is actually a Flag::long(), but sets a internal attribute.
|
|
||||||
///
|
|
||||||
/// Examples in here pls
|
|
||||||
pub struct ArgFlag;
|
|
||||||
|
|
||||||
#[allow(clippy::new_ret_no_self)]
|
|
||||||
impl ArgFlag {
|
|
||||||
pub fn long(name: &'static str) -> Flag {
|
|
||||||
Flag {
|
|
||||||
long: name,
|
|
||||||
short: None,
|
|
||||||
takes_value: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
|
||||||
pub struct Flag {
|
|
||||||
// Also the name
|
|
||||||
pub long: &'static str,
|
|
||||||
pub short: Option<char>,
|
|
||||||
pub takes_value: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Flag {
|
|
||||||
pub fn long(name: &'static str) -> Self {
|
|
||||||
Self {
|
|
||||||
long: name,
|
|
||||||
short: None,
|
|
||||||
takes_value: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn short(mut self, short_flag_char: char) -> Self {
|
|
||||||
self.short = Some(short_flag_char);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
|
||||||
pub struct Flags {
|
|
||||||
pub boolean_flags: BTreeSet<&'static str>,
|
|
||||||
pub argument_flags: BTreeMap<&'static str, OsString>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Flags {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Flags {
|
|
||||||
pub fn is_present(&self, flag_name: &str) -> bool {
|
|
||||||
self.boolean_flags.contains(flag_name) || self.argument_flags.contains_key(flag_name)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn arg(&self, flag_name: &str) -> Option<&OsString> {
|
|
||||||
self.argument_flags.get(flag_name)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn take_arg(&mut self, flag_name: &str) -> Option<OsString> {
|
|
||||||
self.argument_flags.remove(flag_name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Detect flags from args and filter from args.
|
/// Detect flags from args and filter from args.
|
||||||
///
|
///
|
||||||
/// Each flag received via flags_info should must have unique long and short identifiers.
|
/// Each flag received via flags_info should must have unique long and short identifiers.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user