{"id":18427124,"url":"https://github.com/nowisesys/uup-application-options","last_synced_at":"2025-04-13T19:38:48.552Z","repository":{"id":57028823,"uuid":"445494783","full_name":"nowisesys/uup-application-options","owner":"nowisesys","description":"Uniform CLI command line and HTTP request options.","archived":false,"fork":false,"pushed_at":"2022-01-25T02:14:24.000Z","size":37,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-16T08:27:30.175Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nowisesys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-07T11:14:48.000Z","updated_at":"2022-01-07T12:01:17.000Z","dependencies_parsed_at":"2022-08-23T18:50:11.789Z","dependency_job_id":null,"html_url":"https://github.com/nowisesys/uup-application-options","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nowisesys%2Fuup-application-options","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nowisesys%2Fuup-application-options/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nowisesys%2Fuup-application-options/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nowisesys%2Fuup-application-options/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nowisesys","download_url":"https://codeload.github.com/nowisesys/uup-application-options/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248769428,"owners_count":21158812,"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-11-06T05:09:43.674Z","updated_at":"2025-04-13T19:38:48.531Z","avatar_url":"https://github.com/nowisesys.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"UUP-APPLICATION-OPTIONS\n=======================================================================\n\nSupports transparent/uniform handling of runtime options from CLI command and HTTP request.\n\nThis package supports short/long options, other options and reading password from terminal (masked echo output). For\nHTTP request options, an optional filter can be applied.\n\n### USAGE:\n\nIn your command action class, define a method that checks whether to return command line options (CLI) or from request\noption (HTTP).\n\n```php\nprivate function getApplicationOptions(): ApplicationOptionsInterface\n{\n    if (php_sapi_name() == 'cli') {\n        return new CommandLineOptions();\n    } else {\n        return new HttpRequestOptions();\n    }\n}\n```\n\nCalling `getApplicationOptions` will provide uniform access to application options, whether these come from command line\nor the HTTP request. From your application perspective the origin of options is transparent.\n\nFrom within the application, a number of convenient methods can be used for checking if options was passed and retrieve\nthem with type safety.\n\n```php\npublic function setup() \n{\n    $this-\u003eoptions = $this-\u003egetApplicationOptions();\n}\n\npublic function execute(): void \n{\n    // \n    // Example of setting defaults:\n    // \n    if ($this-\u003eoptions-\u003eisMissing('user')) {\n        $this-\u003eoptions-\u003esetOption('user', 'adam');\n    }\n    \n    // \n    // Take action if option is defined:\n    // \n    if ($this-\u003eoptions-\u003ehasOption('email')) {\n        $this-\u003esendEmail($this-\u003eoptions-\u003egetString('email'));\n    }\n}\n```\n\nA number of other getters exist, for example for boolean, float and integer values. These takes a second default value\nthat is returned of option is missing.\n\n### ORIGIN:\n\nIf origin matters, it can be checked:\n\n```php\nif ($this-\u003eoptions-\u003egetOrigin() == ApplicationOptionsInterface::ORIGIN_HTTP) {\n    throw new RuntimeException(\"This action can't be run from a HTTP context\");\n}\n```\n\n### OPTION ALIAS:\n\nFor command line options, the default behavior is to return options by stripping leading dashes ('-'). To support short\ncommand options mapped to long options, pass a mapping array.\n\n```php\nprivate function getApplicationOptions(): ApplicationOptionsInterface\n{\n    if (php_sapi_name() == 'cli') {\n        return new CommandLineOptions([\n            '-f' =\u003e 'file',\n            '-U' =\u003e 'user'\n        ]);\n    } else {\n        return new HttpRequestOptions();\n    }\n}\n```\n\nThese two short option will now be an alias for their equivalent long option. Some builtin aliases are implicit handled\nfor command line options. These short options are:\n\n* -h =\u003e help\n* -V =\u003e version\n* -d =\u003e debug\n* -v =\u003e verbose\n* -q =\u003e quiet\n\nThese are processed before user defined mappings, making it possible to easily redefine the builtin mapping.\n\n### OPTION VALUES:\n\nOption values are any value after the equal character ('='). For options without a value, the option key will be read\nhaving boolean true as its value.\n\n### DASHES:\n\nProcessing of command line options (CLI) will strip leading dashes and use the remaining string as the command option\nkey.\n\n### OPTION FILTERING:\n\nCommand line options are considered safe. For HTTP request, it's possible to pass an array of sanitation filter to be\napplied.\n\n```php\nprivate function getApplicationOptions(): ApplicationOptionsInterface\n{\n    if (php_sapi_name() == 'cli') {\n        return new CommandLineOptions();\n    } else {\n        return new HttpRequestOptions(\n            new HttpRequestFilter([\n                'user'  =\u003e FILTER_SANITIZE_STRING,\n                'email' =\u003e FILTER_SANITIZE_EMAIL\n            ])\n        );\n    }\n}\n```\n\nThe default behavior is to not filter HTTP request options. For larger applications, some framework for purify HTML\ninput might be used that could be wrapped in a class that implements the `FilterInterface` and used instead of passing\nan instance of the `HttpRequestFilter` class.\n\n### BOOLEANS:\n\nSpecial treatment of boolean options are implemented. For example, option values \"1\", \"true\", \"on\" and \"yes\" yields\ntrue. Analogous \"0\", \"false\", \"off\" and \"no\" yields false.\n\nExample: Call `getBoolean` to have the value for filter option evaluated as boolean.\n\n```php\n$this-\u003eoptions-\u003egetBoolean(\"filter\");\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnowisesys%2Fuup-application-options","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnowisesys%2Fuup-application-options","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnowisesys%2Fuup-application-options/lists"}