-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
[clap-v3-utils] Add replace deprecated value_of
and is_present
with get_one
and contains_id
#33184
[clap-v3-utils] Add replace deprecated value_of
and is_present
with get_one
and contains_id
#33184
Conversation
Codecov Report
@@ Coverage Diff @@
## master #33184 +/- ##
=======================================
Coverage 81.9% 81.9%
=======================================
Files 789 789
Lines 213988 214012 +24
=======================================
+ Hits 175392 175424 +32
+ Misses 38596 38588 -8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
might pay to add a doc comment to the non-try versions stating that they panic. fine to do in follow up though
Would it also make sense to deprecate the non-try versions? |
Yes, I think it would make sense to deprecate the non-try versions. I was actually thinking about doing that in this PR, but the |
please don't remove them outright, instead mark with |
Problem
The behavior of clap
ArgMatches::value_of
andArgMatches::is_present
in clap v2 and v3 are different making it hard to upgrade existing Solana cli tools (in particular the token-cli) from using clap v2 to clap v3.The behavior of
ArgMatches::value_of
differs in clap v2 and v3 as follows:ArgMatches::value_of
succeeds in retrieving the argument sayarg
, then it returnsSome(arg)
. If it fails in any other way, it returnsNone
.ArgMatches::value_of
succeeds in retrieving the argument, then it returnsSome(arg)
. If the queried argument id is valid (it was declared withArg::new(...)
when defining the command), but it was not provided by the user, the it returnsNone
. If the argument id is not valid, then it panics.The
ArgMatches::is_present
has the analogous behavior:arg
was provided by the user, thenis_present(arg)
returnstrue
. In all other cases, it returnsfalse
.true
. Ifarg
is valid, but was not provided by the user, then it returnsfalse
. Ifarg
is not valid, then it panics.So if we try to upgrade a cli tools from using
solana-clap-v2-utils
tosolana-clap-v3-utils
, the tests fails due to the panicking behavior introduced with v3.Also,
value_of
andis_present
are technically deprecated functions, but they are still used inclap-v3-utils
,solana-keygen
, andsolana-zk-keygen
. They should really be replaced withArgMatches::get_one
/Argmatches::try_get_one
andArgMatches::contains_id
/ArgMatches::try_contains_id
. In particular, thetry_get_one
andtry_contains_id
returns anOption<_>
type wrapped inside anotherResult<_,_>
type.try_get_one
andtry_contains_id
returnsOk(Some(arg_val))
andOk(true)
respectivelytry_get_one
andtry_contains_id
returnsOk(None)
andOk(false)
respectively.try_get_one
andtry_contains_id
both returnsMatchesError::UnknownArgument
.It would be nice to just replace
value_of
andis_present
withget_one
andcontains_id
inclap-v3-utils
, but unfortunately, the deprecated functions and the newer functions behave differently in a non-trivial way, which would break existing cli tools that rely onclap-v3-utils
.Summary of Changes
try_...
variants that usetry_get_one
ortry_contains_id
to return a suitable error when an argument is not valid.Box<dyn error::Error>
, I replaced the use ofvalue_of
andis_present
withtry_get_one
andcontains_id
. This does not alter the behavior of the functions other than potentially returning a suitable error when it would otherwise panic.This will unblock cli tools from upgrading to using clap-v3 without introducing breaking changes. On a follow-up PR, I will make some additional updates to
clap-v3-utils
to replace some deprecated functions.Fixes #