{"id":21571132,"url":"https://github.com/cheprasov/php-cli-args","last_synced_at":"2025-07-23T17:07:12.027Z","repository":{"id":62501531,"uuid":"73003436","full_name":"cheprasov/php-cli-args","owner":"cheprasov","description":"Class CliArgs helps to get cli options/params from the command line argument list easy.","archived":false,"fork":false,"pushed_at":"2021-06-10T19:43:27.000Z","size":37,"stargazers_count":13,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-21T17:58:44.594Z","etag":null,"topics":["arguments","argv","cli","cli-args","php"],"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/cheprasov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"cheprasov","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2016-11-06T16:27:34.000Z","updated_at":"2023-01-21T15:27:46.000Z","dependencies_parsed_at":"2022-11-02T12:01:40.793Z","dependency_job_id":null,"html_url":"https://github.com/cheprasov/php-cli-args","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/cheprasov/php-cli-args","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-cli-args","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-cli-args/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-cli-args/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-cli-args/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cheprasov","download_url":"https://codeload.github.com/cheprasov/php-cli-args/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cheprasov%2Fphp-cli-args/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266717709,"owners_count":23973384,"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","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["arguments","argv","cli","cli-args","php"],"created_at":"2024-11-24T11:14:59.313Z","updated_at":"2025-07-23T17:07:11.999Z","avatar_url":"https://github.com/cheprasov.png","language":"PHP","readme":"[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)\n# CliArgs v3.0.0 for PHP \u003e= 5.5\n\n## About\nClass **CliArgs** helps to get options from the command line argument list easy.\n\n## Features\n- CliArgs uses **$GLOBAL['argv']** as base, and it does not use function **getopt()**.\n- It does not implement logic for 'required', it should be in your side.\n- It helps to get options easy from command line argument list.\n- It generates help info about options based on config.\n- Flexible configuration and data filtering.\n\n## Note\nThis class is not workable when [register_argc_argv](http://php.net/manual/en/ini.core.php#ini.register-argc-argv) is disabled.\n\n## Usage\n\n### Example\n\nrun\n```\n\u003e example.php --name=Alexander --age=32 --sex=m\nor\n\u003e example.php -n Alexander -a 32 -s m\n```\n\nexample.php\n```php\n$config = [\n    'name' =\u003e 'n',\n    'age' =\u003e 'a',\n    'sex' =\u003e 's'\n];\n$CliArgs = new CliArgs($config);\n\necho $CliArgs-\u003egetArg('name'); // Alexander\necho $CliArgs-\u003egetArg('n'); // Alexander\n\necho $CliArgs-\u003egetArg('age'); // 42\necho $CliArgs-\u003egetArg('a'); // 42\n\necho $CliArgs-\u003egetArg('sex'); // m\necho $CliArgs-\u003egetArg('s'); // m\n```\n\n### Config\n\nNote: all params from cli that you want to use should be specified in config, otherwise they will be ignored.\n```php\n$config = [\n    // You should specify key as name of option from the command line argument list.\n    // Example, name \u003cparam-name\u003e for --param-name option\n    'param-name' =\u003e [\n\n        'alias' =\u003e 'p',\n            // [optional], [string]\n            // Alias helps to have short or long name for this key.\n            // Example, name \u003cp\u003e for -p option\n\n        'default' =\u003e false,\n            // [optional], [mixed], [default = null]\n            // Default value will returned if param is not setted\n            // or params has not value.\n\n        'help' =\u003e 'Some description about param',\n            // [optional], [string]\n            // Text that will returned, if you request help\n\n        'filter' =\u003e 'int',\n            // [optional], [string | array | callable]\n            // Filter for the return value.\n            // You can use next filters: flag, bool, int, float, help, json, \u003carray\u003e, \u003cfunction\u003e\n\n            // 'int' - cast to integer before return.\n            // 'float' - cast to float before return.\n            // 'bool' - cast to bool before return. Yes, true, 1 = TRUE, other = FALSE\n            // 'json' - decode JSON data before return.\n            // 'flag' - will return TRUE, if key is exists in command line argument list, otherwise - FALSE\n            // \u003carray\u003e - use array for enums. Example use ['a', 'b', 'c'] to get only one of these.\n            // \u003ccallable\u003e - use function($value, $default) { ... } to process value by yourself\n    ]\n];\n\n$CliArgs = new CliArgs($config);\n```\n\nExamples of config:\n\nExample 1\n```php\n\n// Simple configs\n\n// The config1 and config2 are equal\n$config1 = ['foo', 'bar', 'a'];\n$config2 = [\n    'foo' =\u003e [],\n    'bar' =\u003e [],\n    'a' =\u003e [],\n];\n\n// The config3 and config4 are equal\n$config3 = ['foo' =\u003e 'f', 'bar' =\u003e 'b', 'a'];\n$config4 = [\n    'foo' =\u003e [\n        'alias' =\u003e 'f',\n    ],\n    'bar' =\u003e [\n        'alias' =\u003e 'b',\n    ],\n    'a' =\u003e [],\n];\n```\n\nExample 2\n```php\n$config = [\n    'help' =\u003e [\n        'alias' =\u003e 'h',\n        'help' =\u003e 'Show help about all options',\n    ],\n    'data' =\u003e [\n        'alias' =\u003e 'd',\n        'filter' =\u003e 'json',\n        'help' =\u003e 'Some description about this param',\n    ],\n    'user-id' =\u003e [\n        'alias' =\u003e 'u',\n        'filter' =\u003e 'int',\n        'help' =\u003e 'Some description about this param',\n    ]\n];\n$CliArgs = new CliArgs($config);\n```\n```\nShow help\n\u003e some-script.php --help\n\u003c?php if ($CliArgs-\u003eisFlagExist('help', 'h')) echo $CliArgs-\u003egetHelp('help'); ?\u003e\n\nShow help only for param data\n\u003e some-script.php --help data\n\u003c?php if ($CliArgs-\u003eisFlagExist('help')) echo $CliArgs-\u003egetHelp('help'); ?\u003e\n\nShow help for all params data\n\u003e some-script.php --help data\n\u003c?php if ($CliArgs-\u003eisFlagExist('help')) echo $CliArgs-\u003egetHelp(); ?\u003e\n\nAll the same:\n\u003e some-script.php --data='{\"foo\":\"bar\"}' --user-id=42\nor\n\u003e some-script.php --data '{\"foo\":\"bar\"}' --user-id 42\nor\n\u003e some-script.php -d '{\"foo\":\"bar\"}' --user-id 42\nor\n\u003e some-script.php -d '{\"foo\":\"bar\"}' -u 42\n\n\u003c?php\n    print_r($CliArgs-\u003egetArg('data'));\n    print_r($CliArgs-\u003egetArg('d'));\n    print_r($CliArgs-\u003egetArg('user-id'));\n    print_r($CliArgs-\u003egetArg('u'));\n```\n\nExample 3\n```php\n    $config = [\n        'flag' =\u003e [\n            'alias' =\u003e 'f',\n            'filter' =\u003e 'flag',\n        ],\n        'id' =\u003e [\n            'filter' =\u003e 'int',\n        ],\n        'any' =\u003e [],\n    ];\n```\n```\n\u003e some-script.php --flag\n\n\u003e some-script.php -f\n\n\u003e some-script.php -f --id=42 --any=\"any value\"\n\n\u003e some-script.php --any=\"any value\"\n\n\u003c?php\n    print_r($CliArgs-\u003eisFlagExist('flag')); // or $CliArgs-\u003eisFlagExist('f')\n    print_r($CliArgs-\u003egetArg('data'));\n    print_r($CliArgs-\u003egetArg('d'));\n    print_r($CliArgs-\u003egetArg('user-id'));\n    print_r($CliArgs-\u003egetArg('u'));\n\n```\n\nExample 4\n```php\n    $config = [\n        'name' =\u003e [\n            'alias' =\u003e 'n',\n            'filter' =\u003e function($name, $default) {\n                return $name ? mb_convert_case($name, MB_CASE_TITLE, 'UTF-8') : $defult;\n            },\n            'default' =\u003e 'No name',\n        ],\n        'sex' =\u003e [\n            'alias' =\u003e 's',\n            'filter' =\u003e ['m', 'f'],\n            'default' =\u003e null,\n        ],\n        'city' =\u003e [\n            'alias' =\u003e 'c',\n            'filter' =\u003e function($city) {\n                // ... some checks of city\n            },\n        ],\n        'email' =\u003e [\n            'alias' =\u003e 'e',\n            'filter' =\u003e function($city) {\n                // ... some checks of email\n            },\n        ]\n    ];\n```\n```\n\u003e some-script.php --name alexander\n\n\u003e some-script.php -f\n\n\u003e some-script.php -f --id=42 --any=\"any value\"\n\n\u003e some-script.php --any=\"any value\"\n\n\u003c?php\n    print_r($CliArgs-\u003egetArg('name'));\n    print_r($CliArgs-\u003e('d'));\n    print_r($CliArgs-\u003egetArg('user-id'));\n    print_r($CliArgs-\u003egetArg('u'));\n```\n\n### Create a new instance\n```php\n// simple config\n$config = ['foo' =\u003e 'f', 'bar' =\u003e 'b'];\n$CliArgs = new CliArgs($config);\n```\n\n### Methods\n\n```\n\u003e example.php --foo Hello --bar World\n```\n\n##### new CliArgs(array|null $config = null)\nConstructor. If config contents wrong aliases then ConfigErrorException will be thrown.\n```php\n$config = ['foo' =\u003e 'f', 'bar' =\u003e 'b'];\n$CliArgs = new CliArgs($config);\n```\n\n##### getArgs(): array\nThe method returns all passed arguments which is specified in config.\n```php\n$argv = $CliArgs-\u003egetArgs();\nprint_r($argv);\n// array(\n//    'foo' =\u003e 'Hello',\n//    'bar' =\u003e 'World',\n// )\n```\n\n##### getArg(string $key): mixed | null\nReturns value for argument by key. If argument is not set, then it will return default value or `null`\n```php\n$arg = $CliArgs-\u003egetArg('foo');\n// or $CliArgs-\u003egetArg('f');\necho $arg; // Hello\n```\n\n##### isFlagExist(string $key, boolean $checkAlias = true): bool\nReturn `true` if key exists, otherwise the method returns `false`\nIf `$checkAlias` is `true`, then the method will check key and alias, and will return `true` if key or alias exists.\nIf `$checkAlias` is `false`, then the method will check only key and will return `true` only if key exists.\n```php\n// some_script.php --foo\n\n$CliArgs = new $CliArgs(['foo' =\u003e 'f']);\n\necho $CliArgs-\u003eisFlagExist('foo'); // true\necho $CliArgs-\u003eisFlagExist('f'); // true\n\necho $CliArgs-\u003eisFlagExist('foo', false); // true\necho $CliArgs-\u003eisFlagExist('f', false); // false\n```\n\n##### getArguments(): array\nGet prepared arguments from ARGV\n```php\nprint_r($CliArgs-\u003egetArguments());\n// array(\n//     0 =\u003e 'example.php'\n//    'foo' =\u003e 'Hello',\n//    'bar' =\u003e 'World',\n// )\n```\n\n##### geHelp([string $value = null]): string\nGet help\n```php\necho $CliArgs-\u003egetHelp(); //  Get help for all params\necho $CliArgs-\u003egetHelp('help'); //  Get help for secified params: --help data\n```\n\n## Installation\n\n### Composer\n\nDownload composer:\n\n    wget -nc http://getcomposer.org/composer.phar\n\nand add dependency to your project:\n\n    php composer.phar require cheprasov/php-cli-args\n\n## Running tests\n\n    ./vendor/bin/phpunit\n\n## Something doesn't work\n\nFeel free to fork project, fix bugs and finally request for pull (do not forget write tests please)\n","funding_links":["https://github.com/sponsors/cheprasov"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fphp-cli-args","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcheprasov%2Fphp-cli-args","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcheprasov%2Fphp-cli-args/lists"}