This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Adding Zend\Console\Getopt option callback hooks#5713
Merged
Conversation
Member
There was a problem hiding this comment.
Please roll back all changes not related with the fix
Contributor
Author
|
Removed the stylistic changes for the method param docblocks. |
Member
|
I love this idea! Looking forward to reviewing this for 2.3.0. |
weierophinney
added a commit
that referenced
this pull request
Mar 4, 2014
Adding Zend\Console\Getopt option callback hooks
weierophinney
added a commit
that referenced
this pull request
Mar 4, 2014
weierophinney
added a commit
that referenced
this pull request
Mar 4, 2014
weierophinney
added a commit
to zendframework/zend-console
that referenced
this pull request
May 15, 2015
…getopt Adding Zend\Console\Getopt option callback hooks
weierophinney
added a commit
to zendframework/zend-console
that referenced
this pull request
May 15, 2015
weierophinney
added a commit
to zendframework/zend-console
that referenced
this pull request
May 15, 2015
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Based on some
@todoannotations present in this class, I've added the ability to use callbacks when options are parsed. This feature addresses several of these@todoannotations.(1) Triggering callbacks for options when present
You can now trigger a callback for any option that is present. For example, suppose you wish to trigger a callback when the option
applesis passed, either by it's short formaor by it's aliasapple. Here is how you could do this:As soon as you call
$opts->parse()the callback will be called assuming you passed-a Gala,--apple=Gala, or--apples=Galaon the command line. Furthermore, the value "Gala" will be passed by value to the first callback parameter.(2) Invalidating usage by returning
falseUsing a callback, you can return false to indicate that there is an issue with the way an option was used. Perhaps there is a dependance on some other value that must be present. For example:
Notice that you can specify a 2nd argument to the callback which is the instance of
Getoptyou're currently working with. This allows you to check on other command line parameters, should you need to.This also accomplishes conditionally required parameters from the command line. Here, both options are technically optional, but specifying
--fruit=applewill trigger theapplerequirement. You can use callbacks to accomplish conditional logic in this manner.Other
@todorequestsHandle flags that implicitly print usage message, e.g. --help: this can easily be accomplished using a callback. SinceGetoptclass should never be dealing without output, the way to deal with this would be to attach a callback that throws theExeption\RuntimeExceptionmethod with the usage information. You can then use exception handling to print out the error message in the existing error handling for console apps:Enable user to specify header and footer content in the help message.: This class does not deal with output. The previous example could simply add header/footer information to the printing of the exception message and usage message wherever appropriate. This would simply be the programmers choice.Excerpt of class doc block: