This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Adding Zend\Console\Getopt option callback hooks #5713
Merged
Merged
Conversation
This file contains 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
* @param array $rules | ||
* @param array $argv | ||
* @param array $getoptConfig | ||
* @param array $rules |
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.
Please roll back all changes not related with the fix
Removed the stylistic changes for the method param docblocks. |
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
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
@todo
annotations present in this class, I've added the ability to use callbacks when options are parsed. This feature addresses several of these@todo
annotations.(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
apples
is passed, either by it's short forma
or 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=Gala
on the command line. Furthermore, the value "Gala" will be passed by value to the first callback parameter.(2) Invalidating usage by returning
false
Using 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
Getopt
you'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=apple
will trigger theapple
requirement. You can use callbacks to accomplish conditional logic in this manner.Other
@todo
requestsHandle flags that implicitly print usage message, e.g. --help
: this can easily be accomplished using a callback. SinceGetopt
class should never be dealing without output, the way to deal with this would be to attach a callback that throws theExeption\RuntimeException
method 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: