{"id":16692655,"url":"https://github.com/platisd/cpp-command-parser","last_synced_at":"2025-07-19T08:33:39.122Z","repository":{"id":65438854,"uuid":"566910115","full_name":"platisd/cpp-command-parser","owner":"platisd","description":"Parse CLI commands with compile-time checks for your sanity","archived":false,"fork":false,"pushed_at":"2023-10-27T12:54:09.000Z","size":1083,"stargazers_count":1,"open_issues_count":4,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-13T22:41:39.366Z","etag":null,"topics":["c-plus-plus","cli","cpp","parser"],"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/platisd.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,"publiccode":null,"codemeta":null}},"created_at":"2022-11-16T17:10:02.000Z","updated_at":"2024-06-29T09:17:10.000Z","dependencies_parsed_at":"2023-02-13T00:46:34.724Z","dependency_job_id":"a1187396-b666-40a6-989d-71a2ffc39456","html_url":"https://github.com/platisd/cpp-command-parser","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/platisd/cpp-command-parser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platisd%2Fcpp-command-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platisd%2Fcpp-command-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platisd%2Fcpp-command-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platisd%2Fcpp-command-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/platisd","download_url":"https://codeload.github.com/platisd/cpp-command-parser/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/platisd%2Fcpp-command-parser/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265905093,"owners_count":23846695,"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":["c-plus-plus","cli","cpp","parser"],"created_at":"2024-10-12T16:28:01.097Z","updated_at":"2025-07-19T08:33:39.098Z","avatar_url":"https://github.com/platisd.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Command Parser\n\n[![codecov](https://codecov.io/gh/platisd/cpp-command-parser/branch/main/graph/badge.svg?token=MNGCSVLIUM)](https://codecov.io/gh/platisd/cpp-command-parser) [![Build, unit tests and coverage CI](https://github.com/platisd/cpp-command-parser/actions/workflows/build-ut-coverage.yml/badge.svg)](https://github.com/platisd/cpp-command-parser/actions/workflows/build-ut-coverage.yml) [![clang-format CI](https://github.com/platisd/cpp-command-parser/actions/workflows/clang-format.yml/badge.svg)](https://github.com/platisd/cpp-command-parser/actions/workflows/clang-format.yml) [![clang-tidy CI](https://github.com/platisd/cpp-command-parser/actions/workflows/clang-tidy.yml/badge.svg)](https://github.com/platisd/cpp-command-parser/actions/workflows/clang-tidy.yml) [![Commit messages](https://github.com/platisd/cpp-command-parser/actions/workflows/commit-messages.yml/badge.svg)](https://github.com/platisd/cpp-command-parser/actions/workflows/commit-messages.yml)\n\nCommand Parser is a C++17 header-only utility library for parsing command line \"commands\".\n\nWe define a _command_ as the _first_ CLI argument passed to a binary in the form of:\n\n```bash\n./your_binary command_name \u003carg1\u003e \u003carg2\u003e [arg3]\n# `command_name` is the command\n```\n\nA command may optionally take some (boolean) options\n\n```bash\n./your_binary command_name --option1 --option2 \u003carg1\u003e \u003carg2\u003e [arg3]\n```\n\nAs long as your CLI application requires the _first_ argument to be a \"command\", this library will help you parse the\ncommand, as well as any subcommands and options.\n\nTo put it simply, **there must be at least one argument**. That first argument to the binary is your \"command\".\nIf your logic does _not_ require at least one argument to be passed as a \"command\", then this library is not for you.\n\n## Usage\n\nSee [example_main.cpp](example_main.cpp) for a simple example of how to use the library. In a nutshell:\n\n```cpp\nconst auto all = UnparsedCommand::create(\"all\", \"Print current configuration\");\nconst auto get = UnparsedCommand::create(\"get\", \"Get configuration key\", \"[-xyz] \u003ckey\u003e [default]\")\n                         .withOptions({ \"x\", \"y\", \"z\" }) // Can be --x or -x etc\n                         .withAliases({ \"g\", \"get-key\" }); // Alternative IDs for the command instead of \"get\"\n                         .withArgs\u003cstd::string, std::optional\u003cstd::string\u003e\u003e();\nconst auto encrypt = UnparsedCommand::create(\n                         \"encrypt\"\n                         \"Encrypt the given files with the specified policy\",\n                         \"\u003cpolicy\u003e [file...]\")\n                         .withArgs\u003cstd::string, std::vector\u003cstd::string\u003e\u003e();\nconst std::tuple commands { all, get, encrypt };\nconst auto parsedCommand = UnparsedCommand::parse(argc, argv, commands);\n\nif (parsedCommand.is(all)) {\n    std::cout \u003c\u003c \"all\" \u003c\u003c std::endl;\n    // auto config = parsedCommand.getArgs(all); // Does not compile because all has no args\n} else if (parsedCommand.is(get)) {\n    const auto [key, defaultValue] = parsedCommand.getArgs(get);\n    const auto x = parsedCommand.hasOption(\"x\");\n    const auto y = parsedCommand.hasOption(\"y\");\n    const auto z = parsedCommand.hasOption(\"z\");\n    std::cout \u003c\u003c \"get \" \u003c\u003c key \u003c\u003c \" \" \u003c\u003c x \u003c\u003c \" \" \u003c\u003c y \u003c\u003c \" \" \u003c\u003c z \u003c\u003c std::endl;\n    if (defaultValue) {\n        std::cout \u003c\u003c \"default \" \u003c\u003c defaultValue.value() \u003c\u003c std::endl;\n    }\n} else if (parsedCommand.is(encrypt)) {\n    const auto [policy, files] = parsedCommand.getArgs(encrypt);\n    std::cout \u003c\u003c \"encrypt \" \u003c\u003c policy \u003c\u003c std::endl;\n    for (const auto\u0026 file : files) {\n        std::cout \u003c\u003c \"file \" \u003c\u003c file \u003c\u003c std::endl;\n    }\n} else {\n        std::cout \u003c\u003c \"Available commands:\" \u003c\u003c std::endl;\n        std::cout \u003c\u003c parsedCommand.help() \u003c\u003c std::endl;\n}\n```\n\n### Allowed types\n\nThe following types are permitted as arguments. They are mandatory unless otherwise specified and their usage rules are\nenforced during compilation:\n\n* `std::string`\n* `bool`\n    * The following (case-insensitive) strings are considered `true` and everything else `false`:\n        * `true`\n        * `yes`\n        * `y`\n        * `on`\n        * `1`\n* `int`\n* `long`\n* `long long`\n* `unsigned long`\n* `unsigned long long`\n* `float`\n* `double`\n* `long double`\n* `std::optional\u003cT\u003e` where `T` is any of the above types\n    * Optional argument: May not be provided but must be at the end of the argument list\n* `std::vector\u003cT\u003e` where `T` is any of the above types\n    * Zero or more optional arguments: May be of any number or not provided, but cannot precede a mandatory argument and\n      cannot be combined with a `std::optional` argument\n\nIn addition to the above, any user-defined type that is default constructible and constructible from a `std::string` is\nalso allowed.\n\n## Why not `insert your favorite CLI parsing library here`?\n\nIn all fairness, this library was created under the misconception that [cxxopts](https://github.com/jarro2783/cxxopts)\nwas not able to satisfy our use-case out of the box, which is **not** true. Some of its cool features are:\n\n* Compile-time check of argument count and types (i.e. you cannot \"forget\" to parse an argument or parse one that was\n  not expected)\n* Header-only\n* No dependencies\n* No macros\n* No exceptions\n\n## Acknowledgements\n\nThis library was developed internally at [neat.no](https://neat.no) and is now open-sourced.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplatisd%2Fcpp-command-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplatisd%2Fcpp-command-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplatisd%2Fcpp-command-parser/lists"}