-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add getters for option in InvalidOption and MissingOption classes #15048
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
base: master
Are you sure you want to change the base?
Conversation
Related to #9264, I'd say this sounds reasonable. |
@@ -85,12 +85,16 @@ class OptionParser | |||
end | |||
|
|||
class InvalidOption < Exception | |||
getter option | |||
|
|||
def initialize(option) |
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.
option
-> @option
ditto below
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.
Thank you.
Should we specify the String type?
getter option : String
I'm not sure I understand the changes here: you can already access the invalid/missing option via the |
@devnote-dev No, this could be accomplished with missing_option as you say. |
This is more a limitation of |
Rust has been successful in the area of command line tools. In my opinion, Crystal is also suited for this purpose. Don't give up on improving the standard library OptionParser... |
OptionParser is designed in an object-oriented manner, but it tries to behave in a completely transparent way. This might be the right strategy during the early stages of development because setting constraints can reduce bugs and guide users in the right direction. However, I think sticking to this approach until the end may limit its flexibility. I’m not sure which stage OptionParser is at now, but I believe this is an important point to consider for future improvements. This might seem unrelated at first, but it is actually closely connected to this pull request. When trying to use OptionParser.parse do |parser|
parser.banner = "Usage: salute [arguments]"
parser.on("-u", "--upcase", "Upcases the salute") { upcase = true }
parser.on("-t NAME", "--to=NAME", "Specifies the name to salute") { |name| destination = name }
parser.on("-h", "--help", "Show this help") do
puts parser
exit
end
parser.invalid_option do |flag|
STDERR.puts "ERROR: #{flag} is not a valid option."
STDERR.puts parser
exit(1)
end
end In this example, OptionParser is responsible for handling a major task: exiting the program when an invalid option is detected. But should OptionParser really be responsible for such a task? Personally, I think it would be better if it simply reported the error and left the decision to terminate the program to another component. I believe it would be more flexible if the parser ran once, and then another class handled any errors or decided whether the program should exit. Separating option parsing from post-parsing actions — this is what I want. This pull request is a small step in that direction. This text was translated from Japanese to English by ChatGPT 4o. |
Keep in mind that
The handler methods exist because of |
I wondered why you think that this is not related. |
Hi all
When an error occurs in OptionParser, I would like to be able to catch which option caused the error.
Could it be changed this way?