{"id":13409530,"url":"https://github.com/CHH/optparse","last_synced_at":"2025-03-14T14:31:31.668Z","repository":{"id":4430200,"uuid":"5568224","full_name":"CHH/optparse","owner":"CHH","description":"Another Command Line Argument Parser","archived":false,"fork":false,"pushed_at":"2016-10-07T23:42:47.000Z","size":131,"stargazers_count":19,"open_issues_count":4,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-13T11:26:29.249Z","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/CHH.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-27T07:59:17.000Z","updated_at":"2023-11-11T05:14:39.000Z","dependencies_parsed_at":"2022-09-21T16:02:25.579Z","dependency_job_id":null,"html_url":"https://github.com/CHH/optparse","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CHH%2Foptparse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CHH%2Foptparse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CHH%2Foptparse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CHH%2Foptparse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CHH","download_url":"https://codeload.github.com/CHH/optparse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243593455,"owners_count":20316187,"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-07-30T20:01:01.693Z","updated_at":"2025-03-14T14:31:28.992Z","avatar_url":"https://github.com/CHH.png","language":"PHP","funding_links":[],"categories":["命令行","Command Line","PHP","命令行( Command Line )","命令行 Command Line"],"sub_categories":[],"readme":"# Optparse — Another Command Line Argument Parser\n\n## Install\n\n1\\. Get [composer](http://getcomposer.org).\n\n2\\. Put this into your local `composer.json`:\n```\n{\n  \"require\": {\n    \"chh/optparse\": \"*@dev\"\n  }\n}\n```\n\n3\\. `php composer.phar install`\n\n## Example\n\n`hello.php`:\n\n```php\n\u003c?php\n\nrequire \"vendor/autoload.php\";\n\nuse CHH\\Optparse;\n\n$parser = new Optparse\\Parser(\"Says Hello\");\n\nfunction usage_and_exit()\n{\n    global $parser;\n    fwrite(STDERR, \"{$parser-\u003eusage()}\\n\");\n    exit(1);\n}\n\n$parser-\u003eaddFlag(\"help\", array(\"alias\" =\u003e \"-h\"), \"usage_and_exit\");\n$parser-\u003eaddFlag(\"shout\", array(\"alias\" =\u003e \"-S\"));\n$parser-\u003eaddArgument(\"name\", array(\"required\" =\u003e true));\n\ntry {\n    $parser-\u003eparse();\n} catch (Optparse\\Exception $e) {\n    usage_and_exit();\n}\n\n$msg = \"Hello {$parser[\"name\"]}!\";\n\nif ($parser[\"shout\"]) {\n    $msg = strtoupper($msg);\n}\n\necho \"$msg\\n\";\n```\n\nTry it:\n\n    % php hello.php Christoph --shout\n    HELLO CHRISTOPH!\n\n## Use\n\nThere are two things you will define in the parser:\n\n - _Flags_, arguments which start with one or two dashes and are\n   considered as options of your program.\n - _Arguments_, everything else which is not a flag.\n\nThe main point of interest is the `CHH\\Optparse\\Parser`, which you can\nuse to define _Flags_ and _Arguments_.\n\n### Flags\n\nTo define a flag, pass the flag's name to the `addFlag` method:\n\n```php\n\u003c?php\n\n$parser = new CHH\\Optparse\\Parser;\n\n$parser-\u003eaddFlag(\"help\");\n$parser-\u003eparse();\n\nif ($parser[\"help\"]) {\n    echo $parser-\u003eusage();\n    exit;\n}\n```\n\nA flag defined with `addFlag` is by default available as `--$flagName`.\nTo define another name (e.g. a short name) for the flag, pass it as the\nvalue of the `alias` option in the options array:\n\n```php\n\u003c?php\n$parser-\u003eaddFlag(\"help\", [\"alias\" =\u003e \"-h\"]);\n```\n\nThis way the `help` flag is available as `--help` _and_ `-h`.\n\nFlags don't expect values following them by default. To turn this on set the flag's `has_value` option\nto `true`:\n\n```php\n\u003c?php\n\n$parser-\u003eaddFlag(\"name\", [\"has_value\" =\u003e true]);\n$parser-\u003eparse(['--name', 'John']);\n\necho \"Hello World {$parser[\"name\"]}!\\n\";\n```\n\nYou can assign a default value to a flag, by setting the `default` option:\n\n```php\n\u003c?php\n\n$parser-\u003eaddFlag(\"pid_file\", [\"default\" =\u003e \"/var/tmp/foo.pid\", \"has_value\" =\u003e true]);\n\n$parser-\u003eparse([]);\n\necho \"{$parser[\"pid_file\"]}\\n\";\n// Output:\n// /var/tmp/foo.pid\n```\n\nYou can also bind the flag directly to a reference, by passing the reference in the `var` option\nor by using the `addFlagVar` method and passing it the variable:\n\n```php\n\u003c?php\n\n$foo = null;\n$bar = null;\n\n$parser-\u003eaddFlag(\"foo\", [\"var\" =\u003e \u0026$foo, \"has_value\" =\u003e true]);\n$parser-\u003eaddFlagVar(\"bar\", $bar, [\"has_value\" =\u003e true]);\n\n$parser-\u003eparse(['--foo', 'foo', '--bar', 'bar']);\n\necho \"$foo\\n\";\necho \"$bar\\n\";\n// Output:\n// foo\n// bar\n```\n\nThe parser also supports callbacks for flags. These are passed to\n`addFlag` as last argument. The callback is called everytime the parser\nencounters the flag. It gets passed a reference to the flag's value (`true` if it\nhasn't one). Use cases for this include splitting a string in pieces or\nrunning a method when a flag is passed:\n\n```php\n\u003c?php\n\n$parser = new Parser;\n\nfunction usage_and_exit()\n{\n    global $parser;\n    echo $parser-\u003eusage(), \"\\n\";\n    exit;\n}\n\n$parser-\u003eaddFlag(\"help\", ['alias' =\u003e '-h'], \"usage_and_exit\");\n\n$parser-\u003eaddFlag(\"queues\", [\"has_value\" =\u003e true], function(\u0026$value) {\n    $value = explode(',', $value);\n});\n```\n\nThe call to `parse` takes an array of arguments, or falls back to using\nthe arguments from `$_SERVER['argv']`. The `parse` method throws an\n`CHH\\Optparse\\ArgumentException` when a required flag or argument is missing, so make\nsure to catch this Exception and provide the user with a nice error\nmessage.\n\nThe parser is also able to generate a usage message for the command by\nlooking at the defined flags and arguments. Use the `usage` method to\nretrieve it.\n\n### Named Arguments\n\nNamed arguments can be added by using the `addArgument` method, which\ntakes the argument's name as first argument and then an array of\noptions.\n\nThese options are supported for arguments:\n\n - `default`: Default Value (default: `null`).\n - `var_arg`: Makes this a variable length argument (default: `false`).\n - `help`: Help text, used to describe the argument in the usage message generated by `usage()` (default: `null`).\n - `required`: Makes this argument required, the parser throws an exception when the\n   argument is omitted in the argument list (default: `false`).\n\nAs opposed to flags, the order **matters** in which you define your\narguments.\n\nVariable length arguments can be defined by setting the `var_arg` option\nto `true` in the options array. Variable arguments can only be at the\nlast position, and arguments defined after an variable argument are\nnever set.\n\n```php\n\u003c?php\n\n$parser-\u003eaddArgument(\"files\", [\"var_arg\" =\u003e true]);\n\n// Will always be null, because the value will be consumed by the\n// \"var_arg\" enabled argument.\n$parser-\u003eaddArgument(\"foo\");\n\n$parser-\u003eparse([\"foo\", \"bar\", \"baz\"]);\n\nforeach ($parser[\"files\"] as $file) {\n    echo $file, \"\\n\";\n}\n// Output:\n// foo\n// bar\n// baz\n```\n\nArguments can also be retrieved by using the `args`, `arg` and `slice` methods:\n\n```php\n\u003c?php\n\n$parser-\u003eaddArgument(\"foo\");\n$parser-\u003eparse([\"foo\", \"bar\", \"baz\"]);\n\necho var_export($parser-\u003eargs());\n// Output:\n// array(\"foo\", \"bar\", \"baz\")\n\n// Can also be used to fetch named arguments:\necho var_export($parser-\u003earg(0));\necho var_export($parser-\u003earg(\"foo\"));\n// Output:\n// \"foo\"\n// \"foo\"\n\n// Pass start and length:\necho var_export($parser-\u003eslice(0, 2));\n// Output:\n// array(\"foo\", \"bar\");\n```\n\n## License\n\nCopyright (c) 2012 Christoph Hochstrasser\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCHH%2Foptparse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCHH%2Foptparse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCHH%2Foptparse/lists"}