This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
Derive attribute that specializes a user-provided function that gets to modify Arg
#233
Open
2 tasks done
Labels
Issue by marjakm
Thursday Nov 04, 2021 at 21:24 GMT
Originally opened as clap-rs/clap#2991
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:
clap-rs/clap#2990
Alternatives, if applicable
No response
Additional Context
Conversation started on #2813
The text was updated successfully, but these errors were encountered: