PHP code example of tomnomnom / phargs

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

    

tomnomnom / phargs example snippets



tory = new \Phargs\Factory();
$screen = $factory->screen();

$screen->outln("Hello, World!");


// ./Examples/Flags.php

// Bootstrap Phargs
ory
$factory = new \Phargs\Factory();

// Get an argument processor
$args = $factory->args();

// Expect the -h flag to be an argument
$args->expectFlag('-h');

if ($args->flagIsSet('-h')){
  echo "Help flag is set\n";
} else {
  echo "Help flag is not set";
}


// ./Examples/FlagAliases.php

rgs = $factory->args();

$args->expectFlag('-h');

// Alias the -h flag to --help so either can be used
$args->addFlagAlias('-h', '--help');

// You could check for --help instead of -h here and it would still work
if ($args->flagIsSet('-h')){
  echo "Help flag is set\n";
} else {
  echo "Help flag is not set\n";
}


// ./Examples/CompoundFlags.php

s = $factory->args();

$args->expectFlag('-H');
$args->expectFlag('-n');
$args->expectFlag('-r');

if ($args->flagIsSet('-H')){
  echo "-H flag is set\n";
}
if ($args->flagIsSet('-n')){
  echo "-n flag is set\n";
}
if ($args->flagIsSet('-r')){
  echo "-r flag is set\n";
}


// ./Examples/Params.php

// Bootstrap Phargs
ry
$factory = new \Phargs\Factory();

// Get an argument processor
$args = $factory->args();

// Expect the --count param
$args->expectParam('--count');

if ($args->paramIsSet('--count')){
  echo "--count param is set\n";
  echo "--count value is: ";
  echo $args->getParamValue('--count').PHP_EOL;
} else {
  echo "--count param is not set\n";
}


// ./Examples/ParamAliases.php

gs = $factory->args();

// Expect the --count param
$args->expectParam('--count');

// Alias --count to -c so that either can be used
$args->addParamAlias('--count', '-c');

if ($args->paramIsSet('--count')){
  echo "--count param is set\n";
  echo "--count value is: ";
  echo $args->getParamValue('-c').PHP_EOL;
} else {
  echo "--count param is not set\n";
}


// ./Examples/RequiredParams.php

include __DIR__.'/../Phargs/Init.php';
$factory = new \Phargs\Factory();
$args = $factory->args();

// Require some params
$args->cho "Not all arg 


// ./Examples/ResidualArgs.php

gs = $factory->args();

// We're expecting some arguments
$args->expectParam('--count');
$args->expectFlag('-h');

// Arguments we're not expecting are considered 'residual'
echo "Residual arg #0: ".$args->getResidualArg(0).PHP_EOL;
echo "All residual args: ".implode(', ', $args->getResidualArgs()).PHP_EOL;
echo "First two residual args: ".implode(', ', $args->getResidualArgs(0, 2)).PHP_EOL;


// ./Examples/ScreenBasic.php

/ Get a screen interface
$screen = $factory->screen();

$screen->out("Hello, ");
$screen->outln("World!");

$screen->err("Error ");
$screen->errln("message");

$screen->printf("When in %s".PHP_EOL, "Rome");

$testVar = array(1, 2, 3);
$screen->varExport($testVar);

$screen->log('A log message');


// ./Examples/ScreenColors.php

 Get a screen interface
$screen = $factory->screen();

$screen->outln("Red", 'red');
$screen->outln("Red with a white background", "red", "white");
$screen->outln("Red with a white background, underlined", "red", "white", "underline");


// ./Examples/PrompterBasic.php

een = $factory->screen();

// Get a prompter
$prompter = $factory->prompter();

// Prompt for some input
$name = $prompter->prompt('What is your name? ');

// Do something with the response
$screen->outln("Hello, {$name}!");


// ./Examples/PrompterRequired.php

include __DIR__.'/../Phargs/Init.php';
$factory = new \Phargs\Factory();
$screen = $factory->screen();

// Get a prompter
$prompter = $factory->prompter();

// Prompt for some 


// ./Examples/Table.php

();
$screen = $factory->screen();

// Get a table formatter
$table = $factory->table();

$table->setFields(array('id', 'name'));
$table->addRows(array(
  array(1, 'Tom'),
  array(2, 'Dick'),
  array(3, 'Harry'),
));

$screen->out($table);


// ./Examples/Tsv.php

ry();
$screen = $factory->screen();

// Get a TSV formatter
$table = $factory->tsv();

$table->setFields(array('id', 'name'));
$table->addRows(array(
  array(1, 'Tom'),
  array(2, 'Dick'),
  array(3, 'Harry'),
));

$screen->out($table);