-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds argument suggestion support for unknown arguments
When an undefined prefixed argument is provided, the current default behavior is to ignore this unknown argument. Some command-line tools (Composer is an example) suggest alternative arguments based on a similarity between the unknown argument and those defined in the script. This modification adds this functionality to CLImate without breaking compatibility with previous versions and without this functionality being mandatory. The `League\CLImate\Argument\Manager::getUnknowPrefixedArgumentsAndSuggestions()` method returns an array with the unknown arguments as keys and the best argument suggestion as values, based on the PHP function [similar_text](https://www.php.net/manual/en/function.similar-text.php). With this result, the developer can easily implement a message to the user informing about the unknown argument and offering alternatives, as in this example: <?php require_once 'vendor/autoload.php'; $climate = new \League\CLImate\CLImate(); $climate->arguments->add([ 'user' => [ 'longPrefix' => 'user', ], 'password' => [ 'longPrefix' => 'password', ], 'flag' => [ 'longPrefix' => 'flag', 'noValue' => true, ], ]); $climate->arguments->parse(); $suggestions = $climate->arguments->getUnknowPrefixedArgumentsAndSuggestions(); if(count($suggestions) > 0){ $climate->error('Arguments not defined:'); foreach ($suggestions as $arg => $suggest){ if($suggest !== ''){ $climate->info("\"$arg\" is not defined. Did you mean these? $suggest"); } } } /* * Run: * * ~$ php .\test.php --user=baz --pass=123 --fag --xyz * * Return: * * Arguments not defined: * "pass" is not defined. Did you mean these? password * "fag" is not defined. Did you mean these? flag * */
- Loading branch information
Showing
3 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
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
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
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