{"id":13733807,"url":"https://github.com/jermp/cmd_line_parser","last_synced_at":"2025-12-24T17:59:37.363Z","repository":{"id":49282245,"uuid":"217627078","full_name":"jermp/cmd_line_parser","owner":"jermp","description":"Command line parser for C++17.","archived":false,"fork":false,"pushed_at":"2023-01-08T09:30:01.000Z","size":35,"stargazers_count":24,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-05-15T14:19:49.473Z","etag":null,"topics":["cmdline-parser","command-line-parser","option-parser"],"latest_commit_sha":null,"homepage":null,"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/jermp.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":"2019-10-25T23:08:25.000Z","updated_at":"2023-10-23T18:07:34.000Z","dependencies_parsed_at":"2023-02-08T05:31:03.990Z","dependency_job_id":null,"html_url":"https://github.com/jermp/cmd_line_parser","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jermp%2Fcmd_line_parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jermp%2Fcmd_line_parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jermp%2Fcmd_line_parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jermp%2Fcmd_line_parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jermp","download_url":"https://codeload.github.com/jermp/cmd_line_parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":213758655,"owners_count":15634354,"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":["cmdline-parser","command-line-parser","option-parser"],"created_at":"2024-08-03T03:00:49.277Z","updated_at":"2025-12-24T17:59:37.322Z","avatar_url":"https://github.com/jermp.png","language":"C++","readme":"[![CodeQL](https://github.com/jermp/cmd_line_parser/actions/workflows/codeql.yml/badge.svg)](https://github.com/jermp/cmd_line_parser/actions/workflows/codeql.yml)\n\nCommand Line Parser for C++17\n============================\n\nThis is a single-header command line parser for C++17.\n\nIt offers, basically, the same funcionalities as other popular libraries, such as [cxxopts](https://github.com/jarro2783/cxxopts),\n[cmdline](https://github.com/tanakh/cmdline) and\n[argparse](https://github.com/hbristow/argparse),\nbut relies on the powerful `if constexpr` construct of C++17 instead of dynamic casting.\nThis results in a very compact code (~150 sloc).\n\n### Integration\n\nJust add the file `include/parser.hpp` to your own project.\n\nOr if you use git:\n\n\tgit submodule add https://github.com/jermp/cmd_line_parser.git\n\n### Example\n\n```C++\n#include \u003ciostream\u003e\n\n#include \"../include/parser.hpp\"\n\nvoid configure(cmd_line_parser::parser\u0026 parser) {\n    parser.add(\"perc\",                 // name\n               \"A percentage value.\",  // description\n               \"-p\",                   // shorthand\n               true,                   // required argument\n               false                   // not boolean option (default is false)\n    );\n    parser.add(\"input_filename\", \"An input file name.\", \"-i\", true);\n\n    parser.add(\"output_filename\",       // name\n               \"An output file name.\",  // description\n               \"-o\",                    // shorthand\n               false, false);\n    parser.add(\"num_trials\", \"Number of trials.\", \"-n\", false, false);\n\n    parser.add(\"sorted\", \"Sort output.\", \"--sort\", false,\n               true  // boolean option: a value is not expected after the shorthand\n    );\n    parser.add(\"buffered\", \"Buffer input.\", \"--buffer\", false, true);\n\n    parser.add(\"ram\", \"Amount of ram to use.\", \"--ram\", false, false);\n}\n\nint main(int argc, char** argv) {\n    // declare the parser\n    cmd_line_parser::parser parser(argc, argv);\n    // configure the parser\n    configure(parser);\n\n    // parse command line and return on failure\n    bool success = parser.parse();\n    if (!success) return 1;\n\n    // now get some variables\n    auto perc = parser.get\u003cfloat\u003e(\"perc\");  // deduced type is float\n    auto input_filename =                   // deduced type is std::string\n        parser.get\u003cstd::string\u003e(\"input_filename\");\n    auto sorted_output = parser.get\u003cbool\u003e(\"sorted\");     // deduced type is bool\n    auto buffered_input = parser.get\u003cbool\u003e(\"buffered\");  // deduced type is bool\n\n    size_t ram = 999;  // some default value\n    if (parser.parsed(\"ram\")) {\n        ram = parser.get\u003csize_t\u003e(\"ram\");  // deduced type is size_t\n    }\n\n    std::cout \u003c\u003c \"perc: \" \u003c\u003c perc \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"input_filename: \" \u003c\u003c input_filename \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"sorted_output: \" \u003c\u003c sorted_output \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"buffered_input: \" \u003c\u003c buffered_input \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"ram: \" \u003c\u003c ram \u003c\u003c std::endl;\n\n    try {\n        auto val = parser.get\u003cint\u003e(\"bar\");  // fail: no name 'bar' was specified\n        (void)val;                          // shut up, compiler!\n    } catch (std::runtime_error const\u0026 e) {\n        std::cerr \u003c\u003c e.what() \u003c\u003c std::endl;\n        return 1;\n    }\n    return 0;\n}\n```\n\nTo compile and run the example, do as follows.\n\n\tcd src\n\tmkdir -p build\n\tcd build\n\tcmake ..\n\tmake\n\t./example\n\nAlso run `./test_parse` for some testing.\n\n\n### Interface\n\n```C++\n/* Constructor. */\nparser(int argc, char** argv);\n\n/* Parse command line; return false on failure. */\nbool parse();\n\n/* Print help message. */\nvoid help() const;\n\n/* Add an argument with a name, a description, and a shorthand.\n   Return false if an argument with the same name already exists.\n   The last two boolean parameters are used to specify if the argument\n   is to be required and to be considered a boolean argument (no value after\n   the shorthand). */\n bool add(std::string const\u0026 name, std::string const\u0026 descr,\n          std::string const\u0026 shorthand,\n          bool is_required, bool is_boolean = false);\n\n/* Return true is an option with the given name was parsed; false otherwise. */\nbool parsed(std::string const\u0026 name) const;\n\n/* Get a variable of type T by name.\n   It throws an exception if an argument with the specified name\n   is not found. */\ntemplate \u003ctypename T\u003e\nT get(std::string const\u0026 name) const;\n```\n","funding_links":[],"categories":["Argument Parsers"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjermp%2Fcmd_line_parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjermp%2Fcmd_line_parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjermp%2Fcmd_line_parser/lists"}