PHP code example of krypt0nn / consoleargs

1. Go to this page and download the library: Download krypt0nn/consoleargs 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/ */

    

krypt0nn / consoleargs example snippets




namespace ConsoleArgs;

(new Manager ([
    new Command ('hello', function ()
    {
        echo 'Hello, World!';
    })
]))->execute (array_slice ($argv, 1));

// array_slice нужен чтобы отрезать аргумент вызова файла из консоли



namespace ConsoleArgs;

(new Manager ([
    new Command ('write', function ($args)
    {
        echo implode (' ', $args);
    })
]))->execute (array_slice ($argv, 1));



namespace ConsoleArgs;

(new Manager ([
    (new Command ('write', function ($args, $params)
    {
        // Если было указано несколько одинаковых параметров, то будет указан список всех введённых значений
        // Поэтому это так же нужно предусмотреть:
        if (is_array ($params['--glue']))
            $params['--glue'] = $params['--glue'][0];
        
        echo $params['--base64'] ?
            base64_encode (implode ($params['--glue'], $args)) :
            implode ($params['--glue'], $args);
    }))->addParams ([
        // Первый аргумент - название параметра
        // Второй аргумент (не обязательный) - значение по умолчанию
        // Третий аргумент (не обязательный) - обязательно ли нужно использовать данный параметр
        new Param ('--glue', ' '),

        // Аргумент - название флага
        // "-b64" - алиас флага (альтернативное название)
        (new Flag ('--base64'))->addAlias ('-b64')
    ])
], new DefaultCommand (function ($args)
{
    echo 'Command "'. $args[0] .'" not founded. You should write correct command name';
})))->execute (array_slice ($argv, 1));



namespace ConsoleArgs;

(new Manager ([
    (new Command ('test', function ($args)
    {
        (new Manager ([
            new Command ('1', function ()
            {
                echo 'Enfesto Studio'. PHP_EOL;
            }),

            new Command ('2', function ()
            {
                echo 'Every Software'. PHP_EOL;
            })
        ]))->execute ($args);
    }))->addAlias ('alias_test')
]))->execute (array_slice ($argv, 1));



namespace ConsoleArgs;

$manager = new Manager ([
    (new Command ('write', function ($args, $params)
    {
        if (is_array ($params['--glue']))
            $params['--glue'] = $params['--glue'][0];
        
        echo implode ($params['--glue'], $args);
    }))
        ->setDescription ('Output entered message')
        ->addParams ([
            (new Param ('--glue', ' '))->addAlias ('-g')
        ])
]);

$manager
    ->addCommand (new HelpCommand ($manager))
    ->execute (array_slice ($argv, 1));

php index.php hello

php index.php write kek lol arbidol

php index.php write kek lol arbidol

php index.php write kek lol arbidol --glue ", "

php index.php write kek lol arbidol --glue ", " --base64

php index.php test 1

php index.php alias_test 2

php index.php help