-
Notifications
You must be signed in to change notification settings - Fork 36
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
Using permitted: restricts the allowed values that a end-user inputs to a pre-defined list #147
Conversation
…to a pre-defined list. For example: opt :name, type: string, permitted: ['Jack', 'Jill'] means the user can only do one of the following: $ mycmd -n Jack $ mycmd -n Jill It also works for multi-parameters $ mycmd -n Jack Jill
Can you show an example of what |
@@ -158,6 +158,17 @@ def test_help_has_grammatical_default_text | |||
assert help[1] =~ /Default/ | |||
assert help[2] =~ /default/ | |||
end | |||
|
|||
def test_help_has_grammatical_permitted_text | |||
parser.opt :arg1, 'description with period.', :type => :strings, :permitted => %w(foo bar) |
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.
Does this also work with other types that are not strings? At a minimum I was thinking :string type, but even something like an integer would be useful to constrain. If we can have other types constrained, I think we should also have some specs for those.
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.
@akhoury6 @Fryguy This functionality is was added in Optimist_XL (based on your code/idea that I merged in), see:
- Consider merging in optimist_xl #115
- [WIP] Merge in optimist_xl #116
- https://github.com/nanobowers/optimist_xl/?tab=readme-ov-file#permitted
- https://github.com/nanobowers/optimist_xl/blob/master/examples/permitted.rb
Along with some other enhancements to the idea including range / regex matching and permitted_response:
I do get that some of the optimist-xl features aren't for everyone, but if you decide to merge this permitted:
feature in it may be worth considering borrowing code (or at least the tests) from Optimist_XL
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.
If I had known you had an OptimistXL I would have just used that instead! For this and the other PRs too.
@Fryguy That's just a test, the code should work with all types. See Line 334.
|
Here's an example from optmist-xl. I must not have tested the --help on permitted range until now - it's kind of a mess and needs help. The way the regex types print is a little odd, but how Ruby does it, suggestions for improvement appreciated.
|
When you try to print a regex like that you're using the As for that long list of numbers... that one's a bit harder, but I can think of two ways to do it. Either
Edit: https://stackoverflow.com/questions/20847212/how-to-convert-an-array-of-number-into-ranges |
@nanobowers I believe you have merge rights here now, so I'll leave it up to you how you would like to proceed. Personally I'm ok merging this PR as a first pass, then moving to the more thorough optimist xl version afterwards. |
@akhoury6 Also note that optimist_xl has a thing called |
@Fryguy Yeah that sounds good, lets get this merged. I can add in the additional features related to this (range/regex support and permitted_response:) in a separate enhancement PR. |
@nanobowers @akhoury6 I merged this one after #149 , but didn't have it rerun the specs and now it's broken on Ruby 2.7. Can you take a look? |
Added: - Align the short and long forms into their own columns in the help output (#145 - thanks akhoury6) - Add support for DidYouMean when long options are spelled incorrectly (#150 - thanks nanobowers) - Using `permitted:` restricts the allowed values that a end-user inputs to a pre-defined list (#147 - thanks akhoury6) - Add exact_match to settings, defaulting to inexact matching (#154 - thanks nanobowers) - Add setting to disable implicit short options (#155 - thanks nanobowers) - Add alt longname and multiple char support (#151 - thanks nanobowers) - Permitted regexp/range support (#158, #159- thanks nanobowers) - Add some examples (#161 - thanks nanobowers) Changed: - Enable frozen_string_literal for future-ruby support (#149, #153 - thanks nanobowers) - Refactor constraints (#156 - thanks nanobowers) - Fix assert_raises to assert_raises_errmatch (#160 - thanks nanobowers)
Using
permitted:
when defining an option restricts the allowed values that a end-user inputs to a pre-defined list.Note: This is a continuation of an old PR which I accidentally closed by deleting my old fork (it was too old to use now). This is the PR. It is now rebased as requested, and I have added the tests, but I never got a final answer if you wanted it merged in this form or if you wanted to do something else. I've been using my own fork for the last few years and so far I haven't hit any issues with the code as written.
Defining an option like this...
...restricts the input to one of the following:
Any other input will raise a
CommandlineError
in line with other features.