{"id":23893675,"url":"https://github.com/aaronngi/petopts","last_synced_at":"2025-04-10T13:06:48.191Z","repository":{"id":188495601,"uuid":"446357220","full_name":"aaronNGi/petopts","owner":"aaronNGi","description":"Portable and simple option parsing, powered by getopts(1p).","archived":false,"fork":false,"pushed_at":"2022-01-10T17:55:36.000Z","size":8,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-24T11:45:43.764Z","etag":null,"topics":["getopts","option-parser","options-parsing","portable","posix","shell"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aaronNGi.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,"governance":null}},"created_at":"2022-01-10T09:25:44.000Z","updated_at":"2024-11-30T01:04:47.000Z","dependencies_parsed_at":"2023-08-15T16:11:18.962Z","dependency_job_id":"2dde5e79-0cfc-44d1-baa8-9a1265cad0ae","html_url":"https://github.com/aaronNGi/petopts","commit_stats":null,"previous_names":["aaronngi/petopts"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaronNGi%2Fpetopts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaronNGi%2Fpetopts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaronNGi%2Fpetopts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aaronNGi%2Fpetopts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aaronNGi","download_url":"https://codeload.github.com/aaronNGi/petopts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248223828,"owners_count":21068069,"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":["getopts","option-parser","options-parsing","portable","posix","shell"],"created_at":"2025-01-04T14:28:23.825Z","updated_at":"2025-04-10T13:06:48.172Z","avatar_url":"https://github.com/aaronNGi.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":" petopts\n========================================================================\n\nPortable and simple option parsing, powered by getopts(1p).\n\n\u003e pet /pɛt/ To stroke in a gentle or loving manner.\n\nUnlike most (bash) option parser frameworks, where the complexity can\neasily dwarf that of the actual script, petopts is purposefully made as\nsimple and concise as possible.\n\n\n Features\n------------------------------------------------------------------------\n\n* Easy to use\n* Minimal/simple\n* Portable\n* No GNU style long options\n* No optional parameters\n* Count of option occurrences\n* Short usage message\n* Support `--` argument to indicates the end of the options\n* Proper separation of option parsing and handling\n* POSIX utility conventions compliance\n\n\n Usage\n------------------------------------------------------------------------\n\n### tl;dr\n\n1. Copy the content of [petopts.sh](petopts.sh) to the top of a script\n2. Change `options`, `usage` and `version`\n3. Handle the parsed options and remaining operands\n\nGiven `options=\"a b: c\"`, petopts would set `opt_a`, `opt_b` and `opt_c`\nto the amount each of the options are supplied (empty if 0). `arg_b`\nwould contain the option argument of the -b option.\n\n\n### Details\n\n[petopts.sh](petopts.sh) is supposed to be used at the top of a script.\nThe following 3 variables have to be adapted:\n\n`options`: A string containing the space separated list of alphanumeric\noption characters. If a character is followed by a \\\u003ccolon\u003e, the option\nshall be expected to have an argument.\n\n`usage`: The string printed by the `usage()` function.\n\n`version`: The string printed by the `version()` function.\n\nFor each option in the `options` variable, petopts will set\ncorresponding variables in the form of `opt_*` and `arg_*`, where `*` is\nthe option character. The `opt_*` variables will hold the number of\noccurrences of the options (unset if an option does not occur), while\nthe `arg_*` variables store the option arguments (empty if an option has\nno option argument).\n\nAfter parsing the options, the positional parameters are shifted until\nonly operands remain.\n\nFor example, to conditionally do something with the option 'a', 'h' and\n'v', we could do:\n\n```sh\n[ \"$opt_h\" ] \u0026\u0026 usage\n[ \"$opt_v\" ] \u0026\u0026 version\n[ \"$opt_a\" ] \u0026\u0026 printf 'Option a present with argument: %s\\n' \"$arg_a\"\n```\n\npetopts provides the following functions:\n\n`die()`: Prints its arguments to stderr and exits the script with status\n1.\n\n`usage()`: Prints the content of the `usage` variable and exits the\nscript with status 0.\n\n`version()`: Prints the content of the `version` variable and exits the\nscript with status 0.\n\nFor a complete script using petopts, check [example.sh](example.sh).\n\n\n Usage of eval\n------------------------------------------------------------------------\n\n`eval` is considered dangerous because it can execute \"dirty\" data and\nthus allow arbitrary code execution. However, how petopts uses `eval`,\nto dynamically set the option variables, is totally safe.\n\n\n Limitations\n------------------------------------------------------------------------\n\n* No GNU style long options\n* No optional parameters\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faaronngi%2Fpetopts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faaronngi%2Fpetopts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faaronngi%2Fpetopts/lists"}