Skip to content
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

feat: Adds argument suggestion support for unknown arguments #204

Closed
wants to merge 2 commits into from

Commits on Oct 25, 2024

  1. 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
     *
     */
    everton3x committed Oct 25, 2024
    Configuration menu
    Copy the full SHA
    06d2936 View commit details
    Browse the repository at this point in the history

Commits on Oct 28, 2024

  1. fet: Adds getCurrent() and getTotal() to Progress.

    Adds getCurrent() and getTotal() to Progress.
    
    Issue #192
    everton3x committed Oct 28, 2024
    Configuration menu
    Copy the full SHA
    996d4a0 View commit details
    Browse the repository at this point in the history