Closed mslinn closed 1 year ago
This is indeed a bug. Mind to send a PR?
I will leave that to others
The given case does print the following deprecation warning on thor v1.2.2:
Deprecation warning: Expected array default value for '--type'; got "tag" (string).
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
With check_default_type: true
set on the options, the validity of the default option is properly evaluated, resulting in the following error and a non-zero exit code:
Expected array default value for '--type'; got "tag" (string) (ArgumentError)
Given the presence of the deprecation warning, it appears that the usage of repeatable
is not intended to be used with a default that is anything other than an Array or Hash. Is this worth a patch given that this will presumably be validated in the future?
This sounds like you are asking if Strings should be able to be validated against an array of allowable values. I think it is obvious that this ability is valuable. Apparently, YMMV.
No, I don't think that that's a fair assessment of what I wrote, especially given my last paragraph:
it appears that the usage of repeatable is not intended to be used with a default that is anything other than an Array or Hash
To elaborate:
Expected '--type' to be one of tag, block, generator; got not-in-enum
This conclusion may be correct, but it is not helpful. Strings need to be constrained to a list of acceptable values also.
On 2023-06-12 9:45 p.m., Jennifer Page wrote:
it appears that the usage of repeatable is not intended to be used with a default that is anything other than an Array or Hash
Thank you for the investigation @jmpage.
@jmpage is correct. The option definition should be:
method_option :type, type: :string, default: ['tag'], enum: %w[tag block generator],
repeatable: true, desc: 'Specifies the types of plugin.'
Not
method_option :type, type: :string, default: 'tag', enum: %w[tag block generator],
repeatable: true, desc: 'Specifies the types of plugin.'
This demo program lives in a project with Thor v1.2.2 as a dependency:
When called this way:
Then
options
has value{"type"=>"tagblock"}
, which is an error.However, if
repeatable: false
(or is not specified), thenoptions
has value{"type"=>"block"}
, as it should.