Skip to content

Conversation

@everton3x
Copy link
Contributor

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.

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
 *
 */ 

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
 *
 */
@duncan3dc
Copy link
Member

This sounds great @everton3x I'll take a proper look next week and see when we can get it merged

Adds getCurrent() and getTotal() to Progress.

Issue #192
@duncan3dc
Copy link
Member

I've merged this in as 9e8a8b1 now, thanks for your work!

I think this should be the default functionality, but we can use this optional version for a while to iron out any issues before making it on by default

@duncan3dc duncan3dc closed this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants