PHP code example of jacekciach / commandline

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

    

jacekciach / commandline example snippets



CommandLine\CommandLine;

$cmd = new CommandLine();
var_export(array(
  "binary"  => $cmd->binary(),  // returns PHP_BINARY
  "script"  => $cmd->script(),  // returns the name of the script
  "options" => $cmd->options(), // returns all "options" passed from command line
  "params"  => $cmd->params(),  // returns all "params" passed from command line
));
echo PHP_EOL;


CommandLine\CommandLine;

$cmd = new CommandLine();
echo 'start = ' . var_export($cmd->option('start'), true) . PHP_EOL;
echo 'msg   = ' . var_export($cmd->option('msg'), true) . PHP_EOL;
echo 'stop  = ' . var_export($cmd->option('stop'), true) . PHP_EOL; // reading a not existing options will return NULL 
echo PHP_EOL;
echo 'param(0) = ' . var_export($cmd->param(0), true) . PHP_EOL;
echo 'param(1) = ' . var_export($cmd->param(1), true) . PHP_EOL;
echo 'param(2) = ' . var_export($cmd->param(2), true) . PHP_EOL; // reading a not existing param will return NULL


$ php example1.php --start --msg="Hello, World!" file1.txt --book

array (
  'binary' => '/usr/bin/php7.0',
  'script' => 'example1.php',
  'options' => 
  array (
    'start' => true,
    'msg' => 'Hello, World!',
  ),
  'params' => 
  array (
    0 => 'file1.txt',
    1 => '--book'
  ),
)

$ php example2.php --start --msg="Hellow World!" "Test Application" user