{"id":13623642,"url":"https://github.com/c9s/GetOptionKit","last_synced_at":"2025-04-15T15:30:33.400Z","repository":{"id":1932294,"uuid":"2860978","full_name":"c9s/GetOptionKit","owner":"c9s","description":"An object-oriented option parser library for PHP, which supports type constraints, flag, multiple flag, multiple values, required value checking","archived":false,"fork":false,"pushed_at":"2025-01-04T06:49:25.000Z","size":469,"stargazers_count":150,"open_issues_count":7,"forks_count":25,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-09T15:02:41.553Z","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":"mit","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-11-27T13:44:08.000Z","updated_at":"2024-12-18T10:10:55.000Z","dependencies_parsed_at":"2023-07-06T05:54:49.754Z","dependency_job_id":null,"html_url":"https://github.com/c9s/GetOptionKit","commit_stats":{"total_commits":473,"total_committers":18,"mean_commits":26.27777777777778,"dds":0.507399577167019,"last_synced_commit":"aab2728de80175217cab0d7a4d078c3eb907e2c0"},"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c9s%2FGetOptionKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c9s%2FGetOptionKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c9s%2FGetOptionKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c9s%2FGetOptionKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/c9s","download_url":"https://codeload.github.com/c9s/GetOptionKit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248112882,"owners_count":21049744,"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-01T21:01:34.084Z","updated_at":"2025-04-15T15:30:33.364Z","avatar_url":"https://github.com/c9s.png","language":"PHP","funding_links":[],"categories":["目录","Table of Contents","PHP","命令行 Command Line"],"sub_categories":["命令行 Command Line","Command Line"],"readme":"GetOptionKit\n============\n\nCode Quality\n\n[![Build Status](https://travis-ci.org/c9s/GetOptionKit.svg?branch=master)](https://travis-ci.org/c9s/GetOptionKit)\n[![Coverage Status](https://coveralls.io/repos/github/c9s/GetOptionKit/badge.svg?branch=master)](https://coveralls.io/github/c9s/GetOptionKit?branch=master)\n\nVersions \u0026 Stats\n\n[![Latest Stable Version](https://poser.pugx.org/corneltek/getoptionkit/v/stable)](https://packagist.org/packages/corneltek/getoptionkit) \n[![Latest Unstable Version](https://poser.pugx.org/corneltek/getoptionkit/v/unstable)](https://packagist.org/packages/corneltek/getoptionkit) \n[![Total Downloads](https://poser.pugx.org/corneltek/getoptionkit/downloads)](https://packagist.org/packages/corneltek/getoptionkit) \n[![Monthly Downloads](https://poser.pugx.org/corneltek/getoptionkit/d/monthly)](https://packagist.org/packages/corneltek/getoptionkit)\n[![Daily Downloads](https://poser.pugx.org/corneltek/getoptionkit/d/daily)](https://packagist.org/packages/corneltek/getoptionkit)\n[![License](https://poser.pugx.org/corneltek/getoptionkit/license)](https://packagist.org/packages/corneltek/getoptionkit)\n\nA powerful option parser toolkit for PHP, supporting type constraints,\nflag, multiple flag, multiple values and required value checking.\n\nGetOptionKit supports PHP5.3, with fine unit testing with PHPUnit testing\nframework.\n\nGetOptionKit is object-oriented, it's flexible and extendable.\n\nPowering PHPBrew \u003chttps://github.com/phpbrew/phpbrew\u003e, CLIFramework \u003chttps://github.com/c9s/CLIFramework\u003e and AssetKit \u003chttps://github.com/c9s/AssetKit\u003e\n\n\n\n## Features\n\n- Simple format.\n- Type constrant.\n- Multiple value, requried value, optional value checking.\n- Auto-generated help text from defined options.\n- Support app/subcommand option parsing.\n- Option Value Validator\n- Option Suggestions\n- SPL library.\n- HHVM support.\n\n## Requirements\n\n* PHP 5.3+\n\n## Install From Composer\n\n```sh\ncomposer require corneltek/getoptionkit\n```\n\n\n## Supported Option Formats\n\nsimple flags:\n\n```sh\nprogram.php -a -b -c\nprogram.php -abc\nprogram.php -vvv   # incremental flag v=3\nprogram.php -a -bc\n```\n\nwith multiple values:\n\n```sh\nprogram.php -a foo -a bar -a zoo -b -b -b\n```\n\nspecify value with equal sign:\n\n```sh\nprogram.php -a=foo\nprogram.php --long=foo\n```\n\nwith normal arguments:\n\n```\nprogram.php -a=foo -b=bar arg1 arg2 arg3\nprogram.php arg1 arg2 arg3 -a=foo -b=bar\n```\n\n## Option SPEC\n\n    v|verbose    flag option (with boolean value true)\n    d|dir:       option require a value (MUST require)\n    d|dir+       option with multiple values.\n    d|dir?       option with optional value\n    dir:=string  option with type constraint of string\n    dir:=number  option with type constraint of number\n    dir:=file    option with type constraint of file\n    dir:=date    option with type constraint of date\n    dir:=boolean option with type constraint of boolean\n    d            single character only option\n    dir          long option name\n\n## Command Line Forms\n\n    app [app-opts] [app arguments]\n\n    app [app-opts] subcommand [subcommand-opts] [subcommand-args]\n\n    app [app-opts] subcmd1 [subcmd-opts1] subcmd2 [subcmd-opts] subcmd3 [subcmd-opts3] [subcommand arguments....]\n\n\n## Documentation\n\nSee more details in the [documentation](https://github.com/c9s/GetOptionKit/wiki)\n\n## Demo\n\nPlease check `examples/demo.php`.\n\nRun:\n\n```sh\n% php examples/demo.php -f test -b 123 -b 333\n```\n\nPrint:\n\n    * Available options:\n          -f, --foo \u003cvalue\u003e    option requires a value.\n         -b, --bar \u003cvalue\u003e+    option with multiple value.\n        -z, --zoo [\u003cvalue\u003e]    option with optional value.\n              -v, --verbose    verbose message.\n                -d, --debug    debug message.\n                     --long    long option name only.\n                         -s    short option name only.\n    Enabled options: \n    * key:foo      spec:-f, --foo \u003cvalue\u003e  desc:option requires a value.\n        value =\u003e test\n\n    * key:bar      spec:-b, --bar \u003cvalue\u003e+  desc:option with multiple value.\n        Array\n        (\n            [0] =\u003e 123\n            [1] =\u003e 333\n        )\n\n## Synopsis\n\n```php\nuse GetOptionKit\\OptionCollection;\nuse GetOptionKit\\OptionParser;\nuse GetOptionKit\\OptionPrinter\\ConsoleOptionPrinter;\n\n$specs = new OptionCollection;\n$specs-\u003eadd('f|foo:', 'option requires a value.' )\n    -\u003eisa('String');\n\n$specs-\u003eadd('b|bar+', 'option with multiple value.' )\n    -\u003eisa('Number');\n\n$specs-\u003eadd('ip+', 'Ip constraint' )\n    -\u003eisa('Ip');\n\n$specs-\u003eadd('email+', 'Email address constraint' )\n    -\u003eisa('Email');\n\n$specs-\u003eadd('z|zoo?', 'option with optional value.' )\n    -\u003eisa('Boolean');\n\n$specs-\u003eadd('file:', 'option value should be a file.' )\n    -\u003eisa('File');\n\n$specs-\u003eadd('v|verbose', 'verbose message.' );\n$specs-\u003eadd('d|debug', 'debug message.' );\n$specs-\u003eadd('long', 'long option name only.' );\n$specs-\u003eadd('s', 'short option name only.' );\n\n$printer = new ConsoleOptionPrinter();\necho $printer-\u003erender($specs);\n\n$parser = new OptionParser($specs);\n\necho \"Enabled options: \\n\";\ntry {\n    $result = $parser-\u003eparse( $argv );\n    foreach ($result-\u003ekeys as $key =\u003e $spec) {\n        print_r($spec);\n    }\n\n    $opt = $result-\u003ekeys['foo']; // return the option object.\n    $str = $result-\u003ekeys['foo']-\u003evalue; // return the option value\n    \n    print_r($opt);\n    var_dump($str);\n    \n} catch( Exception $e ) {\n    echo $e-\u003egetMessage();\n}\n```\n\n\n## Documentation\n\nSee \u003chttps://github.com/c9s/GetOptionKit/wiki\u003e for more details.\n\n### Option Value Type\n\nThe option value type help you validate the input,\nthe following list is the current supported types:\n\n- `string`\n- `number`\n- `boolean`\n- `file`\n- `date`\n- `url`\n- `email`\n- `ip`\n- `ipv4`\n- `ipv6`\n- `regex`\n\nAnd here is the related sample code:\n\n```php\n$opt-\u003eadd( 'f|foo:' , 'with string type value' )\n    -\u003eisa('string');\n\n$opt-\u003eadd( 'b|bar+' , 'with number type value' )\n    -\u003eisa('number');\n\n$opt-\u003eadd( 'z|zoo?' , 'with boolean type value' )\n    -\u003eisa('boolean');\n\n$opt-\u003eadd( 'file:' , 'with file type value' )\n    -\u003eisa('file');\n\n$opt-\u003eadd( 'date:' , 'with date type value' )\n    -\u003eisa('date');\n\n$opt-\u003eadd( 'url:' , 'with url type value' )\n    -\u003eisa('url');\n\n$opt-\u003eadd( 'email:' , 'with email type value' )\n    -\u003eisa('email');\n\n$opt-\u003eadd( 'ip:' , 'with ip(v4/v6) type value' )\n    -\u003eisa('ip');\n\n$opt-\u003eadd( 'ipv4:' , 'with ipv4 type value' )\n    -\u003eisa('ipv4');\n\n$opt-\u003eadd( 'ipv6:' , 'with ipv6 type value' )\n    -\u003eisa('ipv6');\n\n$specs-\u003eadd('r|regex:', 'with custom regex type value')\n      -\u003eisa('Regex', '/^([a-z]+)$/');\n```\n\n\u003e Please note that currently only `string`, `number`, `boolean` types can be validated.\n\n### ContinuousOptionParser\n\n```php\n$specs = new OptionCollection;\n$spec_verbose = $specs-\u003eadd('v|verbose');\n$spec_color = $specs-\u003eadd('c|color');\n$spec_debug = $specs-\u003eadd('d|debug');\n$spec_verbose-\u003edescription = 'verbose flag';\n\n// ContinuousOptionParser\n$parser = new ContinuousOptionParser( $specs );\n$result = $parser-\u003eparse(explode(' ','program -v -d test -a -b -c subcommand -e -f -g subcommand2'));\n$result2 = $parser-\u003econtinueParse();\n```\n\n### OptionPrinter\n\nGetOptionKit\\OptionPrinter can print options for you:\n\n    * Available options:\n                  -f, --foo   option requires a value.\n                  -b, --bar   option with multiple value.\n                  -z, --zoo   option with optional value.\n              -v, --verbose   verbose message.\n                -d, --debug   debug message.\n                     --long   long option name only.\n                         -s   short option name only.\n\n\n## Command-line app with subcommands\n\nFor application with subcommands is designed by following form:\n\n\n    [app name] [app opts] \n                 [subcommand1] [subcommand-opts]\n                 [subcommand2] [subcommand-opts]\n                 [subcommand3] [subcommand-opts]\n                 [arguments]\n\nYou can check the `tests/GetOptionKit/ContinuousOptionParserTest.php` unit test file:\n\n```php\n// subcommand stack\n$subcommands = array('subcommand1','subcommand2','subcommand3');\n\n// different command has its own options\n$subcommandSpecs = array(\n    'subcommand1' =\u003e $cmdspecs,\n    'subcommand2' =\u003e $cmdspecs,\n    'subcommand3' =\u003e $cmdspecs,\n);\n\n// for saved options\n$subcommandOptions = array();\n\n// command arguments\n$arguments = array();\n\n$argv = explode(' ','program -v -d -c subcommand1 -a -b -c subcommand2 -c subcommand3 arg1 arg2 arg3');\n\n// parse application options first\n$parser = new ContinuousOptionParser( $appspecs );\n$app_options = $parser-\u003eparse( $argv );\nwhile (! $parser-\u003eisEnd()) {\n    if (@$subcommands[0] \u0026\u0026 $parser-\u003egetCurrentArgument() == $subcommands[0]) {\n        $parser-\u003eadvance();\n        $subcommand = array_shift( $subcommands );\n        $parser-\u003esetSpecs( $subcommandSpecs[$subcommand] );\n        $subcommandOptions[ $subcommand ] = $parser-\u003econtinueParse();\n    } else {\n        $arguments[] = $parser-\u003eadvance();\n    }\n}\n```\n\n## Todo\n\n* Option Spec group.\n* option valid value checking.\n* custom command mapping.\n\n## Command Line Utility Design Concept\n\n* main program name should be easy to type, easy to remember.\n* subcommand should be easy to type, easy to remember. length should be shorter than 7 characters.\n* options should always have long descriptive name\n* a program should be easy to check usage.\n\n## General command interface\n\nTo list usage of all subcommands or the program itself:\n\n\t$ prog help\n\nTo list the subcommand usage\n\n\t$ prog help subcommand subcommand2 subcommand3\n\n## Hacking\n\nFork this repository and clone it:\n\n    $ git clone git://github.com/c9s/GetOptionKit.git\n    $ cd GetOptionKit\n    $ composer install\n\nRun PHPUnit to test:\n\n    $ phpunit \n\n## License\n\nThis project is released under MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc9s%2FGetOptionKit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fc9s%2FGetOptionKit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc9s%2FGetOptionKit/lists"}