PHP code example of tolidano / commandox

1. Go to this page and download the library: Download tolidano/commandox library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tolidano / commandox example snippets

 php
 declare(strict_types = 1);

;

// Define first option
$hello_cmd->option()
    ->ion('t')
    ->aka('title')
    ->describedAs('When set, use this title to address the person')
    ->must(function($title) {
        $titles = ['Mister', 'Mr', 'Misses', 'Mrs', 'Miss', 'Ms'];
        return in_array($title, $titles);
    })
    ->map(function($title) {
        $titles = ['Mister' => 'Mr.', 'Misses' => 'Mrs.', 'Miss' => 'Ms.'];
        if (array_key_exists($title, $titles)) {
            $title = $titles[$title];
        }
        return $title;
    });

// Define a boolean flag "-c" aka "--capitalize"
$hello_cmd->option('c')
    ->aka('capitalize')
    ->aka('cap')
    ->describedAs('Always capitalize the words in a name')
    ->boolean();

// Define an incremental flag "-e" aka "--educate"
$hello_cmd->option('e')
    ->aka('educate')
    ->map(function ($value) {
        $postfix = ['', 'Jr.', 'Esq.', 'PhD'];
        return $postfix[$value];
    })
    ->count(4);

$name = $hello_cmd['capitalize'] ? ucwords($hello_cmd[0]) : $hello_cmd[0];

print("Hello {$hello_cmd['title']} $name {$hello_cmd['educate']}!" . PHP_EOL);

php command.php -f value1 --long value2 value3 value4 value5