All Projects → amercier → php-cli-helpers

amercier / php-cli-helpers

Licence: ISC license
Utility classes to write PHP command-line scripts

Programming Languages

PHP
23972 projects - #3 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to php-cli-helpers

ngx-file-helpers
Angular File Helpers
Stars: ✭ 34 (+30.77%)
Mutual labels:  helpers
android-helpers
Android helpers collection
Stars: ✭ 20 (-23.08%)
Mutual labels:  helpers
leak
Show info about package releases on PyPI.
Stars: ✭ 15 (-42.31%)
Mutual labels:  helpers
react-semantic-render
Semantic helper components for rendering content with React.
Stars: ✭ 13 (-50%)
Mutual labels:  helpers
support
Collection of agnostic PHP Functions and helpers with zero dependencies to use as foundation in packages and other project
Stars: ✭ 54 (+107.69%)
Mutual labels:  helpers
php-lodash
php-lodash is a PHP utility library, similar to Underscore/Lodash.
Stars: ✭ 35 (+34.62%)
Mutual labels:  helpers
dpytools
Collection of easy to use, beginner friendly but powerful, orthogonal tools to speed up discord bots development (discord.py)
Stars: ✭ 23 (-11.54%)
Mutual labels:  helpers
figma-plugin-helpers
A collection of useful helper functions to import to your Figma plugin project
Stars: ✭ 161 (+519.23%)
Mutual labels:  helpers
Milvasoft
Helper structures for .Net 6 projects.
Stars: ✭ 24 (-7.69%)
Mutual labels:  helpers
support
Support Classes
Stars: ✭ 56 (+115.38%)
Mutual labels:  helpers
laravel-helper-functions
Laravel-specific and pure PHP Helper Functions.
Stars: ✭ 106 (+307.69%)
Mutual labels:  helpers
UnityCommon
A collection of common frameworks and tools for Unity-based projects
Stars: ✭ 61 (+134.62%)
Mutual labels:  helpers
redux-fp
Functional programming helpers for Redux.
Stars: ✭ 28 (+7.69%)
Mutual labels:  helpers
php-helpers
An extensive set of PHP helper functions and classes.
Stars: ✭ 27 (+3.85%)
Mutual labels:  helpers
utils
🛠 A collection of light-weight methods and helpers for defensive programming
Stars: ✭ 15 (-42.31%)
Mutual labels:  helpers
MADE.NET
MADE.NET is a home to all of those bits of code that you know you'll reuse in another project. Making app development easier with .NET.
Stars: ✭ 75 (+188.46%)
Mutual labels:  helpers
three-stdlib
📚 Stand-alone library of threejs examples designed to run without transpilation in node & browser
Stars: ✭ 380 (+1361.54%)
Mutual labels:  helpers
dotfiles
©️ home! sweet home
Stars: ✭ 19 (-26.92%)
Mutual labels:  helpers
iterative
Functions for working with iterators in JavaScript and TypeScript
Stars: ✭ 16 (-38.46%)
Mutual labels:  helpers
ideas
Идеи по улучшению языка C++ для обсуждения
Stars: ✭ 65 (+150%)
Mutual labels:  helpers

php-cli-helpers

Utility classes to write PHP command-line scripts

Build Status Code Climate Test Coverage Dependency Status

Latest Stable Version PHP version License

Installation

php-cli-helpers is available through Composer.

  "require": {
    "amercier/cli-helpers": "1.*"
  }
php composer.phar install

If you are not familiar with Composer, please read the full installation intructions.

Usage

\Cli\Helpers\Parameter

Utility class to handle command-line parameters.

$options = \Cli\Helpers\Parameter::getFromCommandLine(array(
    'host'     => new Parameter('h', 'host'    , '127.0.0.1'),
    'username' => new Parameter('u', 'username', Parameter::VALUE_REQUIRED),
    'password' => new Parameter('p', 'password', Parameter::VALUE_REQUIRED),
    'verbose'  => new Parameter('v', 'verbose' , Parameter::VALUE_NO_VALUE),
));

$options['host'];     // given -h/--host, or 127.0.0.1 otherwise
$options['username']; // given -u/--username
$options['password']; // given -p/--password
$options['verbose'];  // true if -v/--verbose is given, false otherwise

See API Documentation for Parameter

\Cli\Helpers\Script and \Cli\Helpers\DocumentedScript

