2017 © Pedro Peláez
 

library console

A set of console related components without the overhead of the illuminate framework

image

leaveyou/console

A set of console related components without the overhead of the illuminate framework

  • Saturday, June 3, 2017
  • by Leaveyou
  • Repository
  • 3 Watchers
  • 1 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Console

Travis, (*1)

A set of console components for parsing command line arguments, (*2)

Features:

  • complex arguments recognition:
Parameters without value         --verbose -v -vv -vvv
Optional parameters with value   --optional=1970 -o=1970 -o1970 -o"1970"
Mandatory parameters with value  --required=1970 -r=1970 -r1970 -r"1970" --required 1970 -r 1970 -r "1970"
  • Automatically generated help pages
$ php test.php --help
Asked for help page...
  -o,  --optional    Some example of an optional parameter
  -v,  --verbose     Whether verbose
       --account     This parameter doesn't have a description
       --mandatory   Mandatory parameter description. This should have one and only one value.
       --help        Shows this help message
  • Dependency injection / container-ready
  • Improves upon native php function getopt() shortcomings:
    • Treats --version and -v as the same parameter if defined as such.
    • Mandatory parameters are really MANDATORY
    • Optional parameters cannot have empty value
    • Allows to specify in advance if a parameter is a single string or a list of strings
    • Default values for optional parameters
    • @SOON: Support for trailing arbitrary data: php x.php -abc [TRAILING] [DATA] in PHP 5

Usage

<?php

/** class importing */
use Leaveyou\Console\Input;
use Leaveyou\Console\Parameter;
use Leaveyou\Console\ParameterSet;
use Leaveyou\Console\Tools\CommandLineParser;
use Leaveyou\Console\Tools\Help;
use Leaveyou\Console\Tools\ResultNormalizer;

/** composer autoloader */
require_once "../vendor/autoload.php";

$normalizer = new ResultNormalizer();
$cmdParser = new CommandLineParser($normalizer);
$helpGenerator = new Help();
$parameterSet = new ParameterSet();
$consoleInput = new Input($parameterSet, $cmdParser, $helpGenerator);

/** Define parameters */
$simpleParameter = new Parameter("simple");

$complexParameter = Parameter::create("complex")
    ->setType(Parameter::TYPE_MANDATORY)
    ->setDescription("Some example of a complex parameter")
    ->setShortName('c')
    ->setValueType(Parameter::VALUE_STRING);

/** Inject parameters */
$consoleInput->addParameter($simpleParameter);
$consoleInput->addParameter($complexParameter);

/** Read parameters */
$simple = $consoleInput->getValue('simple');
$complex = $consoleInput->getValue('complex');

Then just run php test.php --simple="sure thing" --complex "yes" --complex "no", (*3)

Oups..., (*4)

The "--complex" parameter can only be used once since the expected value must be a string.
  -c,  --complex     Some example of a complex parameter
       --simple      This parameter doesn't have a description
       --help        Shows this help message

To do

  • [x] Make a list
  • [ ] Unit tests
  • [ ] Replace native getopt() call to custom implementation
  • [ ] Make repository more S.O.L.I.D.
  • [ ] Better help page generation. Current one is crap.
  • [ ] Find more things for this list
  • [ ] Generate command synopsis and show optional parameters and usage

The Versions

03/06 2017

dev-master

9999999-dev

A set of console related components without the overhead of the illuminate framework

  Sources   Download

MIT

The Development Requires

by Liviu Gelea

04/04 2017

1.0.0

1.0.0.0

A set of console related components without the overhead of the illuminate framework

  Sources   Download

MIT

by Liviu Gelea

03/04 2017

1.0.0-alpha.3

1.0.0.0-alpha3

A set of console related components without the overhead of the illuminate framework

  Sources   Download

MIT

by Liviu Gelea

01/04 2017

1.0.0-alpha.2

1.0.0.0-alpha2

A set of console related components without the overhead of the illuminate framework

  Sources   Download

MIT

by Liviu Gelea

01/04 2017

1.0.0-alpha.1

1.0.0.0-alpha1

A set of console related components without the overhead of the illuminate framework

  Sources   Download

MIT

by Liviu Gelea