pub struct ParseAny<T> { /* private fields */ }
Expand description
Implementations§
source§impl<T> ParseAny<T>
impl<T> ParseAny<T>
sourcepub fn help<M: Into<Doc>>(self, help: M) -> Self
pub fn help<M: Into<Doc>>(self, help: M) -> Self
Examples found in repository?
examples/compression.rs (line 18)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn compression() -> impl Parser<usize> {
any::<isize, _, _>("COMP", |x: isize| {
if (-9..=-1).contains(&x) {
Some(x.abs().try_into().unwrap())
} else {
None
}
})
.metavar(&[
("-1", Style::Literal),
(" to ", Style::Text),
("-9", Style::Literal),
])
.help("Compression level")
.anywhere()
}
More examples
examples/xorg.rs (line 23)
13 14 15 16 17 18 19 20 21 22 23 24 25
fn toggle_options(meta: &'static str, name: &'static str, help: &'static str) -> impl Parser<bool> {
any(meta, move |s: String| {
if let Some(suf) = s.strip_prefix('+') {
(suf == name).then_some(true)
} else if let Some(suf) = s.strip_prefix('-') {
(suf == name).then_some(false)
} else {
None
}
})
.help(help)
.anywhere()
}
examples/dd.rs (line 26)
16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn tag<T>(name: &'static str, meta: &str, help: &'static str) -> impl Parser<T>
where
T: FromStr,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
// it is possible to parse OsString here and strip the prefix with
// `os_str_bytes` or a similar crate
any("", move |s: String| Some(s.strip_prefix(name)?.to_owned()))
// this defines custom metavar for the help message
.metavar(&[(name, Style::Literal), (meta, Style::Metavar)][..])
.help(help)
.anywhere()
.parse(|s| s.parse())
}
examples/find.rs (line 44)
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
fn exec() -> impl Parser<Option<Vec<OsString>>> {
let tag = literal("-exec")
.help("for every file find finds execute a separate shell command")
.anywhere();
let item = any::<OsString, _, _>("ITEM", |s| (s != ";").then_some(s))
.help("command with its arguments, find will replace {} with a file name")
.many();
let endtag = any::<String, _, _>(";", |s| (s == ";").then_some(()))
.help("anything after literal \";\" will be considered a regular option again");
construct!(tag, item, endtag)
.adjacent()
.map(|triple| triple.1)
.optional()
}
/// parses symbolic permissions `-perm -mode`, `-perm /mode` and `-perm mode`
fn perm() -> impl Parser<Option<Perm>> {
fn parse_mode(input: &str) -> Result<Perms, String> {
let mut perms = Perms::default();
for c in input.chars() {
match c {
'r' => perms.read = true,
'w' => perms.write = true,
'x' => perms.exec = true,
_ => return Err(format!("{} is not a valid permission string", input)),
}
}
Ok(perms)
}
let tag = literal("-mode").anywhere();
// `any` here is used to parse an arbitrary string that can also start with dash (-)
// regular positional parser won't work here
let mode = any("MODE", Some)
.help("(perm | -perm | /perm), where perm is any subset of rwx characters, ex +rw")
.parse::<_, _, String>(|s: String| {
if let Some(m) = s.strip_prefix('-') {
Ok(Perm::All(parse_mode(m)?))
} else if let Some(m) = s.strip_prefix('/') {
Ok(Perm::Any(parse_mode(m)?))
} else {
Ok(Perm::Exact(parse_mode(&s)?))
}
});
construct!(tag, mode)
.adjacent()
.map(|pair| pair.1)
.optional()
}
sourcepub fn metavar<M: Into<Doc>>(self, metavar: M) -> Self
pub fn metavar<M: Into<Doc>>(self, metavar: M) -> Self
Replace metavar with a custom value
See examples in any
Examples found in repository?
examples/compression.rs (lines 13-17)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn compression() -> impl Parser<usize> {
any::<isize, _, _>("COMP", |x: isize| {
if (-9..=-1).contains(&x) {
Some(x.abs().try_into().unwrap())
} else {
None
}
})
.metavar(&[
("-1", Style::Literal),
(" to ", Style::Text),
("-9", Style::Literal),
])
.help("Compression level")
.anywhere()
}
More examples
examples/dd.rs (line 25)
16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn tag<T>(name: &'static str, meta: &str, help: &'static str) -> impl Parser<T>
where
T: FromStr,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
// it is possible to parse OsString here and strip the prefix with
// `os_str_bytes` or a similar crate
any("", move |s: String| Some(s.strip_prefix(name)?.to_owned()))
// this defines custom metavar for the help message
.metavar(&[(name, Style::Literal), (meta, Style::Metavar)][..])
.help(help)
.anywhere()
.parse(|s| s.parse())
}
sourcepub fn anywhere(self) -> Self
pub fn anywhere(self) -> Self
Try to apply the parser to each unconsumed element instead of just the front one
By default any
tries to parse just the front unconsumed item behaving similar to
positional
parser, anywhere
changes it so it applies to every unconsumed item,
similar to argument parser.
See examples in any
Examples found in repository?
examples/find.rs (line 33)
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
fn user() -> impl Parser<Option<String>> {
// match only literal "-user"
let tag = literal("-user").anywhere();
let value = positional("USER").help("User name");
construct!(tag, value)
.adjacent()
.map(|pair| pair.1)
.optional()
}
// parsers -exec xxx yyy zzz ;
fn exec() -> impl Parser<Option<Vec<OsString>>> {
let tag = literal("-exec")
.help("for every file find finds execute a separate shell command")
.anywhere();
let item = any::<OsString, _, _>("ITEM", |s| (s != ";").then_some(s))
.help("command with its arguments, find will replace {} with a file name")
.many();
let endtag = any::<String, _, _>(";", |s| (s == ";").then_some(()))
.help("anything after literal \";\" will be considered a regular option again");
construct!(tag, item, endtag)
.adjacent()
.map(|triple| triple.1)
.optional()
}
/// parses symbolic permissions `-perm -mode`, `-perm /mode` and `-perm mode`
fn perm() -> impl Parser<Option<Perm>> {
fn parse_mode(input: &str) -> Result<Perms, String> {
let mut perms = Perms::default();
for c in input.chars() {
match c {
'r' => perms.read = true,
'w' => perms.write = true,
'x' => perms.exec = true,
_ => return Err(format!("{} is not a valid permission string", input)),
}
}
Ok(perms)
}
let tag = literal("-mode").anywhere();
// `any` here is used to parse an arbitrary string that can also start with dash (-)
// regular positional parser won't work here
let mode = any("MODE", Some)
.help("(perm | -perm | /perm), where perm is any subset of rwx characters, ex +rw")
.parse::<_, _, String>(|s: String| {
if let Some(m) = s.strip_prefix('-') {
Ok(Perm::All(parse_mode(m)?))
} else if let Some(m) = s.strip_prefix('/') {
Ok(Perm::Any(parse_mode(m)?))
} else {
Ok(Perm::Exact(parse_mode(&s)?))
}
});
construct!(tag, mode)
.adjacent()
.map(|pair| pair.1)
.optional()
}
More examples
examples/compression.rs (line 19)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn compression() -> impl Parser<usize> {
any::<isize, _, _>("COMP", |x: isize| {
if (-9..=-1).contains(&x) {
Some(x.abs().try_into().unwrap())
} else {
None
}
})
.metavar(&[
("-1", Style::Literal),
(" to ", Style::Text),
("-9", Style::Literal),
])
.help("Compression level")
.anywhere()
}
examples/xorg.rs (line 24)
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
fn toggle_options(meta: &'static str, name: &'static str, help: &'static str) -> impl Parser<bool> {
any(meta, move |s: String| {
if let Some(suf) = s.strip_prefix('+') {
(suf == name).then_some(true)
} else if let Some(suf) = s.strip_prefix('-') {
(suf == name).then_some(false)
} else {
None
}
})
.help(help)
.anywhere()
}
// matches literal +ext and -ext followed by extension name
fn extension() -> impl Parser<(String, bool)> {
let state = any("(+|-)ext", |s: String| match s.as_str() {
"-ext" => Some(false),
"+ext" => Some(true),
_ => None,
})
.anywhere();
let name = positional::<String>("EXT")
.help("Extension to enable or disable, see documentation for the full list");
construct!(state, name).adjacent().map(|(a, b)| (b, a))
}
examples/dd.rs (line 27)
16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn tag<T>(name: &'static str, meta: &str, help: &'static str) -> impl Parser<T>
where
T: FromStr,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
// it is possible to parse OsString here and strip the prefix with
// `os_str_bytes` or a similar crate
any("", move |s: String| Some(s.strip_prefix(name)?.to_owned()))
// this defines custom metavar for the help message
.metavar(&[(name, Style::Literal), (meta, Style::Metavar)][..])
.help(help)
.anywhere()
.parse(|s| s.parse())
}
Trait Implementations§
source§impl<T> Parser<T> for ParseAny<T>
impl<T> Parser<T> for ParseAny<T>
source§fn collect<C>(self) -> ParseCollect<Self, C, T>where
C: FromIterator<T>,
Self: Sized,
fn collect<C>(self) -> ParseCollect<Self, C, T>where
C: FromIterator<T>,
Self: Sized,
Transform parser into a collection parser Read more
source§fn optional(self) -> ParseOptional<Self>
fn optional(self) -> ParseOptional<Self>
Turn a required argument into an optional one Read more
source§fn count(self) -> ParseCount<Self, T>
fn count(self) -> ParseCount<Self, T>
Count how many times the inner parser succeeds, and return that number. Read more
source§fn last(self) -> ParseLast<Self>
fn last(self) -> ParseLast<Self>
Apply the inner parser as many times as it succeeds, return the last value Read more
source§fn parse<F, R, E>(self, f: F) -> ParseWith<T, Self, F, E, R>
fn parse<F, R, E>(self, f: F) -> ParseWith<T, Self, F, E, R>
Apply a failing transformation to a contained value Read more
source§fn map<F, R>(self, map: F) -> ParseMap<T, Self, F, R>
fn map<F, R>(self, map: F) -> ParseMap<T, Self, F, R>
Apply a pure transformation to a contained value Read more
source§fn guard<F>(self, check: F, message: &'static str) -> ParseGuard<Self, F>
fn guard<F>(self, check: F, message: &'static str) -> ParseGuard<Self, F>
Validate or fail with a message Read more
source§fn fallback(self, value: T) -> ParseFallback<Self, T>
fn fallback(self, value: T) -> ParseFallback<Self, T>
Use this value as default if the value isn’t present on a command line Read more
source§fn fallback_with<F, E>(self, fallback: F) -> ParseFallbackWith<T, Self, F, E>
fn fallback_with<F, E>(self, fallback: F) -> ParseFallbackWith<T, Self, F, E>
Use value produced by this function as default if the value isn’t present Read more
source§fn hide(self) -> ParseHide<Self>
fn hide(self) -> ParseHide<Self>
Ignore this parser during any sort of help generation Read more
source§fn hide_usage(self) -> ParseUsage<Self>
fn hide_usage(self) -> ParseUsage<Self>
Ignore this parser when generating a usage line Read more
source§fn custom_usage<M>(self, usage: M) -> ParseUsage<Self>
fn custom_usage<M>(self, usage: M) -> ParseUsage<Self>
Customize how this parser looks like in the usage line
source§fn group_help<M: Into<Doc>>(self, message: M) -> ParseGroupHelp<Self>
fn group_help<M: Into<Doc>>(self, message: M) -> ParseGroupHelp<Self>
Attach a help message to a complex parser Read more
source§fn with_group_help<F>(self, f: F) -> ParseWithGroupHelp<Self, F>
fn with_group_help<F>(self, f: F) -> ParseWithGroupHelp<Self, F>
source§fn complete<M, F>(self, op: F) -> ParseComp<Self, F>
fn complete<M, F>(self, op: F) -> ParseComp<Self, F>
Available on crate feature
autocomplete
only.Dynamic shell completion Read more
source§fn complete_shell(self, op: ShellComp) -> ParseCompShell<Self>
fn complete_shell(self, op: ShellComp) -> ParseCompShell<Self>
Available on crate feature
autocomplete
only.Static shell completion Read more
source§fn to_options(self) -> OptionParser<T>
fn to_options(self) -> OptionParser<T>
Auto Trait Implementations§
impl<T> Freeze for ParseAny<T>
impl<T> !RefUnwindSafe for ParseAny<T>
impl<T> !Send for ParseAny<T>
impl<T> !Sync for ParseAny<T>
impl<T> Unpin for ParseAny<T>
impl<T> !UnwindSafe for ParseAny<T>
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