Utility class to write scripts as objects.

#!/usr/bin/env php
<?php
require_once dirname(__FILE__) . '/path/to/vendor/autoload.php';

use Cli\Helpers\DocumentedScript;
use Cli\Helpers\Parameter;

$script = new DocumentedScript();
$script
    ->setName('test-documented-script.php')
    ->setVersion('1.0')
    ->setDescription('Test script for Cli\Helpers\DocumentedScript')
    ->setCopyright('Copyright (c) Alexandre Mercier 2014')
    ->addParameter(new Parameter('H', 'host'    , '127.0.0.1')              , 'Host.')
    ->addParameter(new Parameter('u', 'username', Parameter::VALUE_REQUIRED), 'User name.')
    ->addParameter(new Parameter('p', 'password', Parameter::VALUE_REQUIRED), 'Password.')
    ->addParameter(new Parameter('v', 'verbose' , Parameter::VALUE_NO_VALUE), 'Enable verbosity.')
    ->setProgram(function ($options, $arguments) {
        var_dump($arguments);
        var_dump($options);
    })
    ->start();

While Script doesn't have any pre-configured switch, DocumentedScript has --h, --help and -V, --version. This provides an automatic handling of this two switches.

Version example:

test-documented-script.php -V

test-documented-script.php v1.0
Copyright (c) 2014 Alexandre Mercier

Help example:

test-documented-script.php -h

Usage: test-documented-script.php -p PASSWORD -u USERNAME [OPTIONS]

Test script for Cli\Helpers\DocumentedScript

  -H, --host     HOST        Host (defaults to '127.0.0.1').
  -u, --username USERNAME    User name.
  -p, --password PASSWORD    Password.
  -v, --verbose              Enable verbosity.
  -h, --help                 Display this help and exit.
  -V, --version              Output version information and exit.

test-documented-script.php v1.0
Copyright (c) 2014 Alexandre Mercier

\Cli\Helpers\Job

Utility class to run a job and catch exceptions.

On successful jobs:

\Cli\Helpers\Job::run('Doing awesome stuff', function() {
    ... // awesome stuff
});
Doing awesome stuff... OK

On unsuccessful jobs:

\Cli\Helpers\Job::run('Fighting Chuck Norris', function() {
    ... // throws a RoundHouseKickException('You've received a round-house kick', 'face')
});
Fighting Chuck Norris... NOK - You've received a round-house kick in the face

You can also add parameters to the function:

\Cli\Helpers\Job::run(
    'Doing awesome stuff',
    function($a, $b) {
        $a; // => 1337;
        $b; // => 'good luck, im behind 7 firewalls';
    },
    array(1337, 'im behind 7 firewalls')
});

See API Documentation for Job

\Cli\Helpers\IO

Utility class to handle standard input/output.

IO::form

Usage

\Cli\Helpers\IO::form('an apple', array(
    'Golden Delicious',
    'Granny Smith',
    'Pink Lady',
    'Royal Gala',
));

will display:

1. Golden Delicious
2. Granny Smith
3. Pink Lady
4. Royal Gala

Choose an apple: |

Then, user is asked to make a choice between 1 and 3 on standard input.

IO::strPadAll

echo IO::strPadAll(
    array( // items
        array('#', 'EN', 'FR', 'ES'),
        '',
        array('1', 'One', 'Un', 'Uno'),
        array('2', 'Two', 'Deux', 'Dos'),
        array('3', 'Three', 'Trois', 'Tres'),
        array('4', 'Four', 'Quatre', 'Cuatro'),
        array('5', 'Five', 'Cinq', 'Cinco'),
    ),
    array( // alignment
        2 => STR_PAD_LEFT,
        3 => STR_PAD_RIGHT,
    ),
    "\n", // line separator
    '   ' // field separator
));

will display:

#   EN          FR   ES

1   One         Un   Uno
2   Two       Deux   Dos
3   Three    Trois   Tres
4   Four    Quatre   Cuatro
5   Five      Cinq   Cinco

See API Documentation for IO

Contributing

Contributions (issues , pull requests ) are more than welcome! Feel free to clone, fork, modify, extend, etc, as long as you respect the license terms.

See contributing intructions for details.

Licensing

This project is released under ISC License license. If this license does not fit your requirement for whatever reason, but you would be interested in using the work (as defined below) under another license, please contact authors.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].