{"id":13532689,"url":"https://github.com/c9s/CLIFramework","last_synced_at":"2025-04-01T21:30:46.820Z","repository":{"id":408835,"uuid":"2962174","full_name":"c9s/CLIFramework","owner":"c9s","description":"A powerful command line application framework for PHP. It's an extensible, flexible component, You can build your command-based application in seconds!","archived":false,"fork":false,"pushed_at":"2023-04-20T14:05:15.000Z","size":1004,"stargazers_count":442,"open_issues_count":41,"forks_count":51,"subscribers_count":29,"default_branch":"master","last_synced_at":"2025-03-25T01:38:51.516Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/c9s.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2011-12-12T05:38:25.000Z","updated_at":"2025-03-24T19:35:16.000Z","dependencies_parsed_at":"2023-07-06T16:16:10.790Z","dependency_job_id":null,"html_url":"https://github.com/c9s/CLIFramework","commit_stats":{"total_commits":973,"total_committers":26,"mean_commits":37.42307692307692,"dds":0.355601233299075,"last_synced_commit":"b501234bb4f5f7fe95a3bd36691df856904abeee"},"previous_names":[],"tags_count":58,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c9s%2FCLIFramework","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c9s%2FCLIFramework/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c9s%2FCLIFramework/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c9s%2FCLIFramework/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/c9s","download_url":"https://codeload.github.com/c9s/CLIFramework/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246447042,"owners_count":20778907,"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":[],"created_at":"2024-08-01T07:01:12.922Z","updated_at":"2025-04-01T21:30:46.790Z","avatar_url":"https://github.com/c9s.png","language":"PHP","funding_links":[],"categories":["PHP","目录","Table of Contents","类库","命令行 Command Line","[PHP](https://www.php.net/)"],"sub_categories":["命令行 Command Line","Command Line","CLI","Library","Useful awesome list for Go cli"],"readme":"CLIFramework\n============\n\n[![Coverage Status](https://img.shields.io/coveralls/c9s/CLIFramework.svg)](https://coveralls.io/r/c9s/CLIFramework) \n[![Latest Stable Version](https://poser.pugx.org/corneltek/cliframework/v/stable.svg)](https://packagist.org/packages/corneltek/cliframework) \n[![Latest Unstable Version](https://poser.pugx.org/corneltek/cliframework/v/unstable.svg)](https://packagist.org/packages/corneltek/cliframework) \n[![Total Downloads](https://poser.pugx.org/corneltek/cliframework/downloads.svg)](https://packagist.org/packages/corneltek/cliframework) \n[![Monthly Downloads](https://poser.pugx.org/corneltek/cliframework/d/monthly)](https://packagist.org/packages/corneltek/cliframework)\n[![License](https://poser.pugx.org/corneltek/cliframework/license.svg)](https://packagist.org/packages/corneltek/cliframework)\n\n\nCLIFramework is a command-line application framework, for building flexiable, simple command-line applications.\n\nCommands and Subcommands can be registered from outside of an application or your plugins.\n\nDefining a new command is pretty simple, all you need to is declare a class which is inherited from `CLIFramework\\Command` class.\n\nFeatures\n--------------------\n\n- Intuitive command class and option spec\n\n- command options are supported, powered by GetOptionKit. including long option, short option, required|optional|default value.\n\n- Hierarchical commands.\n\n- Automatic help page generation.\n\n- Automatic zsh completion generator.\n\n- Automatic bash completion generator.\n\n- Friendly message when command arguments are not enough.\n\n- Testable, CLIFramework provides PHPUnit test case for testing the commands in PHP.\n\n- Argument validation, suggestion, \n\n- Command Groups\n\n- HHVM compatible\n\n\n\n\nSynopsis\n--------------------\n\n```php\nclass CommitCommand extends CLIFramework\\Command {\n\n    public function brief() { return 'brief of bar'; }\n\n    public function options($opts) {\n        $opts-\u003eadd('C|reuse-message:','Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.')\n            -\u003eisa('string')\n            -\u003evalueName('commit hash')\n            // -\u003evalidValues([ 'static-50768ab', 'static-c2efdc2', 'static-ed5ba6a', 'static-cf0b1eb'])\n            -\u003evalidValues(function() {\n                $output = array();\n                exec(\"git rev-list --abbrev-commit HEAD -n 20\", $output);\n                return $output;\n            })\n            ;\n\n        // Runtime completion by setting up a closure for completion\n        $opts-\u003eadd('c|reedit-message:','like -C, but with -c the editor is invoked, so that the user can further edit the commit message.')\n            -\u003eisa('string')\n            -\u003evalueName('commit hash')\n            -\u003evalidValues(function() {\n                // exec(\"git log -n 10 --pretty=format:%H:%s\", $output);\n                exec(\"git log -n 10 --pretty=format:%H:%s\", $output);\n                return array_map(function($line) {\n                    list($key,$val) = explode(':',$line);\n                    $val = preg_replace('/\\W/',' ', $val);\n                    return array($key, $val);\n                }, $output);\n            })\n            ;\n\n        $opts-\u003eadd('author:', 'Override the commit author. Specify an explicit author using the standard A U Thor \u003cauthor@example.com\u003e format.')\n            -\u003esuggestions(array( 'c9s', 'foo' , 'bar' ))\n            -\u003evalueName('author name')\n            ;\n\n        $opts-\u003eadd('output:', 'Output file')\n            -\u003eisa('file')\n            ;\n    }\n\n    public function arguments($args) {\n        $args-\u003eadd('user')\n            -\u003evalidValues(['c9s','bar','foo']);\n\n        // Static completion result\n        $args-\u003eadd('repo')\n            -\u003evalidValues(['CLIFramework','GetOptionKit']);\n\n        // Add an argument info expecting multiple *.php files\n        $args-\u003eadd('file')\n            -\u003eisa('file')\n            -\u003eglob('*.php')\n            -\u003emultiple()\n            ;\n    }\n\n    public function init() {\n\n        $this-\u003ecommand('foo'); // register App\\Command\\FooCommand automatically\n\n        $this-\u003ecommand('bar', 'WhatEver\\MyCommand\\BarCommand');\n\n        $this-\u003ecommandGroup('General Commands', ['foo', 'bar']);\n\n        $this-\u003ecommandGroup('Database Commands', ['create-db', 'drop-db']);\n\n        $this-\u003ecommandGroup('More Commands', [\n            'foo' =\u003e 'WhatEver\\MyCommand\\FooCommand',\n            'bar' =\u003e 'WhatEver\\MyCommand\\BarCommand'\n        ]);\n    }\n\n    public function execute($user,$repo) {\n        $this-\u003elogger-\u003enotice('executing bar command.');\n        $this-\u003elogger-\u003einfo('info message');\n        $this-\u003elogger-\u003edebug('info message');\n        $this-\u003elogger-\u003ewrite('just write');\n        $this-\u003elogger-\u003ewriteln('just drop a line');\n        $this-\u003elogger-\u003enewline();\n\n        return \"Return result as an API\"; // This can be integrated in your web application\n    }\n}\n```\n\n\n### Automatic Zsh Completion Generator\n\n![Imgur](http://imgur.com/sU3mrDe.gif)\n\n#### Zsh Completion With Lazy Completion Values:\n\n![Imgur](http://i.imgur.com/ItYGDIu.gif)\n\n#### Bash Completion\n\n![Imgur](http://i.imgur.com/sF5UPX5.gif)\n\n\nDocumentation\n-------------\n\nSee documentation on our wiki \u003chttps://github.com/c9s/CLIFramework/wiki\u003e\n\n\nCommand Forms\n---------------------\n\nCLIFramework supports many command-line forms, for example:\n\n    $ app [app-opts] [subcommand1] [subcommand1-opts] [subcommand2] [subcommand2-opts] .... [arguments] \n\nIf the subcommand is not defined, you can still use the simple form:\n\n    $ app [app-opts] [arguments]\n\nFor example,\n\n    $ app db schema --clean dbname\n    $ app gen controller --opt1 --opt2 ControllerName \n\nSubcommand Hierarchy\n------------------------\n\nCommands have methods for stages, like `prepare`, `execute`, `finish`, for a command like below:\n\n    $ app foo_cmd bar_cmd arg1 arg2 arg3\n\nThe call graph is like:\n\n    app-\u003erun\n    - app-\u003eprepare\n      - foo_cmd-\u003eprepare\n        - bar_cmd-\u003eprepare\n        - bar_cmd-\u003eexecute\n        - bar_cmd-\u003efinish\n      - foo_cmd-\u003efinish\n    - app-\u003efinish\n\n\n\nBasic Requirement\n-----------------\n\n* PHP 5.3\n\nInstallation\n------------\n\nFrom composer\n\n```json\n{\n    \"require\": {\n        \"corneltek/cliframework\": \"*\"\n    }\n}\n```\n\n\n\n\nZsh Completion Generator\n----------------------------\n\n```sh\nexample/demo zsh demo \u003e _demo\nsource _demo\n```\n\n```sh\ndemo \u003cTAB\u003e\n```\n\n![Imgur](http://imgur.com/BOZRFJT.png)\n\n![Imgur](http://imgur.com/AXUji1T.png)\n\n![Imgur](http://imgur.com/bg2PPIF.png)\n\n![Imgur](http://imgur.com/DLmzKD4.png)\n\n\nConsole Prompt (Readline)\n-------------------------\n\nsimple prompt:\n\n```php\n$input = $this-\u003eask(\"Your name please\");\n```\n\n    $ php demo.php\n    Your name please: \n\nprompt and except valid values:\n\n```php\n$input = $this-\u003eask(\"Your name please\", array('John', 'Pedro'));\n```\n\n\nVersion Info\n------------\nCLIFrameword has a built-in --version option, to setup the version info, \nyou can simply override a const in your application class to setup version string:\n\n\n```php\nclass ConsoleApp extends CLIFramework\\Application\n{\n    const NAME = 'YourApp';\n    const VERSION = '1.2.1';\n}\n```\n\nThis shows:\n\n    $ yourapp.php --version\n    YourApp - version 1.2.1\n\n\nExample\n-------\nPlease check `example/demo.php`\n\n    $ php example/demo.php\n\n\n\nArgumentEditor\n----------------------\n\n```php\nuse CLIFramework\\ArgumentEditor\\ArgumentEditor;\n\n$editor = new ArgumentEditor(array('./configure','--enable-debug'));\n$editor-\u003eappend('--enable-zip');\n$editor-\u003eappend('--with-sqlite','--with-postgres');\n\necho $editor;\n# ./configure --enable-debug --enable-zip --with-sqlite --with-postgres\n```\n\nMessage style formatter\n--------------------\n\n```php\n$formatter = new CLIFramework\\Formatter;\n$formatter-\u003eformat( 'message' , 'green' );\n```\n\nBuilt-in styles:\n\n    'red'          =\u003e array('fg' =\u003e 'red'),\n    'green'        =\u003e array('fg' =\u003e 'green'),\n    'white'        =\u003e array('fg' =\u003e 'white'),\n    'yellow'       =\u003e array('fg' =\u003e 'yellow'),\n    'strong_red'   =\u003e array('fg' =\u003e 'red', 'bold'  =\u003e 1),\n    'strong_green' =\u003e array('fg' =\u003e 'green','bold' =\u003e 1),\n    'strong_white' =\u003e array('fg' =\u003e 'white','bold' =\u003e 1),\n\n\nBuilding Phar Archive file\n--------------------------\n\n    COMPOSER=tests/fixture/composer.json.phar-test composer install\n    php example/demo archive --working-dir /Users/c9s/work/php/CLIFramework \\\n            --composer tests/fixture/composer.json.phar-test \\\n            app.phar\n\n\nChooser Component\n-----------------\n\n```php\n$chooser = new CLIFramework\\Chooser;\n$value = $chooser-\u003echoose( \"System Options\" , array( \n    'use php-5.4.0' =\u003e '5.4.0',\n    'use php-5.4.1' =\u003e '5.4.1',\n    'use system' =\u003e '5.3.0',\n));\n```\n\n\nDebug Utilities\n-----------------------\n\n### LineIndicator\n\n```php\nuse CLIFramework\\Debug\\LineIndicator;\n$indicator = new LineIndicator;\necho PHP_EOL, $indicator-\u003eindicateFile(__FILE__, __LINE__);\n```\n\n\n### ConsoleDebug class\n\n```php\nuse CLIFramework\\Debug\\ConsoleDebug;\n\nConsoleDebug::dumpRows($pdo-\u003efetchAll());\n\nConsoleDebug::dumpException($e);\n```\n\n\n\nTodos in the next release\n-------------------------\n * [ ] provide a easy way to define chained commands\n * [ ] inheritable options for subcommands.\n * [ ] human readable exception renderer.\n * [ ] interact utilities\n\nHacking\n=======\n\nSetup\n-------\n\n1. Download \u0026 install Onion from https://github.com/phpbrew/Onion\n\n2. Use Onion to bundle the dependencies:\n\n    $ onion bundle\n\n3. Run tests, it should pass.\n\n4. Hack hack hack.\n\n5. Run tests.\n\n6. Send a pull request.\n\n\nHow command class register works\n--------------------------------\n\n- CLIApplication is inherited from CommandBase.\n- Command is also inherited from CommandBase.\n- To register a subcommand, we use the `addCommand` method to register commands or subcommands.\n    - The command class is optional, if command class name is omitted, then the `addCommand` method\n      will try to guess the *real* command class, and try to load the command class.\n\n\n\n\n[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/c9s/cliframework/trend.png)](https://bitdeli.com/free \"Bitdeli Badge\")\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc9s%2FCLIFramework","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fc9s%2FCLIFramework","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc9s%2FCLIFramework/lists"}