{"id":13418085,"url":"https://github.com/jarro2783/cxxopts","last_synced_at":"2025-05-13T23:07:05.719Z","repository":{"id":21429555,"uuid":"24747723","full_name":"jarro2783/cxxopts","owner":"jarro2783","description":"Lightweight C++ command line option parser","archived":false,"fork":false,"pushed_at":"2025-03-20T16:31:26.000Z","size":992,"stargazers_count":4394,"open_issues_count":91,"forks_count":603,"subscribers_count":60,"default_branch":"master","last_synced_at":"2025-04-13T17:46:35.744Z","etag":null,"topics":["c-plus-plus","option-parser","positional-arguments"],"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/jarro2783.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null}},"created_at":"2014-10-03T05:21:29.000Z","updated_at":"2025-04-13T12:26:01.000Z","dependencies_parsed_at":"2023-01-13T21:28:21.158Z","dependency_job_id":"6373afc1-752f-4408-9b73-d24876dfbb66","html_url":"https://github.com/jarro2783/cxxopts","commit_stats":{"total_commits":373,"total_committers":79,"mean_commits":"4.7215189873417724","dds":0.4879356568364611,"last_synced_commit":"16cc254919ba78767cf2644b80693b11e379f9f2"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jarro2783%2Fcxxopts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jarro2783%2Fcxxopts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jarro2783%2Fcxxopts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jarro2783%2Fcxxopts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jarro2783","download_url":"https://codeload.github.com/jarro2783/cxxopts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254041078,"owners_count":22004660,"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","option-parser","positional-arguments"],"created_at":"2024-07-30T22:00:58.352Z","updated_at":"2025-05-13T23:07:00.703Z","avatar_url":"https://github.com/jarro2783.png","language":"C++","funding_links":[],"categories":["TODO scan for Android support in followings","CLI","C++","Uncategorized","Libraries \u0026 Frameworks:","Argument Parsers","Libraries","C/C++ ⚙️","[C++](https://www.cpp-lang.net/)","C/C++程序设计"],"sub_categories":["Uncategorized","Other","Misc","资源传输下载"],"readme":"[![Build Status](https://travis-ci.org/jarro2783/cxxopts.svg?branch=master)](https://travis-ci.org/jarro2783/cxxopts)\n\n# Release versions\n\nNote that `master` is generally a work in progress, and you probably want to use a\ntagged release version.\n\n## Version 3 breaking changes\n\nIf you have used version 2, there are a couple of breaking changes in version 3\nthat you should be aware of. If you are new to `cxxopts` you can skip this\nsection.\n\nThe parser no longer modifies its arguments, so you can pass a const `argc` and\n`argv` and expect them not to be changed.\n\nThe `ParseResult` object no longer depends on the parser. So it can be returned\nfrom a scope outside the parser and still work. Now that the inputs are not\nmodified, `ParseResult` stores a list of the unmatched arguments. These are\nretrieved like follows:\n\n```cpp\nauto result = options.parse(argc, argv);\nresult.unmatched(); // get the unmatched arguments\n```\n\n# Quick start\n\nThis is a lightweight C++ option parser library, supporting the standard GNU\nstyle syntax for options.\n\nOptions can be given as:\n\n    --long\n    --long=argument\n    --long argument\n    -a\n    -ab\n    -abc argument\n\nwhere c takes an argument, but a and b do not.\n\nAdditionally, anything after `--` will be parsed as a positional argument.\n\n## Basics\n\n```cpp\n#include \u003ccxxopts.hpp\u003e\n```\n\nCreate a `cxxopts::Options` instance.\n\n```cpp\ncxxopts::Options options(\"MyProgram\", \"One line description of MyProgram\");\n```\n\nThen use `add_options`.\n\n```cpp\noptions.add_options()\n  (\"d,debug\", \"Enable debugging\") // a bool parameter\n  (\"i,integer\", \"Int param\", cxxopts::value\u003cint\u003e())\n  (\"f,file\", \"File name\", cxxopts::value\u003cstd::string\u003e())\n  (\"v,verbose\", \"Verbose output\", cxxopts::value\u003cbool\u003e()-\u003edefault_value(\"false\"))\n  ;\n```\n\nOptions are declared with a long and an optional short option. A description\nmust be provided. The third argument is the value, if omitted it is boolean.\nAny type can be given as long as it can be parsed, with operator\u003e\u003e.\n\nTo parse the command line do:\n\n```cpp\nauto result = options.parse(argc, argv);\n```\n\nTo retrieve an option use `result.count(\"option\")` to get the number of times\nit appeared, and\n\n```cpp\nresult[\"opt\"].as\u003ctype\u003e()\n```\n\nto get its value. If \"opt\" doesn't exist, or isn't of the right type, then an\nexception will be thrown.\n\n## Unrecognised arguments\n\nYou can allow unrecognised arguments to be skipped. This applies to both\npositional arguments that are not parsed into another option, and `--`\narguments that do not match an argument that you specify. This is done by\ncalling:\n\n```cpp\noptions.allow_unrecognised_options();\n```\n\nand in the result object they are retrieved with:\n\n```cpp\nresult.unmatched()\n```\n\n## Exceptions\n\nExceptional situations throw C++ exceptions. There are two types of\nexceptions: errors defining the options, and errors when parsing a list of\narguments. All exceptions derive from `cxxopts::exceptions::exception`. Errors\ndefining options derive from `cxxopts::exceptions::specification` and errors\nparsing arguments derive from `cxxopts::exceptions::parsing`.\n\nAll exceptions define a `what()` function to get a printable string\nexplaining the error.\n\n## Help groups\n\nOptions can be placed into groups for the purposes of displaying help messages.\nTo place options in a group, pass the group as a string to `add_options`. Then,\nwhen displaying the help, pass the groups that you would like displayed as a\nvector to the `help` function.\n\n## Positional Arguments\n\nPositional arguments are those given without a preceding flag and can be used\nalongside non-positional arguments. There may be multiple positional arguments,\nand the final positional argument may be a container type to hold a list of all\nremaining positionals.\n\nTo set up positional arguments, first declare the options, then configure a\nset of those arguments as positional like:\n\n```cpp\noptions.add_options()\n  (\"script\", \"The script file to execute\", cxxopts::value\u003cstd::string\u003e())\n  (\"server\", \"The server to execute on\", cxxopts::value\u003cstd::string\u003e())\n  (\"filenames\", \"The filename(s) to process\", cxxopts::value\u003cstd::vector\u003cstd::string\u003e\u003e());\n\noptions.parse_positional({\"script\", \"server\", \"filenames\"});\n\n// Parse options the usual way\noptions.parse(argc, argv);\n```\n\nFor example, parsing the following arguments:\n~~~\nmy_script.py my_server.com file1.txt file2.txt file3.txt\n~~~\nwill result in parsed arguments like the following table:\n\n| Field         | Value                                     |\n| ------------- | ----------------------------------------- |\n| `\"script\"`    | `\"my_script.py\"`                          |\n| `\"server\"`    | `\"my_server.com\"`                         |\n| `\"filenames\"` | `{\"file1.txt\", \"file2.txt\", \"file3.txt\"}` |\n\n## Default and implicit values\n\nAn option can be declared with a default or an implicit value, or both.\n\nA default value is the value that an option takes when it is not specified\non the command line. The following specifies a default value for an option:\n\n```cpp\ncxxopts::value\u003cstd::string\u003e()-\u003edefault_value(\"value\")\n```\n\nAn implicit value is the value that an option takes when it is given on the\ncommand line without an argument. The following specifies an implicit value:\n\n```cpp\ncxxopts::value\u003cstd::string\u003e()-\u003eimplicit_value(\"implicit\")\n```\n\nIf an option had both, then not specifying it would give the value `\"value\"`,\nwriting it on the command line as `--option` would give the value `\"implicit\"`,\nand writing `--option=another` would give it the value `\"another\"`.\n\nNote that the default and implicit value is always stored as a string,\nregardless of the type that you want to store it in. It will be parsed as\nthough it was given on the command line.\n\nDefault values are not counted by `Options::count`.\n\n## Boolean values\n\nBoolean options have a default implicit value of `\"true\"`, which can be\noverridden. The effect is that writing `-o` by itself will set option `o` to\n`true`. However, they can also be written with various strings using `=value`.\nThere is no way to disambiguate positional arguments from the value following\na boolean, so we have chosen that they will be positional arguments, and\ntherefore, `-o false` does not work.\n\n## `std::vector\u003cT\u003e` values\n\nParsing a list of values into a `std::vector\u003cT\u003e` is also supported, as long as `T`\ncan be parsed. To separate single values in a list the define symbol `CXXOPTS_VECTOR_DELIMITER`\nis used, which is ',' by default. Ensure that you use no whitespaces between values because\nthose would be interpreted as the next command line option. Example for a command line option\nthat can be parsed as a `std::vector\u003cdouble\u003e`:\n\n~~~\n--my_list=1,-2.1,3,4.5\n~~~\n\n## Options specified multiple times\n\nThe same option can be specified several times, with different arguments, which will all\nbe recorded in order of appearance. An example:\n\n~~~\n--use train --use bus --use ferry\n~~~\n\nthis is supported through the use of a vector of value for the option:\n\n~~~\noptions.add_options()\n  (\"use\", \"Usable means of transport\", cxxopts::value\u003cstd::vector\u003cstd::string\u003e\u003e())\n~~~\n\n## Custom help\n\nThe string after the program name on the first line of the help can be\ncompletely replaced by calling `options.custom_help`. Note that you might\nalso want to override the positional help by calling `options.positional_help`.\n\n\n## Example\n\nPutting all together:\n```cpp\nint main(int argc, char** argv)\n{\n    cxxopts::Options options(\"test\", \"A brief description\");\n\n    options.add_options()\n        (\"b,bar\", \"Param bar\", cxxopts::value\u003cstd::string\u003e())\n        (\"d,debug\", \"Enable debugging\", cxxopts::value\u003cbool\u003e()-\u003edefault_value(\"false\"))\n        (\"f,foo\", \"Param foo\", cxxopts::value\u003cint\u003e()-\u003edefault_value(\"10\"))\n        (\"h,help\", \"Print usage\")\n    ;\n\n    auto result = options.parse(argc, argv);\n\n    if (result.count(\"help\"))\n    {\n      std::cout \u003c\u003c options.help() \u003c\u003c std::endl;\n      exit(0);\n    }\n    bool debug = result[\"debug\"].as\u003cbool\u003e();\n    std::string bar;\n    if (result.count(\"bar\"))\n      bar = result[\"bar\"].as\u003cstd::string\u003e();\n    int foo = result[\"foo\"].as\u003cint\u003e();\n\n    return 0;\n}\n```\n\n# Linking\n\nThis is a header only library.\n\n# Requirements\n\nThe only build requirement is a C++ compiler that supports C++11 features such as:\n\n* regex\n* constexpr\n* default constructors\n\nGCC \u003e= 4.9 or clang \u003e= 3.1 with libc++ are known to work.\n\nThe following compilers are known not to work:\n\n* MSVC 2013\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjarro2783%2Fcxxopts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjarro2783%2Fcxxopts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjarro2783%2Fcxxopts/lists"}