{"id":29585749,"url":"https://github.com/stdbug/argparse","last_synced_at":"2025-07-20T02:30:19.722Z","repository":{"id":45320942,"uuid":"370133967","full_name":"stdbug/argparse","owner":"stdbug","description":"Single-header C++ command line argument parser","archived":false,"fork":false,"pushed_at":"2021-12-27T01:26:47.000Z","size":74,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-20T00:36:05.014Z","etag":null,"topics":["argument-parser","c-plus-plus","c-plus-plus-17","cpp","cpp17","option-parser"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"0bsd","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stdbug.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":"2021-05-23T18:55:20.000Z","updated_at":"2025-05-15T09:14:03.000Z","dependencies_parsed_at":"2022-09-01T19:13:20.520Z","dependency_job_id":null,"html_url":"https://github.com/stdbug/argparse","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/stdbug/argparse","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stdbug%2Fargparse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stdbug%2Fargparse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stdbug%2Fargparse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stdbug%2Fargparse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stdbug","download_url":"https://codeload.github.com/stdbug/argparse/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stdbug%2Fargparse/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266058459,"owners_count":23870154,"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","c-plus-plus","c-plus-plus-17","cpp","cpp17","option-parser"],"created_at":"2025-07-20T02:30:16.577Z","updated_at":"2025-07-20T02:30:19.657Z","avatar_url":"https://github.com/stdbug.png","language":"C++","readme":"# argparse: single-header C++ command line argument parser library\n## Basic usage\nInclude the header\n```cpp\n#include \u003cargparse.h\u003e\n```\n\nDeclare the parser\n```cpp\nargparse::Parser parser;\n```\n\nSet arguments that may appear in the command line. Each `Add*` method will\nreturn a holder that can be used to access argument values after they are parsed\n```cpp\n// flag, set by mentioning it among command line arguments\n// (\"--flag\" or \"-f\")\n// can be mentioned multiple times (e.g. \"-fff\")\nauto flag = parser.AddFlag(\"flag\", 'f', \"Optional help string\");\n\n// integer value passed by one of these ways:\n// --integer 42\n// --integer=42\n// -i 42\n// -i42\nauto integer = parser.AddArg\u003cint\u003e(\"integer\", 'i', \"Some integer\");\n\n// argument that may be passed multiple times, e.g.\n// --double 3.14 --double 2.71 --double 1 --double=0,1,2\nauto doubles = parser.AddMultiArg\u003cdouble\u003e(\"double\", 'd');\n\n// string as-is, but without a short option\nauto string = parser.AddArg\u003cstd::string\u003e(\"string\");\n```\n\nParse arguments\n```cpp\nparser.ParseArgs(argc, argv);\n```\n\nAccess parsed arguments via `operator*`\n\nNo check is required for flags as they only indicate the presence of the flag\nmarker in the command line\n```cpp\nif (*flag) {\n  std::cout \u003c\u003c \"flag was set \" \u003c\u003c *flag \u003c\u003c \" times\\n\";\n} else {\n  std::cout \u003c\u003c \"flag wasn't set\\n\";\n}\n```\n\nFor non-flag arguments first check if it was set (unless a default value is set\nor the argument is marked as required, see\n[below](#default-and-required-values))\n```cpp\nif (integer) {\n  std::cout \u003c\u003c \"integer was set equal to \" \u003c\u003c *integer \u003c\u003c \"\\n\";\n} else {\n  std::cout \u003c\u003c \"integer wasn't set\\n\";\n}\n```\n\nAccess individual multiarg entries using range-based for loop or `operator-\u003e`\n```cpp\nstd::cout \u003c\u003c \"doubles: \"\nfor (double x : *doubles) {\n  std::cout \u003c\u003c x \u003c\u003c \"\\n\";\n}\nstd::cout \u003c\u003c \"\\n\"\n\nstd::cout \u003c\u003c \"doubles were set \" \u003c\u003c doubles-\u003esize() \u003c\u003c \" times:\\n\";\nfor (size_t i = 0; i \u003c doubles-\u003esize(); i++) {\n  std::cout \u003c\u003c \"doubles[\" \u003c\u003c i \u003c\u003c \"] = \" \u003c\u003c doubles-\u003eat(i) \u003c\u003c \"\\n\";\n}\n```\n\n## Default and required values\nNon-flag arguments can be marked as required or have a default value\n```cpp\nauto integer_with_default = parser.AddArg\u003cint\u003e(\"integer\").Default(42);\nauto required_double = parser.AddArg\u003cdouble\u003e(\"double\").Required();\n```\n\nIn both of these cases there is no need to check for argument presence. Any\nargument with a default value will hold it even when it's not mentioned among\n`argv`. If no value is provided for a required argument, then `ParseArgs` will\nthrow an exception\n```cpp\nstd::cout \u003c\u003c \"integer = \" \u003c\u003c *integer_with_default \u003c\u003c \"\\n\";\nstd::cout \u003c\u003c \"double = \" \u003c\u003c *required_double \u003c\u003c \"\\n\";\n```\n\n## Acceptable options for arguments\nA set of valid values can be provided for arguments\n```cpp\nparser.AddArg\u003cint\u003e(\"integer\").Options({0, 42, 256});\n```\n\n### Misusage\nMake sure that provided options are compatible. Examples of incompatible\noptions that will result in an exception:\n```cpp\n// argument with a default value can't be required\n// (why would you set a default value then?)\nparser.AddArg\u003cint\u003e(\"integer\").Default(42).Required();\n\n// same\nparser.AddArg\u003cint\u003e(\"integer\").Required().Default(42);\n```\n\n## Custom types\nBy default, `argparse::Parser` supports parsing built-in numeric types (`float`,\n`double`, `long double`, `short`, `int`, `long`, `long long` and their unsigned\nversions), `bool` and `std::string`. To be able to parse other types one can use\nC++ operators or define a specialization of `argparse::TypeTraits` template:\n```cpp\n\nstd::istream\u0026 operator\u003e\u003e(std::istream\u0026 stream, MyType\u0026 variable) {\n  // implementation\n}\n\n// Required only when using Options or Default\nstd::ostream\u0026 operator\u003c\u003c(std::ostream\u0026 stream, MyType\u0026 variable) {\n  // implementation\n}\n\n// Required only when using Options\nbool operator==(const MyType\u0026 variable1, const MyType\u0026 variable2) {\n  // implementation\n}\n\nnamespace argparse {\n\n// TypeTraits specialization will have higher priority over \u003e\u003e and == operators\ntemplate \u003c\u003e\nclass TypeTraits\u003cMyType\u003e {\npublic:\n  static MyType FromString(const std::string\u0026 str) {\n    // implementation\n  }\n\n  // Required only when using Options or Default\n  static std::string ToString(const MyType\u0026 my_var) {\n    // implementation\n  }\n\n  // Required only when using Options\n  static bool Equal(const MyType\u0026 variable1, const MyType\u0026 variable2) {\n    // implementation\n  }\n};\n\n}  // namespace argparse\n\n\nint main(int argc, char* argv[]) {\n  argparse::Parser parser;\n  auto my_var = parser.AddArg\u003cMyType\u003e(\"my-var\").Default(MyType()).Options({MyType(1), MyType(2)});\n  parser.Parse(argc, argv);\n}\n```\n\nA program using `argparse::Parser` with custom types having neither required\noperators nor TypeTraits specialization will fail to compile\n\n## Errors\nAll parsing errors will result in throwing `argparse::ArgparseError`. State of\nthe parser and holders (including globals) in this case is undefined.\nOptionally, parser can be set to exit the program (via `exit` function)\n```\nparser.ExitOnFailure(exit_code, optional_usage_string);\n```\n\n## TODO\n* `--help` option\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstdbug%2Fargparse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstdbug%2Fargparse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstdbug%2Fargparse/lists"}