-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compute argument possible values based on other arguments #1910
Comments
I think this can be achieved by the hooks proposal mentioned in #1471. |
Thanks @pksunkara for looking into it. I see how #1471 is related, as it suggests to have a closure returning the default value of an argument. This issue remains unique in the following ways:
|
It is related because we can extend the hook proposal to allow defining possible values too. Prompts is a tangential issue. Auto completion will probably be tied into the dynamic completion issue. |
To illustrate the issue, here's a link to the project I'm working on: https://github.com/fbecart/zinoma/blob/9d2ea034d566a92a7464a7ca58c0b75cb87d7072/src/main.rs#L32-L37 |
Also potentially related #1880 |
Closely related to #1232, if not duplicate. |
In #2832, I layout an idea for generalizing clap. This has the potential for opening the door to something like this. With that said, I would generally recommend not doing this. Parsing a file sufficiently to know what future args should be during the parsing of the args feels like there is a can of worms of problems with it. I'm assuming If you really want In favor of those options I listed, I'm leaning towards closing this. If there are concerns that are not covered, feel free to speak up! |
I would like to be able to compute the possible values of an argument dynamically, based on the values provided for some other argument(s).
Use case
I'm working on a CLI which has a goal similar to
make
.The file lists the available targets. Given a value of
FILE
, I am able to load a list of available target names. I would like to use that list as the possible values fortarget
.This would affect the validation of
target
. Ideally, this would also affect its autocompletion.Currently,
possible_values
is expected to be provided as an array slice (&[&str]
) as theApp
is being built.clap/src/build/arg/mod.rs
Line 1712 in 9d03c84
At that time, the value of
FILE
is not available yet.Wanted solution
I would like instead to be able to provide a closure that would compute the possible values for my argument. This closure would take the values of the other app arguments as a parameter.
Workaround
It is already possible to obtain this behavior by parsing the arguments twice (the second time with refined validation):
However, this workaround doubles the overhead of parsing arguments. Also, this solution does not cover the autocompletion part.
Related issues
The text was updated successfully, but these errors were encountered: