{"id":24227552,"url":"https://github.com/pollyren/argparse","last_synced_at":"2025-09-22T18:31:52.432Z","repository":{"id":272102169,"uuid":"913985495","full_name":"pollyren/argparse","owner":"pollyren","description":"A simple command-line argument parser in C","archived":false,"fork":false,"pushed_at":"2025-01-12T06:05:59.000Z","size":17,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-12T06:20:23.485Z","etag":null,"topics":["argument-parsing","c","cli"],"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/pollyren.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":"2025-01-08T18:18:25.000Z","updated_at":"2025-01-12T06:06:02.000Z","dependencies_parsed_at":"2025-01-12T06:30:39.232Z","dependency_job_id":null,"html_url":"https://github.com/pollyren/argparse","commit_stats":null,"previous_names":["pollyren/argparse"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pollyren%2Fargparse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pollyren%2Fargparse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pollyren%2Fargparse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pollyren%2Fargparse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pollyren","download_url":"https://codeload.github.com/pollyren/argparse/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233876725,"owners_count":18744248,"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-parsing","c","cli"],"created_at":"2025-01-14T10:18:13.096Z","updated_at":"2025-09-22T18:31:52.033Z","avatar_url":"https://github.com/pollyren.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# argparse\n\n### Overview\n`argparse` is a simple and flexible C library for parsing command-line options and arguments. Largely inspired by Python's `argparse` module, it simplifies the process of handling command-line inputs by providing macros and functions to define, parse and validate arguments with ease.\n\n### Features\n- Handles integer, float, boolean and string argument types\n- Support for positional arguments and options\n- Supports flag-based, long-form and grouped short options\n- Provides easy-to-use macros for defining arguments\n- Built-in `-h`/`--help` options that automatically generates and prints a customisable usage message\n- Ability to enforce required arguments\n- Enables predefined choices for arguments to restrict values to a specified set\n- Validates arguments and returns errors for invalid arguments\n- Compatible with C++\n\n### Usage\n\n#### Initialising parser\nThe `argparse` library is built around the `argument_parser_t` structure, which serves as a container for argument specifications and manages parsing arguments and options:\n```\nargument_parser_t parser;\nargparse_init(\u0026parser, argc, argv, \"Program description\", \"Epilog text\");\n```\n\n#### Creating arguments\n`argparse` provides a set of convenient macros to simplify the creation of arguments. These macros define positional arguments, options that accept values, boolean flags and count actions:\n```\nint verbosity;\nfloat distance;\nconst char *route_file;\nbool reroute;\n\nargparse_arg_t arg1 = ARGPARSE_COUNT(\n    'v', \"--verbose\", \u0026verbosity, \"verbosity level\"\n);  // count action\n\nargparse_arg_t arg2 = ARGPARSE_OPTION(\n    FLOAT, 'd', \"--distance\", \u0026distance, \"maximum distance for the trip\"\n);  // option that takes float value\n\nargparse_arg_t arg3 = ARGPARSE_TOGGLE(\n    'r', \"--reroute\", \u0026reroute, \"whether the trip should be rerouted\"\n);  // boolean toggle\n\nargparse_arg_t arg4 = ARGPARSE_POSITIONAL(\n    STRING, \"--file\", \u0026route_file, \"name of file containing routes\"\n);  // positional argument\n```\n\n#### Adding arguments\nAfter the arguments are created, the `argparse_add_argument` function adds individual argument specifications to the parser:\n\n```\nargparse_add_argument(\u0026parser, \u0026arg1);\nargparse_add_argument(\u0026parser, \u0026arg2);\nargparse_add_argument(\u0026parser, \u0026arg3);\nargparse_add_argument(\u0026parser, \u0026arg4);\n```\n\nThe `argparse_add_arguments` function can be used to combine the addition of multiple arguments:\n```\nargparse_arg_t args[] = {\n    ARGPARSE_COUNT('v', \"--verbose\", \u0026verbosity, \"verbosity level\"),\n    ARGPARSE_OPTION(FLOAT, 'd', \"--distance\", \u0026distance, \"maximum distance for the trip\"),\n    ARGPARSE_TOGGLE('r', \"--reroute\", \u0026reroute, \"whether the trip should be rerouted\"),\n    ARGPARSE_POSITIONAL(STRING, \"--file\", \u0026route_file, \"name of file containing routes\")\n};\nargparse_add_arguments(\u0026parser, args, 4);\n```\n\n#### Parsing arguments\nThe `argparse_parse_args` function runs the parser and updates the respective variables with the extracted data:\n```\nargparse_parse_args(\u0026parser);\n```\n\n#### Displaying usage message\nThe `-h` and `--help` options are built-in; when either option is encountered during parsing, a usage message is automatically generated and printed. This usage message provides information on all the defined options and positional arguments, their flags and names, a brief user-provided description, whether the argument is required, and the valid choices for the argument. The `argparse_print_help` function also displays the usage message without needing to parse any `-h` or `--help` arguments:\n```\nargparse_print_help(\u0026parser);\n```\n\n#### Running tests\nThe implementation tests are written using the Criterion library. In the `tests/` directory, the tests can be run using `make test_argparse \u0026\u0026 ./test_argparse`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpollyren%2Fargparse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpollyren%2Fargparse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpollyren%2Fargparse/lists"}