Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

Provide a Arg::custom_parse style function #88

Open
epage opened this issue Dec 6, 2021 · 2 comments
Open

Provide a Arg::custom_parse style function #88

epage opened this issue Dec 6, 2021 · 2 comments

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by kbknapp
Friday Feb 16, 2018 at 03:10 GMT
Originally opened as clap-rs/clap#1185


Something I've been thinking about for a while and mentioned in BurntSushi/ripgrep#809 is to add some additional functions which allow users to do something mid parse. From the linked issue:

The other way would involve something I've been thinking about, which is to provide companions to the validators, namely the ability to provide functions to Filter/Skip, Map, and Action arguments mid parse. These would do things such as do something potentially impure (Action), allow one to filter/skip values (Filter/Skip haven't decide on naming yet), or transform actual values themselves (Map) to be saved in the matches struct. These would all have access to the matches struct as it exists at that time, in order to perform decisions or possibly even mutate. I should say though that this method is further off and would not be available until v3 at the earliest (if ever, pending design and implementation, etc.).

However, now that I'm thinking about it, all three of those could actually be combined into a single Arg::custom_parse which accepts a function that takes the arg/value currently being parsed, and returns an Option<T> where None is skip this value (i.e. parse it as a different arg), or Some(T) is "save this to the matches" it could be the value as passed in, or it could be something different entirely (i.e. a map).

This could also require a few companion methods on the matches struct that allows mutation. I'd like to expose these on a newtype wrapper PartialMatches(ArgMatches) so as not to expose these mutation functions on a regular basis. Functions could be something like PartialMatches::remove_{value,arg}, PartialMatches::add_{value,arg}, etc. It would also have all the normal ArgMatches methods provided via a facade.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by Zooce
Tuesday Jul 16, 2019 at 15:55 GMT


This is exactly what I'm looking for. I have a use case where I'd like to give the user the ability to abbreviate a positional argument if they want to. I could then use the custom_parse (or whatever) function to see if they did abbreviate, and if so, map it to the full argument.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pksunkara
Wednesday May 26, 2021 at 10:57 GMT


Probably related #1880

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant