Skip to content

Commit b102ed4

Browse files
committed
document new feature and add example / test
1 parent 48ba2d8 commit b102ed4

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

docs/design/problems_with_clap.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ uutils and when we opened as issue for it, it was discarded. This makes sense
108108
from `clap`'s perspective, but it shows that the priorities between `clap` and
109109
uutils diverge.
110110

111-
## Problem 6: It's stringly typed
111+
## Problem 7: It's stringly typed
112112

113113
`clap`'s arguments are identified by strings. This leads to code like this:
114114

@@ -135,21 +135,31 @@ deal, but a bit annoying.
135135

136136
Of course, we wouldn't have this problem if we were able to use the derive API.
137137

138-
## Problem 7: Reading help string from a file
138+
## Problem 8: Reading help string from a file
139139

140140
In `uutils` our help strings can get quite long. Therefore, we like to extract
141141
those to an external file. With `clap` this means that we need to do some custom
142142
preprocessing on this file to extract the information for the several pieces of
143143
the help string that `clap` supports.
144144

145-
## Problem 8: No markdown support
145+
## Problem 9: No markdown support
146146

147147
Granted, this is not really a problem, but more of a nice-to-have. We have
148148
online documentation for the utils, based on the help strings and these are
149149
rendered from markdown. Ideally, our argument parser supports markdown too, so
150150
that we can have nicely rendered help strings which have (roughly) the same
151151
appearance in the terminal and online.
152152

153+
## Problem 10: No position-dependent argument-error prioritization
154+
155+
This is the question of which error to print if both `-A` and `-B` are given,
156+
and both are individually an error somehow. In case of the GNU tools, only the
157+
first error is printed, and then the program is aborted.
158+
159+
This also is not really a problem, but since it can be reasonably easily
160+
achieved by simply raising an error during argument application, this enables
161+
matching more closely the exact behavior of the GNU tools.
162+
153163
## Good things about `clap`
154164

155165
Alright, enough problems. Let's praise `clap` a bit, because it's an excellent

tests/flags.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,34 @@ fn enum_flag() {
517517
SomeEnum::Baz,
518518
);
519519
}
520+
521+
#[test]
522+
fn simple_error() {
523+
#[derive(Arguments)]
524+
enum Arg {
525+
#[arg("-f", "--foo")]
526+
Foo,
527+
}
528+
529+
#[derive(Debug, Default)]
530+
struct Settings {}
531+
532+
impl Options<Arg> for Settings {
533+
fn apply(&mut self, _arg: Arg) -> Result<(), uutils_args::Error> {
534+
Err(uutils_args::Error {
535+
exit_code: 42,
536+
kind: uutils_args::ErrorKind::UnexpectedArgument("This is an example error".to_owned()),
537+
})
538+
}
539+
}
540+
541+
let settings_or_error = Settings::default().parse(["test", "-f"]);
542+
let the_error = settings_or_error.expect_err("should have propagated error");
543+
assert_eq!(the_error.exit_code, 42);
544+
match the_error.kind {
545+
uutils_args::ErrorKind::UnexpectedArgument(err_str) => {
546+
assert_eq!(err_str, "This is an example error")
547+
}
548+
_ => panic!("wrong error kind: {:?}", the_error.kind),
549+
}
550+
}

0 commit comments

Comments
 (0)