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)
5fn compression() -> impl Parser<usize> {
6 any::<isize, _, _>("COMP", |x: isize| {
7 if (-9..=-1).contains(&x) {
8 Some(x.abs().try_into().unwrap())
9 } else {
10 None
11 }
12 })
13 .metavar(&[
14 ("-1", Style::Literal),
15 (" to ", Style::Text),
16 ("-9", Style::Literal),
17 ])
18 .help("Compression level")
19 .anywhere()
20}
More examples
examples/xorg.rs (line 23)
13fn toggle_options(meta: &'static str, name: &'static str, help: &'static str) -> impl Parser<bool> {
14 any(meta, move |s: String| {
15 if let Some(suf) = s.strip_prefix('+') {
16 (suf == name).then_some(true)
17 } else if let Some(suf) = s.strip_prefix('-') {
18 (suf == name).then_some(false)
19 } else {
20 None
21 }
22 })
23 .help(help)
24 .anywhere()
25}
examples/dd.rs (line 26)
16fn tag<T>(name: &'static str, meta: &str, help: &'static str) -> impl Parser<T>
17where
18 T: FromStr,
19 <T as std::str::FromStr>::Err: std::fmt::Display,
20{
21 // it is possible to parse OsString here and strip the prefix with
22 // `os_str_bytes` or a similar crate
23 any("", move |s: String| Some(s.strip_prefix(name)?.to_owned()))
24 // this defines custom metavar for the help message
25 .metavar(&[(name, Style::Literal), (meta, Style::Metavar)][..])
26 .help(help)
27 .anywhere()
28 .parse(|s| s.parse())
29}
examples/find.rs (line 44)
42fn exec() -> impl Parser<Option<Vec<OsString>>> {
43 let tag = literal("-exec")
44 .help("for every file find finds execute a separate shell command")
45 .anywhere();
46
47 let item = any::<OsString, _, _>("ITEM", |s| (s != ";").then_some(s))
48 .help("command with its arguments, find will replace {} with a file name")
49 .many();
50
51 let endtag = any::<String, _, _>(";", |s| (s == ";").then_some(()))
52 .help("anything after literal \";\" will be considered a regular option again");
53
54 construct!(tag, item, endtag)
55 .adjacent()
56 .map(|triple| triple.1)
57 .optional()
58}
59
60/// parses symbolic permissions `-perm -mode`, `-perm /mode` and `-perm mode`
61fn perm() -> impl Parser<Option<Perm>> {
62 fn parse_mode(input: &str) -> Result<Perms, String> {
63 let mut perms = Perms::default();
64 for c in input.chars() {
65 match c {
66 'r' => perms.read = true,
67 'w' => perms.write = true,
68 'x' => perms.exec = true,
69 _ => return Err(format!("{} is not a valid permission string", input)),
70 }
71 }
72 Ok(perms)
73 }
74
75 let tag = literal("-mode").anywhere();
76
77 // `any` here is used to parse an arbitrary string that can also start with dash (-)
78 // regular positional parser won't work here
79 let mode = any("MODE", Some)
80 .help("(perm | -perm | /perm), where perm is any subset of rwx characters, ex +rw")
81 .parse::<_, _, String>(|s: String| {
82 if let Some(m) = s.strip_prefix('-') {
83 Ok(Perm::All(parse_mode(m)?))
84 } else if let Some(m) = s.strip_prefix('/') {
85 Ok(Perm::Any(parse_mode(m)?))
86 } else {
87 Ok(Perm::Exact(parse_mode(&s)?))
88 }
89 });
90
91 construct!(tag, mode)
92 .adjacent()
93 .map(|pair| pair.1)
94 .optional()
95}
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)
5fn compression() -> impl Parser<usize> {
6 any::<isize, _, _>("COMP", |x: isize| {
7 if (-9..=-1).contains(&x) {
8 Some(x.abs().try_into().unwrap())
9 } else {
10 None
11 }
12 })
13 .metavar(&[
14 ("-1", Style::Literal),
15 (" to ", Style::Text),
16 ("-9", Style::Literal),
17 ])
18 .help("Compression level")
19 .anywhere()
20}
More examples
examples/dd.rs (line 25)
16fn tag<T>(name: &'static str, meta: &str, help: &'static str) -> impl Parser<T>
17where
18 T: FromStr,
19 <T as std::str::FromStr>::Err: std::fmt::Display,
20{
21 // it is possible to parse OsString here and strip the prefix with
22 // `os_str_bytes` or a similar crate
23 any("", move |s: String| Some(s.strip_prefix(name)?.to_owned()))
24 // this defines custom metavar for the help message
25 .metavar(&[(name, Style::Literal), (meta, Style::Metavar)][..])
26 .help(help)
27 .anywhere()
28 .parse(|s| s.parse())
29}
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)
31fn user() -> impl Parser<Option<String>> {
32 // match only literal "-user"
33 let tag = literal("-user").anywhere();
34 let value = positional("USER").help("User name");
35 construct!(tag, value)
36 .adjacent()
37 .map(|pair| pair.1)
38 .optional()
39}
40
41// parsers -exec xxx yyy zzz ;
42fn exec() -> impl Parser<Option<Vec<OsString>>> {
43 let tag = literal("-exec")
44 .help("for every file find finds execute a separate shell command")
45 .anywhere();
46
47 let item = any::<OsString, _, _>("ITEM", |s| (s != ";").then_some(s))
48 .help("command with its arguments, find will replace {} with a file name")
49 .many();
50
51 let endtag = any::<String, _, _>(";", |s| (s == ";").then_some(()))
52 .help("anything after literal \";\" will be considered a regular option again");
53
54 construct!(tag, item, endtag)
55 .adjacent()
56 .map(|triple| triple.1)
57 .optional()
58}
59
60/// parses symbolic permissions `-perm -mode`, `-perm /mode` and `-perm mode`
61fn perm() -> impl Parser<Option<Perm>> {
62 fn parse_mode(input: &str) -> Result<Perms, String> {
63 let mut perms = Perms::default();
64 for c in input.chars() {
65 match c {
66 'r' => perms.read = true,
67 'w' => perms.write = true,
68 'x' => perms.exec = true,
69 _ => return Err(format!("{} is not a valid permission string", input)),
70 }
71 }
72 Ok(perms)
73 }
74
75 let tag = literal("-mode").anywhere();
76
77 // `any` here is used to parse an arbitrary string that can also start with dash (-)
78 // regular positional parser won't work here
79 let mode = any("MODE", Some)
80 .help("(perm | -perm | /perm), where perm is any subset of rwx characters, ex +rw")
81 .parse::<_, _, String>(|s: String| {
82 if let Some(m) = s.strip_prefix('-') {
83 Ok(Perm::All(parse_mode(m)?))
84 } else if let Some(m) = s.strip_prefix('/') {
85 Ok(Perm::Any(parse_mode(m)?))
86 } else {
87 Ok(Perm::Exact(parse_mode(&s)?))
88 }
89 });
90
91 construct!(tag, mode)
92 .adjacent()
93 .map(|pair| pair.1)
94 .optional()
95}
More examples
examples/compression.rs (line 19)
5fn compression() -> impl Parser<usize> {
6 any::<isize, _, _>("COMP", |x: isize| {
7 if (-9..=-1).contains(&x) {
8 Some(x.abs().try_into().unwrap())
9 } else {
10 None
11 }
12 })
13 .metavar(&[
14 ("-1", Style::Literal),
15 (" to ", Style::Text),
16 ("-9", Style::Literal),
17 ])
18 .help("Compression level")
19 .anywhere()
20}
examples/xorg.rs (line 24)
13fn toggle_options(meta: &'static str, name: &'static str, help: &'static str) -> impl Parser<bool> {
14 any(meta, move |s: String| {
15 if let Some(suf) = s.strip_prefix('+') {
16 (suf == name).then_some(true)
17 } else if let Some(suf) = s.strip_prefix('-') {
18 (suf == name).then_some(false)
19 } else {
20 None
21 }
22 })
23 .help(help)
24 .anywhere()
25}
26
27// matches literal +ext and -ext followed by extension name
28fn extension() -> impl Parser<(String, bool)> {
29 let state = any("(+|-)ext", |s: String| match s.as_str() {
30 "-ext" => Some(false),
31 "+ext" => Some(true),
32 _ => None,
33 })
34 .anywhere();
35
36 let name = positional::<String>("EXT")
37 .help("Extension to enable or disable, see documentation for the full list");
38 construct!(state, name).adjacent().map(|(a, b)| (b, a))
39}
examples/dd.rs (line 27)
16fn tag<T>(name: &'static str, meta: &str, help: &'static str) -> impl Parser<T>
17where
18 T: FromStr,
19 <T as std::str::FromStr>::Err: std::fmt::Display,
20{
21 // it is possible to parse OsString here and strip the prefix with
22 // `os_str_bytes` or a similar crate
23 any("", move |s: String| Some(s.strip_prefix(name)?.to_owned()))
24 // this defines custom metavar for the help message
25 .metavar(&[(name, Style::Literal), (meta, Style::Metavar)][..])
26 .help(help)
27 .anywhere()
28 .parse(|s| s.parse())
29}
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 Read more
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