-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
-
I have a custom enum type (doesn't really matter in this case) which is parsed and produces custom error which I'd like to print to the user. When I use So by reading the code, the preferred method should be using I suggest to create a temporary trait or change it for new major version. PS: I also suggest to add a test with implementation of custom types (e.g. enums with a value, etc) for interfaces to show how it should be implemented from a user side. pub MyEnum {
Valued(u8),
AdditionalValue,
}
pub enum MyParseError {
NotU8
}
impl FromStr for MyEnum { /*....*/ } // obvious thing to implement, but will obscure the example below
imp Display for MyParseError { /*....*/ } // obvious thing to implement, but will obscure the example below
struct MyEnumParser {} // can't be the same enum
impl ValueParserFactory for MyEnum {
type Parser = MyEnumParser;
fn value_parser() -> Self::Parser {
MyEnumParser {}
}
}
impl TypedValueParser for MyEnumParser {
type Value = GradleVersion;
fn parse_ref(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
match value.to_str() {
Some(text) => MyEnum::from_str(text).map_err(move |err| {
let mut clap_error = cmd.error(ErrorKind::ValueValidation, err); // This won't ever compile because of API
if let Some(arg) = arg {
clap_error.insert(
ContextKind::InvalidArg,
ContextValue::String(arg.to_string()),
);
}
clap_error
}),
None => Err(clap::Error::new(ErrorKind::InvalidUtf8)),
}
}
} |
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment · 5 replies
-
Relevant discussions
Sounds like in your case, the crux of the problem is:
Could you expand on that more? Note that clap/clap_builder/src/error/mod.rs Lines 808 to 814 in 2c04acd
|
Beta Was this translation helpful? Give feedback.
All reactions
-
Thank you for links above. I use I have following notes and questions better to be addressed in official documentation.
FYI: Currently I've ended up with cloning |
Beta Was this translation helpful? Give feedback.
All reactions
-
That is reasonable for a file format like
It would be good to document the context for this. #6125 does so.
This allows for general parsers that can be configured to your needs, e.g.
The documentation for Arg::value_parser gives two examples of stateful For associating a parser with a type, we offer ValueParserFactory
We do support Now, how we document this could likely be improved. I'm looking around now
I made a small tweak in #6125. If you have thoughts on how to clarify the wording, I'd be open. It would reduce layers of indirection of we laid out all of these details up front but I'm trying to balance approachability with maintainability due to my limited time and we're also trying to help people understand the principles of how things operate so they can more easily reason about things and apply them in ways we don't consider how to write.
In terms of examples, we have
Yes, it would be ideal if we forked all of our documentation to reduce the need for knowing the builder API when it comes to the derive. See above about our balancing of needs and #4090.
There are likely ways around this but there seems to be a lack of context in what you are trying to accomplish for me to provide guidance on what to do. I asked for more in my previous post. If you are interested, feel free to respond to that question. |
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
Thank you for documentation links and improvement for I still curious in which cases Could you please add a task to add a complex example with In my particular case I've implemented How to make output the same between custom implementation and official implementation? pure
my implementation:
PS: I understand how hard to document such project properly and don't spend too much time on it! I really appreciate your help with |
Beta Was this translation helpful? Give feedback.
All reactions
-
We are ensuring the following has been run:
There is a lot there for me to specifically enumerate. To give just on example, this sets up some basics for usage string generation.
If there are use cases we aren't covering, I'd be willing to accept them. Looking around the projects on my system, I found one use of value parsers remotely like yours. I'm adding it in #6126. Whats interesting about this case is that its not easy to provide an error that fits both cases so we prioritize one and this feels like a time to prioritize the non-possible values.
You can put it in a
You can either wrap another format!("invalid value `{value}` for `{arg}`: `{err}`"); You could also use |
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
Thank you for your time and documentation effort. You've answered all my questions for now. |
Beta Was this translation helpful? Give feedback.
We are ensuring the following has been run:
clap/clap_builder/src/builder/command.rs
Lines 4375 to 4687 in 7566762