{"id":15048000,"url":"https://github.com/badaix/popl","last_synced_at":"2025-09-08T08:35:48.643Z","repository":{"id":44627367,"uuid":"47653014","full_name":"badaix/popl","owner":"badaix","description":"Header-only C++ program options parser library","archived":false,"fork":false,"pushed_at":"2023-03-16T15:49:15.000Z","size":262,"stargazers_count":176,"open_issues_count":6,"forks_count":33,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-31T06:11:21.274Z","etag":null,"topics":["argument-parser","bash-completion","command-line-parser","commandline-flags","cpp11","getopt","groff","header-only","option-parser","program-options"],"latest_commit_sha":null,"homepage":"","language":"C++","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/badaix.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,"roadmap":null,"authors":null,"dei":null}},"created_at":"2015-12-08T22:17:01.000Z","updated_at":"2025-03-23T03:21:44.000Z","dependencies_parsed_at":"2024-01-31T09:02:25.427Z","dependency_job_id":"46dba186-fb57-4f7f-b701-3c54cb5d259d","html_url":"https://github.com/badaix/popl","commit_stats":{"total_commits":113,"total_committers":7,"mean_commits":"16.142857142857142","dds":0.3097345132743363,"last_synced_commit":"bda5f43099d67419089a44c9e54474e4998a9a26"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/badaix%2Fpopl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/badaix%2Fpopl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/badaix%2Fpopl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/badaix%2Fpopl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/badaix","download_url":"https://codeload.github.com/badaix/popl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252810273,"owners_count":21807759,"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":["argument-parser","bash-completion","command-line-parser","commandline-flags","cpp11","getopt","groff","header-only","option-parser","program-options"],"created_at":"2024-09-24T21:06:45.038Z","updated_at":"2025-05-07T04:11:21.528Z","avatar_url":"https://github.com/badaix.png","language":"C++","readme":"# popl\n\nProgram Options Parser Library\n\n[![Github Releases](https://img.shields.io/github/release/badaix/popl.svg)](https://github.com/badaix/popl/releases)\n[![Build Status](https://travis-ci.org/badaix/popl.svg?branch=master)](https://travis-ci.org/badaix/popl)\n[![Language grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/badaix/popl.svg)](https://lgtm.com/projects/g/badaix/popl/context:cpp)  \n\npopl is a C++ command line arguments parser that supports the same set of options as GNU's `getopt` and thus closely follows the POSIX guidelines for the command-line options of a program.\n\n## Features\n\n* Single header file implementation. Simply include and use it!\n* No external dependencies, just C++11\n* Platform independent\n* Supports the same set of options as GNU's `getopt`: short options, long options, non-option arguments, ...\n* Supports parsing of `ini` files\n* Templatized option parsing: arguments are directly casted into the desired target type\n* Automatic creation of a usage message\n  * Console help message\n  * [Groff](https://www.gnu.org/software/groff/) formatted help message for use in man pages\n  * Script snippets for use in [bash completion](https://debian-administration.org/article/316/An_introduction_to_bash_completion_part_1) scripts\n* Easy to use: no strange braces syntax, but for each command line option one typesafe object\n\n## Howto\n\nKey object is `OptionParser`, which is populated with different option types:\n\n* `Value\u003cT\u003e` Option with argument  \n* `Switch` Option without argument  \n* `Implicit\u003cT\u003e` Option with optional argument (using an implicit value if no argument is given)  \n\nNext, OptionParser will parse the command line (by passing `argc` and `argv`) and fill the option objects.  \nEach option type is initialized with a short option, long option and a help message.  \n\n### Basic usage example\n\n```C++\nOptionParser op(\"Allowed options\");\nauto help_option   = op.add\u003cSwitch\u003e(\"h\", \"help\", \"produce help message\");\nauto string_option = op.add\u003cValue\u003cstd::string\u003e\u003e(\"s\", \"string\", \"some string value\");\nauto implicit_int  = op.add\u003cImplicit\u003cint\u003e\u003e(\"m\", \"implicit\", \"implicit value\", 42);\nop.parse(argc, argv);\n\n// print auto-generated help message\nif (help_option-\u003eis_set())\n\tcout \u003c\u003c op \u003c\u003c \"\\n\";\ncout \u003c\u003c \"string_option - is_set: \" \u003c\u003c string_option-\u003eis_set() \u003c\u003c \", value: \" \u003c\u003c string_option-\u003evalue() \u003c\u003c \"\\n\";\ncout \u003c\u003c \"implicit_int  - is_set: \" \u003c\u003c implicit_int-\u003eis_set() \u003c\u003c \", value: \" \u003c\u003c implicit_int-\u003evalue() \u003c\u003c \"\\n\";\n```\n\n### Multiple definition\n\nOptions can be set multiple times on command line. Use `count()` and `value(n)` to access them:\n\n```C++\ncout \u003c\u003c \"string_option - count: \" \u003c\u003c string_option-\u003ecount() \u003c\u003c \"\\n\";\nif (string_option-\u003eis_set())\n{\n\tfor (size_t n=0; n\u003cstring_option-\u003ecount(); ++n)\n\t\tcout \u003c\u003c \"string_option #\" \u003c\u003c n \u003c\u003c \" - value: \" \u003c\u003c string_option-\u003evalue(n) \u003c\u003c \"\\n\";\n}\n```\n\n### Default values\n\nEvery option type can have a default value:\n\n```C++\nauto string_option = op.add\u003cValue\u003cstd::string\u003e\u003e(\"s\", \"string\", \"some string value\", \"default value\");\n```\n\nif not set on command line, `string_option-\u003eis_set()` will be `false` and `string_option-\u003evalue()` will be `default value` \n  \n### Assigning to a variable\n\nThe argument of an option can be directly assigned to a variable:\n\n```C++\nstd::string s;\n/*auto string_option =*/ op.add\u003cValue\u003cstd::string\u003e\u003e(\"s\", \"string\", \"some string value\", \"default value\", \u0026s);\n```\n\nThe variable `s` will carry the same value as `string_option.value()`, and thus the declaration of `string_option` can be omitted.  \n  \n### Attributes of an option\n\nOptions have an `Attribute`: they can be hidden in the auto-created help message, or classified as \"advanced\", or \"expert\":\n\n```C++\nauto string_option = op.add\u003cValue\u003cstd::string\u003e\u003e(\"s\", \"string\", \"some string value\");\nauto advanced_int  = op.add\u003cValue\u003cint\u003e, Attribute::advanced\u003e(\"i\", \"integer\", \"advanced integer value\");\nauto hidden_bool   = op.add\u003cSwtich, Attribute::hidden\u003e(\"\", \"hidden\", \"hidden flag\");\n```\n\nNow `cout \u003c\u003c op.help()` (same as `cout \u003c\u003c op`) will not show the hidden or advanced option, while `cout \u003c\u003c op.help(Attribute::advanced)` will show the advanced option. The hidden one is never shown to the user.  \nAlso an option can be flagged as mandatory by assigning `Attribute::required`\n\n## Example\n\n```C++\n#include \"popl.hpp\"\n\nusing namespace std;\nusing namespace popl;\n\nint main(int argc, char **argv)\n{\n\tfloat f;\n\tint m, i;\n\tbool v;\n\n\tOptionParser op(\"Allowed options\");\n\tauto help_option     = op.add\u003cSwitch\u003e(\"h\", \"help\", \"produce help message\");\n\tauto verbose_option  = op.add\u003cSwitch\u003e(\"v\", \"verbose\", \"be verbose\", \u0026v);\n\tauto hidden_option   = op.add\u003cSwitch, Attribute::hidden\u003e(\"x\", \"\", \"hidden option\");\n\tauto double_option   = op.add\u003cValue\u003cdouble\u003e\u003e(\"d\", \"double\", \"test for double values\", 3.14159265359);\n\tauto float_option    = op.add\u003cValue\u003cfloat\u003e\u003e(\"f\", \"float\", \"test for float values\", 2.71828182845f, \u0026f);\n\t                       op.add\u003cValue\u003cint\u003e\u003e(\"i\", \"int\", \"test for int value w/o option\", 23, \u0026i);\n\tauto string_option   = op.add\u003cValue\u003cstring\u003e\u003e(\"s\", \"string\", \"test for string values\");\n\tauto implicit_int_option = op.add\u003cImplicit\u003cint\u003e\u003e(\"m\", \"implicit\", \"implicit test\", 42);\n\tauto advanced_option = op.add\u003cSwitch, Attribute::advanced\u003e(\"\", \"advanced\", \"advanced option\");\n\tauto expert_option   = op.add\u003cSwitch, Attribute::expert\u003e(\"\", \"expert\", \"expert option\");\n\tauto inactive_option = op.add\u003cSwitch\u003e(\"\", \"inactive\", \"inactive option\");\n\tinactive_option-\u003eset_attribute(Attribute::inactive);\n\timplicit_int_option-\u003eassign_to(\u0026m);\n\n\top.parse(argc, argv);\n\n\t// print auto-generated help message\n\tif (help_option-\u003ecount() == 1)\n\t\tcout \u003c\u003c op \u003c\u003c \"\\n\";\n\telse if (help_option-\u003ecount() == 2)\n\t\tcout \u003c\u003c op.help(Attribute::advanced) \u003c\u003c \"\\n\";\n\telse if (help_option-\u003ecount() \u003e 2)\n\t\tcout \u003c\u003c op.help(Attribute::expert) \u003c\u003c \"\\n\";\n\n\t// show all non option arguments (those without \"-o\" or \"--option\")\n\tfor (const auto\u0026 non_option_arg: op.non_option_args())\n\t\tcout \u003c\u003c \"non_option_args: \" \u003c\u003c non_option_arg \u003c\u003c \"\\n\";\n\n\t// show unknown options (undefined ones, like \"-u\" or \"--undefined\")\n\tfor (const auto\u0026 unknown_option: op.unknown_options())\n\t\tcout \u003c\u003c \"unknown_options: \" \u003c\u003c unknown_option \u003c\u003c \"\\n\";\n\n\t// print all the configured values\n\tcout \u003c\u003c \"verbose_option  - is_set: \" \u003c\u003c verbose_option-\u003eis_set() \u003c\u003c \", count: \" \u003c\u003c verbose_option-\u003ecount() \u003c\u003c \", reference: \" \u003c\u003c v \u003c\u003c \"\\n\";\n\tcout \u003c\u003c \"hidden_option   - is_set: \" \u003c\u003c hidden_option-\u003eis_set() \u003c\u003c \", count: \" \u003c\u003c hidden_option-\u003ecount() \u003c\u003c \"\\n\";\n\tcout \u003c\u003c \"double_option   - is_set: \" \u003c\u003c double_option-\u003eis_set() \u003c\u003c \", count: \" \u003c\u003c double_option-\u003ecount() \u003c\u003c \", value: \" \u003c\u003c double_option-\u003evalue() \u003c\u003c \"\\n\";\n\tcout \u003c\u003c \"string_option   - is_set: \" \u003c\u003c string_option-\u003eis_set() \u003c\u003c \", count: \" \u003c\u003c string_option-\u003ecount() \u003c\u003c \"\\n\";\n\tif (string_option-\u003eis_set())\n\t{\n\t  \tfor (size_t n=0; n\u003cstring_option-\u003ecount(); ++n)\n\t\t\tcout \u003c\u003c \"string_option #\" \u003c\u003c n \u003c\u003c \" - value: \" \u003c\u003c string_option-\u003evalue(n) \u003c\u003c \"\\n\";\n\t}\n\tcout \u003c\u003c \"float_option    - is_set: \" \u003c\u003c float_option-\u003eis_set() \u003c\u003c \", value: \" \u003c\u003c float_option-\u003evalue() \u003c\u003c \", reference: \" \u003c\u003c f \u003c\u003c \"\\n\";\n\tcout \u003c\u003c \"int w/o option  - reference: \" \u003c\u003c i \u003c\u003c \"\\n\";\n\tauto int_option = op.get_option\u003cValue\u003cint\u003e\u003e('i');\n\tcout \u003c\u003c \"int_option      - is_set: \" \u003c\u003c int_option-\u003eis_set() \u003c\u003c \", value: \" \u003c\u003c int_option-\u003evalue() \u003c\u003c \", reference: \" \u003c\u003c i \u003c\u003c \"\\n\";\n\tcout \u003c\u003c \"imp_int_option  - is_set: \" \u003c\u003c implicit_int_option-\u003eis_set() \u003c\u003c \", value: \" \u003c\u003c implicit_int_option-\u003evalue() \u003c\u003c \", reference: \" \u003c\u003c m \u003c\u003c \"\\n\";\n\tcout \u003c\u003c \"advanced_option - is_set: \" \u003c\u003c advanced_option-\u003eis_set() \u003c\u003c \", count: \" \u003c\u003c advanced_option-\u003ecount() \u003c\u003c \"\\n\";\n\tcout \u003c\u003c \"expert_option   - is_set: \" \u003c\u003c expert_option-\u003eis_set() \u003c\u003c \", count: \" \u003c\u003c expert_option-\u003ecount() \u003c\u003c \"\\n\";\n}\n```\n\nA call to `popl -s hello -h -m23 test` will produce an output like this:\n\n```shell\nAllowed options:\n  -h, --help                   produce help message\n  -v, --verbose                be verbose\n  -d, --double arg (=3.14159)  test for double values\n  -f, --float arg (=2.71828)   test for float values\n  -i, --int arg (=23)          test for int value w/o option\n  -s, --string arg             test for string values\n  -m, --implicit [=arg(=42)]   implicit test\n\nnon_option_args: test\nverbose_option  - is_set: 0, count: 0, reference: 0\nhidden_option   - is_set: 0, count: 0\ndouble_option   - is_set: 0, count: 0, value: 3.14159\nstring_option   - is_set: 1, count: 1\nstring_option #0 - value: hello\nfloat_option    - is_set: 0, value: 2.71828, reference: 2.71828\nint w/o option  - reference: 23\nint_option      - is_set: 0, value: 23, reference: 23\nimp_int_option  - is_set: 1, value: 23, reference: 23\nadvanced_option - is_set: 0, count: 0\nexpert_option   - is_set: 0, count: 0\n```\n","funding_links":[],"categories":["CLI"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbadaix%2Fpopl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbadaix%2Fpopl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbadaix%2Fpopl/lists"}