PHP Classes

File: examples/hasdefaultvalue.php

Recommend this page to a friend!
  Classes of Johnny Mast   Redbox PHP CLI Arguments   examples/hasdefaultvalue.php   Download  
File: examples/hasdefaultvalue.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Redbox PHP CLI Arguments
Extract command line parameter values
Author: By
Last change: Made the tests compatible with phpcs.
Date: 2 years ago
Size: 1,281 bytes
 

Contents

Class file image Download
<?php
/**
 * Hasdefaultvalue.php
 *
 * Run this script like
 *
 * $ php ./hasdefaultvalue.php
 * OR
 * $ php ./hasdefaultvalue.php --targetpath=/var
 *
 * PHP version 7.3 and up.
 *
 * @category Core
 * @package Redbox_Cli
 * @author Johnny Mast <mastjohnny@gmail.com>
 * @license https://opensource.org/licenses/MIT MIT
 * @link https://github.com/johnnymast/redbox-cli
 * @since 1.0
 */

require 'autoload.php';

use
Redbox\Cli\Cli as CLI;

try {
   
$cli = new CLI;

   
/**
     * Setup the rules of engagement
     */
   
$cli->arguments->add(
        [
           
'targetpath' => [
               
'prefix' => 't',
               
'longPrefix' => 'targetpath',
               
'description' => 'Path',
               
'defaultValue' => '/var/log',
            ]
        ]
    );

   
/**
     * We need to tell the parser to start.
     */
   
$cli->arguments->parse();

   
/**
     * If we don't get an exception of us missing things we can handle stuff.
     */
   
echo "You entered path: " . $cli->arguments->get('targetpath') . "\n";
    echo
"Is this the default value?: " . ($cli->arguments->hasDefaultValue('targetpath') ? 'Yes' : 'No') . "\n";

} catch (
Exception $e) {
   
/**
     * Print how to use the script
     */
   
$cli->arguments->usage();
}