Derive attribute that specializes a user-provided function that gets to modify Arg
#2991
Closed
2 tasks done
Labels
A-derive
Area: #[derive]` macro API
C-enhancement
Category: Raise on the bar on expectations
S-wont-fix
Status: Closed as there is no plan to fix this
Please complete the following tasks
Clap Version
master 879dd23
Describe your use case
I have a bunch of messages defined in protobuf, I'd like to make a CLI to send those messages -
clap
should be able to parse the messages from terminal. I generate rust structs from protobuf definitions using prost and I can automatically addclap_derive
attributes, but I can't specify the attributes flexibly enough to solve my use-case.Problematic cases are when one struct contains another optional struct:
When automatically adding attributes, I can differentiate between protobuf native (u32, bool, ..) types and complex types (another struct), but I don't know if the complex type is
Option<B>
orOption<C>
.What I want is to parse these complex types (for example
A::inner
) from from json string from the command line. The default value in this case should be None (empty string), which is simple enough to implement currently. But, I'd also like to generate an example json value for the Some case in the CLI help. But what annotation can I add to the fieldA::inner
, where I only know that its a optional struct, but don't know which struct type (B or C) it actually is?Its really easy for me to write a function to generate the string:
All I'd need to have some attribute in
clap_derive
to call that function and I don't need to know which type isA::inner
which should call something like this in the right place:
default_value_t
attribute actually does exactly the type-based fn specialization - I'd just like to be able to provide the generic fn and have it specialized by the field type.Describe the solution you'd like
I implemented something that would solve my problems here:
#2990
Alternatives, if applicable
No response
Additional Context
Conversation started on #2813
The text was updated successfully, but these errors were encountered: