{"id":17724765,"url":"https://github.com/jibsen/parg","last_synced_at":"2025-08-20T12:30:57.494Z","repository":{"id":52448902,"uuid":"41554214","full_name":"jibsen/parg","owner":"jibsen","description":"Parser for argv that works similarly to getopt","archived":false,"fork":false,"pushed_at":"2023-10-31T12:22:24.000Z","size":55,"stargazers_count":178,"open_issues_count":0,"forks_count":17,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-07-11T14:43:43.861Z","etag":null,"topics":["c","getopt"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit-0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jibsen.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}},"created_at":"2015-08-28T15:18:42.000Z","updated_at":"2024-06-19T09:03:51.000Z","dependencies_parsed_at":"2024-01-16T23:30:43.606Z","dependency_job_id":"114bb093-a759-4e5c-b354-c9136be8e22d","html_url":"https://github.com/jibsen/parg","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jibsen%2Fparg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jibsen%2Fparg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jibsen%2Fparg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jibsen%2Fparg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jibsen","download_url":"https://codeload.github.com/jibsen/parg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230423559,"owners_count":18223435,"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","getopt"],"created_at":"2024-10-25T15:48:42.982Z","updated_at":"2024-12-19T11:11:26.279Z","avatar_url":"https://github.com/jibsen.png","language":"C","funding_links":[],"categories":["Utilities","公用事业"],"sub_categories":["YAML"],"readme":"\n[![parg CI](https://github.com/jibsen/parg/actions/workflows/parg-ci-workflow.yaml/badge.svg)](https://github.com/jibsen/parg/actions) [![codecov](https://codecov.io/gh/jibsen/parg/branch/master/graph/badge.svg)](https://codecov.io/gh/jibsen/parg)\n\nAbout\n-----\n\nMost command-line programs have to parse options, so there are a lot of\ndifferent solutions to this problem. Some offer many features, while others\nare more basic.\n\nOne of the simpler solutions for C is the [getopt][] function, and its\nextension `getopt_long`. They iterate over the options in `argv`, returning\nthem one at a time on successive calls.\n\nOne nice thing about them is that they are available on most Unix-like\noperating systems (and usually accompany GCC elsewhere, like Windows).\nUnfortunately, some implementation details vary between platforms.\n\nA potential question is what license the version you get when you include\nthem is available under. Some are GPL, others LGPL. There are also ports of\n`getopt` that use more liberal licenses.\n\n`parg` is a parser for `argv` that works similarly to `getopt`, but does not\naim to be a direct replacement. It attempts to make some choices about how to\nhandle the extensions and idiosyncrasies of other `getopt` implementations,\nand document them.\n\nIt consists of a single source and include file, written in portable ANSI C.\nIt is made available under the [MIT No Attribution License](LICENSE) (MIT-0).\n\n[getopt]: https://en.wikipedia.org/wiki/Getopt\n\n\nUsage\n-----\n\nThe include file `parg.h` contains documentation in the form of [doxygen][]\ncomments. A configuration file is included, run `doxygen` to generate\ndocumentation in HTML format.\n\nYou can add the source files `parg.c` and `parg.h` to your own projects.\n\nFor CI, `parg` uses [CMake][] to provide an easy way to build and test across\nvarious platforms and toolsets. To create a build system for the tools on your\nplatform, and build `parg`, use something along the lines of:\n\n~~~sh\nmkdir build\ncd build\ncmake ..\ncmake --build .\n~~~\n\n[doxygen]: http://www.doxygen.org/\n[CMake]: http://www.cmake.org/\n\n\nExample\n-------\n\nHere is an example that parses command-line options using `parg_getopt()`:\n\n~~~c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n\n#include \"parg.h\"\n\nint main(int argc, char *argv[])\n{\n\tstruct parg_state ps;\n\tint c;\n\n\tparg_init(\u0026ps);\n\n\twhile ((c = parg_getopt(\u0026ps, argc, argv, \"hs:v\")) != -1) {\n\t\tswitch (c) {\n\t\tcase 1:\n\t\t\tprintf(\"nonoption '%s'\\n\", ps.optarg);\n\t\t\tbreak;\n\t\tcase 'h':\n\t\t\tprintf(\"Usage: testparg [-h] [-v] [-s STRING]\\n\");\n\t\t\treturn EXIT_SUCCESS;\n\t\t\tbreak;\n\t\tcase 's':\n\t\t\tprintf(\"option -s with argument '%s'\\n\", ps.optarg);\n\t\t\tbreak;\n\t\tcase 'v':\n\t\t\tprintf(\"testparg 1.0.0\\n\");\n\t\t\treturn EXIT_SUCCESS;\n\t\t\tbreak;\n\t\tcase '?':\n\t\t\tif (ps.optopt == 's') {\n\t\t\t\tprintf(\"option -s requires an argument\\n\");\n\t\t\t}\n\t\t\telse {\n\t\t\t\tprintf(\"unknown option -%c\\n\", ps.optopt);\n\t\t\t}\n\t\t\treturn EXIT_FAILURE;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tprintf(\"error: unhandled option -%c\\n\", c);\n\t\t\treturn EXIT_FAILURE;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tfor (c = ps.optind; c \u003c argc; ++c) {\n\t\tprintf(\"nonoption '%s'\\n\", argv[c]);\n\t}\n\n\treturn EXIT_SUCCESS;\n}\n~~~\n\n\nComparison to `getopt`\n----------------------\n\n### Use of global variables\n\n`getopt` uses global variables to store its state between calls. `parg` uses\na struct `parg_state`, which you must pass with each call.\n\n### Handling of nonoptions\n\nPOSIX and BSD `getopt` return `-1` on the first nonoption argument. GNU\n`getopt` by default reorders `argv` (even though it is passed as const), so\nall options come first.\n\n`parg` does not change `argv`, and returns each nonoption as the option\nargument of an option with value `1` (like GNU `getopt`, if `optstring` were\nprefixed by '`-`').\n\nIf you wish to process all options first, and have the nonoptions ordered at\nthe end of `argv`, you can use `parg_reorder()`:\n\n~~~c\n\toptend = parg_reorder(argc, argv, optstring, NULL);\n\n\twhile ((c = parg_getopt(\u0026ps, optend, argv, optstring)) != -1) {\n\t\t/* ... */\n\t}\n\n\t/* elements of argv[] from optend to argc are nonoptions */\n~~~\n\n### Value of `optind` on error\n\nWhen there are multiple short options in one argument, `getopt` does not\nincrement `optind` until the last one is processed. This makes it harder to\ntell which argument an unknown option came from (if `a` is an unknown option,\n`-a` and `-ab` will return '`?`' with different values in `optind`).\n\n`parg` always increments the `optind` value in it's state so it points to the\nnext `argv` element to be processed. So when `parg` returns '`?`' (or '`:`'),\nthe element that contains the error is `argv[optind - 1]`.\n\n### Value of `optopt` on error\n\nWith `getopt_long`, it varies what the values of `optopt` and `longindex` are\nwhen an error is found with option arguments of long options. Sometimes these\nvalues are not documented.\n\n`parg` sets `optopt` to `val` if `flag` is `NULL`, and `0` otherwise (which\nequals the return value on successful match), and `longindex` is set to the\nindex of the entry in `longopts` that matched.\n\n### Return value on option argument error\n\nWhen the first character of `optstring` is '`:`', it varies what `getopt`\nreturns on extraneous option arguments.\n\nIn this case, `parg` returns '`?`' if no option match is found, and '`:`' if\na match is found, but is missing a required argument, or has an extraneous\nargument.\n\n\nAlternatives\n------------\n\nSome ports of `getopt`:\n\n  - [Free Getopt](http://freegetopt.sourceforge.net/)\n  - [ya_getopt](http://github.com/kubo/ya_getopt/)\n  - [getopt_port](http://github.com/kimgr/getopt_port/)\n\nOther command-line parsing libraries that support C:\n\n  - [Gengetopt](http://www.gnu.org/software/gengetopt/)\n  - [Argp](http://www.gnu.org/software/libc/manual/html_node/Argp.html)\n  - [popt](http://en.wikipedia.org/wiki/Popt)\n  - [argtable](https://www.argtable.org/)\n  - [optlist](http://michael.dipperstein.com/optlist/)\n  - [Arg_parser](http://www.nongnu.org/arg-parser/arg_parser.html)\n  - [Gopt](http://www.purposeful.co.uk/software/gopt/)\n  - [docopt](http://docopt.org/)\n  - [optparse](https://github.com/skeeto/optparse)\n  - [getopt](https://github.com/wc-duck/getopt)\n  - [argparse](https://github.com/cofyc/argparse)\n\nA few C++ command-line parsing libraries:\n\n  - [TCLAP](http://tclap.sourceforge.net/)\n  - [program_options](http://www.boost.org/doc/libs/1_58_0/doc/html/program_options.html)\n  - [CommandLine](http://llvm.org/docs/CommandLine.html)\n  - [CLI11](https://github.com/CLIUtils/CLI11)\n  - [argparse](https://github.com/p-ranav/argparse)\n  - [clipp](https://github.com/muellan/clipp)\n  - [argh](https://github.com/adishavit/argh)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjibsen%2Fparg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjibsen%2Fparg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjibsen%2Fparg/lists"}