{"id":18340797,"url":"https://github.com/amercier/php-cli-helpers","last_synced_at":"2025-04-06T06:31:28.458Z","repository":{"id":9602975,"uuid":"11524973","full_name":"amercier/php-cli-helpers","owner":"amercier","description":"Utility classes to write PHP command-line scripts","archived":false,"fork":false,"pushed_at":"2023-05-18T07:30:42.000Z","size":120,"stargazers_count":25,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-21T18:37:53.532Z","etag":null,"topics":["cli","command-line","helpers","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amercier.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"docs/contributing.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-07-19T09:51:30.000Z","updated_at":"2023-12-22T03:30:09.000Z","dependencies_parsed_at":"2024-06-19T16:05:01.429Z","dependency_job_id":"fda51b76-a57d-43b7-aede-381aad76a959","html_url":"https://github.com/amercier/php-cli-helpers","commit_stats":{"total_commits":157,"total_committers":4,"mean_commits":39.25,"dds":"0.21019108280254772","last_synced_commit":"5767dd441960c0ed89b64ba72fb312bf8e217739"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amercier%2Fphp-cli-helpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amercier%2Fphp-cli-helpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amercier%2Fphp-cli-helpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amercier%2Fphp-cli-helpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amercier","download_url":"https://codeload.github.com/amercier/php-cli-helpers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445647,"owners_count":20939951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","command-line","helpers","php"],"created_at":"2024-11-05T20:24:16.447Z","updated_at":"2025-04-06T06:31:28.202Z","avatar_url":"https://github.com/amercier.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"php-cli-helpers\n===============\n\nUtility classes to write PHP command-line scripts\n\n[![Build Status](https://img.shields.io/travis/amercier/php-cli-helpers/master.svg?style=flat)](https://travis-ci.org/amercier/php-cli-helpers)\n[![Code Climate](https://img.shields.io/codeclimate/github/amercier/php-cli-helpers.svg?style=flat)](https://codeclimate.com/github/amercier/php-cli-helpers)\n[![Test Coverage](https://img.shields.io/codeclimate/coverage/github/amercier/php-cli-helpers.svg?style=flat)](https://codeclimate.com/github/amercier/php-cli-helpers)\n[![Dependency Status](https://img.shields.io/gemnasium/amercier/php-cli-helpers.svg?style=flat)](https://gemnasium.com/amercier/php-cli-helpers)\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/amercier/cli-helpers.svg)](https://packagist.org/packages/amercier/cli-helpers)\n[![PHP version](https://img.shields.io/packagist/php-v/amercier/cli-helpers.svg)](https://packagist.org/packages/amercier/cli-helpers)\n[![License](https://img.shields.io/packagist/l/amercier/cli-helpers.svg)](https://packagist.org/packages/amercier/cli-helpers)\n\n\nInstallation\n------------\n\n_php-cli-helpers_ is available through [Composer](http://getcomposer.org/).\n```json\n  \"require\": {\n    \"amercier/cli-helpers\": \"1.*\"\n  }\n```\n```bash\nphp composer.phar install\n```\n\nIf you are not familiar with _Composer_, please read the\n[full installation intructions](docs/install.md).\n\n\nUsage\n-----\n\n\n### \\Cli\\Helpers\\Parameter\n\nUtility class to handle command-line parameters.\n\n```php\n$options = \\Cli\\Helpers\\Parameter::getFromCommandLine(array(\n    'host'     =\u003e new Parameter('h', 'host'    , '127.0.0.1'),\n    'username' =\u003e new Parameter('u', 'username', Parameter::VALUE_REQUIRED),\n    'password' =\u003e new Parameter('p', 'password', Parameter::VALUE_REQUIRED),\n    'verbose'  =\u003e new Parameter('v', 'verbose' , Parameter::VALUE_NO_VALUE),\n));\n\n$options['host'];     // given -h/--host, or 127.0.0.1 otherwise\n$options['username']; // given -u/--username\n$options['password']; // given -p/--password\n$options['verbose'];  // true if -v/--verbose is given, false otherwise\n```\n\nSee [API Documentation for Parameter](docs/api-parameter.md)\n\n\n### \\Cli\\Helpers\\Script and \\Cli\\Helpers\\DocumentedScript\n\nUtility class to write scripts as objects.\n\n```php\n#!/usr/bin/env php\n\u003c?php\nrequire_once dirname(__FILE__) . '/path/to/vendor/autoload.php';\n\nuse Cli\\Helpers\\DocumentedScript;\nuse Cli\\Helpers\\Parameter;\n\n$script = new DocumentedScript();\n$script\n    -\u003esetName('test-documented-script.php')\n    -\u003esetVersion('1.0')\n    -\u003esetDescription('Test script for Cli\\Helpers\\DocumentedScript')\n    -\u003esetCopyright('Copyright (c) Alexandre Mercier 2014')\n    -\u003eaddParameter(new Parameter('H', 'host'    , '127.0.0.1')              , 'Host.')\n    -\u003eaddParameter(new Parameter('u', 'username', Parameter::VALUE_REQUIRED), 'User name.')\n    -\u003eaddParameter(new Parameter('p', 'password', Parameter::VALUE_REQUIRED), 'Password.')\n    -\u003eaddParameter(new Parameter('v', 'verbose' , Parameter::VALUE_NO_VALUE), 'Enable verbosity.')\n    -\u003esetProgram(function ($options, $arguments) {\n        var_dump($arguments);\n        var_dump($options);\n    })\n    -\u003estart();\n```\n\nWhile `Script` doesn't have any pre-configured switch, `DocumentedScript` has\n`--h, --help` and `-V, --version`. This provides an automatic handling of this\ntwo switches.\n\nVersion example:\n\n`test-documented-script.php -V`\n```\ntest-documented-script.php v1.0\nCopyright (c) 2014 Alexandre Mercier\n```\n\nHelp example:\n\n`test-documented-script.php -h`\n```\nUsage: test-documented-script.php -p PASSWORD -u USERNAME [OPTIONS]\n\nTest script for Cli\\Helpers\\DocumentedScript\n\n  -H, --host     HOST        Host (defaults to '127.0.0.1').\n  -u, --username USERNAME    User name.\n  -p, --password PASSWORD    Password.\n  -v, --verbose              Enable verbosity.\n  -h, --help                 Display this help and exit.\n  -V, --version              Output version information and exit.\n\ntest-documented-script.php v1.0\nCopyright (c) 2014 Alexandre Mercier\n```\n\n\n### \\Cli\\Helpers\\Job\n\nUtility class to run a job and catch exceptions.\n\nOn successful jobs:\n```php\n\\Cli\\Helpers\\Job::run('Doing awesome stuff', function() {\n    ... // awesome stuff\n});\n```\n```\nDoing awesome stuff... OK\n```\n\nOn unsuccessful jobs:\n```php\n\\Cli\\Helpers\\Job::run('Fighting Chuck Norris', function() {\n    ... // throws a RoundHouseKickException('You've received a round-house kick', 'face')\n});\n```\n```\nFighting Chuck Norris... NOK - You've received a round-house kick in the face\n```\n\nYou can also add parameters to the function:\n\n```php\n\\Cli\\Helpers\\Job::run(\n    'Doing awesome stuff',\n    function($a, $b) {\n        $a; // =\u003e 1337;\n        $b; // =\u003e 'good luck, im behind 7 firewalls';\n    },\n    array(1337, 'im behind 7 firewalls')\n});\n```\n\nSee [API Documentation for Job](docs/api-job.md)\n\n\n###  \\Cli\\Helpers\\IO\n\nUtility class to handle standard input/output.\n\n#### IO::form\n\nUsage\n-----\n\n```php\n\\Cli\\Helpers\\IO::form('an apple', array(\n    'Golden Delicious',\n    'Granny Smith',\n    'Pink Lady',\n    'Royal Gala',\n));\n```\nwill display:\n```\n1. Golden Delicious\n2. Granny Smith\n3. Pink Lady\n4. Royal Gala\n\nChoose an apple: |\n```\nThen, user is asked to make a choice between 1 and 3 on standard input.\n\n#### IO::strPadAll\n\n```php\necho IO::strPadAll(\n    array( // items\n        array('#', 'EN', 'FR', 'ES'),\n        '',\n        array('1', 'One', 'Un', 'Uno'),\n        array('2', 'Two', 'Deux', 'Dos'),\n        array('3', 'Three', 'Trois', 'Tres'),\n        array('4', 'Four', 'Quatre', 'Cuatro'),\n        array('5', 'Five', 'Cinq', 'Cinco'),\n    ),\n    array( // alignment\n        2 =\u003e STR_PAD_LEFT,\n        3 =\u003e STR_PAD_RIGHT,\n    ),\n    \"\\n\", // line separator\n    '   ' // field separator\n));\n```\nwill display:\n```\n#   EN          FR   ES\n\n1   One         Un   Uno\n2   Two       Deux   Dos\n3   Three    Trois   Tres\n4   Four    Quatre   Cuatro\n5   Five      Cinq   Cinco\n```\n\nSee [API Documentation for IO](docs/api-io.md)\n\n\n\nContributing\n------------\n\nContributions (issues ♥, pull requests ♥♥♥) are more than welcome! Feel free to\nclone, fork, modify, extend, etc, as long as you respect the\n[license terms](LICENSE.md).\n\nSee [contributing intructions](docs/contributing.md) for details.\n\n\nLicensing\n---------\n\nThis project is released under [ISC License](LICENSE.d) license. If this license\ndoes not fit your requirement for whatever reason, but you would be interested\nin using the work (as defined below) under another license, please contact\n[authors](docs/authors.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famercier%2Fphp-cli-helpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famercier%2Fphp-cli-helpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famercier%2Fphp-cli-helpers/lists"}