PHP code example of d0riven / php-flags

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

    

d0riven / php-flags example snippets



use PhpFlags\Parser;
use PhpFlags\Spec\ApplicationSpec;

// example ping
$spec = ApplicationSpec::create();
$spec->version('1.0.0')->clearShort();
$count = $spec->flag('count')->short('c')->default(-1)
    ->desc('Number of times to send an ICMP request. The default of -1 sends an unlimited number of requests.')
    ->validRule(function($count) {
        return $count >= -1;
    })
    ->int('request count');
$timeout = $spec->flag('timeout')->short('t')->default(5)
    ->desc('Timeout seconds for ICMP requests.')
    ->validRule(function($timeout) {
        return $timeout >= 0;
    })
    ->int('request count');
$verbose = $spec->flag('verbose')->short('v')
    ->desc('verbose output.')
    ->bool();
$host = $spec->arg()
    ->desc('IP of the host for the ICMP request.')
    ->validRule(function($ip){
        return preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', $ip);
    })
    ->string('host');
try {
    Parser::create($spec)->parse($argv);
} catch (PhpFlags\InvalidArgumentsException $e) {
    echo $e->getMessage(), PHP_EOL;
    exit(1);
} catch (PhpFlags\InvalidSpecException $e) {
    echo $e->getMessage(), PHP_EOL;
    exit(1);
}
echo "  count: ", $count->get(), PHP_EOL;
echo "timeout: ", $timeout->get(), PHP_EOL;
echo "verbose: ", $verbose->get() ? 'true' : 'false', PHP_EOL;
echo "   host: ", $host->get(), PHP_EOL;



use PhpFlags\Spec\ApplicationSpec;

$appSpec = ApplicationSpec::create();
$appSpec->help()->long('show-help')->short('s')
    ->action(function ($helpMessage) {
        fputs(STDERR, $helpMessage);
        exit(1);
    });
$appSpec->flag('example-flag')->desc('example flag declaration')->int();
$appSpec->arg()->desc('example arg declaration')->int('EXAMPLE-ARG');
// ...



use PhpFlags\Spec\ApplicationSpec;

$appSpec = ApplicationSpec::create();
$appSpec->version('1.0')->long('ver')->short('V')
    ->format('app version: {{VERSION}}')
    ->action(function ($versionMessage) {
        fputs(STDERR, $versionMessage);
        exit(1);
    });
// ...
bash
$ php ping.php --version
version 1.0.0
bash
   $ php examples/chain/customVersionHelp.php --show-help; echo $?
or $ php examples/chain/customVersionHelp.php -s; echo $?
Usage:
        php examples/chain/customVersionHelp.php --example-flag=int [FLAG]... (EXAMPLE-ARG)

FLAG:
        --example-flag=int
                example flag declaration

ARG:
        EXAMPLE-ARG
                example arg declaration

1 # return exit 1 status code
bash
   $ php examples/chain/customVersionHelp.php --ver; echo $?
or $ php examples/chain/customVersionHelp.php -V; echo $?
app version: 1.0
1 # return exit 1 status code