{"id":15049458,"url":"https://github.com/sir-ragna/flag-parser","last_synced_at":"2026-02-06T10:10:06.289Z","repository":{"id":150497935,"uuid":"420426107","full_name":"sir-ragna/flag-parser","owner":"sir-ragna","description":"flagparser.h inspired by the goflags package.","archived":false,"fork":false,"pushed_at":"2021-10-30T12:08:40.000Z","size":281,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-08T05:09:25.869Z","etag":null,"topics":["c","c89"],"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/sir-ragna.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":"2021-10-23T13:52:07.000Z","updated_at":"2023-05-16T12:00:30.000Z","dependencies_parsed_at":"2023-04-11T19:33:14.467Z","dependency_job_id":null,"html_url":"https://github.com/sir-ragna/flag-parser","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/sir-ragna/flag-parser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sir-ragna%2Fflag-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sir-ragna%2Fflag-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sir-ragna%2Fflag-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sir-ragna%2Fflag-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sir-ragna","download_url":"https://codeload.github.com/sir-ragna/flag-parser/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sir-ragna%2Fflag-parser/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265155673,"owners_count":23719561,"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","c89"],"created_at":"2024-09-24T21:20:34.260Z","updated_at":"2026-02-06T10:10:01.234Z","avatar_url":"https://github.com/sir-ragna.png","language":"C","readme":"\n[![.github/workflows/codeql-analysis.yml](https://github.com/sir-ragna/flag-parser/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/sir-ragna/flag-parser/actions/workflows/codeql-analysis.yml)\n\n# flagparser\n\nSomewhat inspired by the golang flags package. \nThe idea is to have a minimal amount of effort to define cmdline \narguments. Parsing is taken care of and some minimal usage is \ngenerated.\n\nSteps.\n\n- Define the cmdline args\n- [optionally] define a rest collection\n- Parse the flags. This will populate the pointers.\n\n\n# Example code\n\nBelow is an example definition of each type of variable.\n\n```c\n\n#include \"flagparser.h\"\n#include \"stdio.h\"\n\nint main(int argc, const char *argv[])\n{\n    /* Example of an integer */\n    int *amount = flg_int_arg(\n        \"-a\",                /* short flag format */\n        \"--amount\",          /* long flag format */\n        1,                   /* default value */\n        \"Specify the amount\" /* Help string */\n    );\n\n    /* Example of a boolean argument, also called a switch\n     * The defaul value of switches is always false */\n    bool *keep = flg_bool_arg(\n        \"-k\",             /* short flag format */\n        \"--keep\",         /* long flag format */\n        \"Keep temp files\" /* help string */\n    );\n\n    /* Example a string argument */\n    char **dns = flg_string_arg(\n        \"-d\",                           /* short flag format */\n        \"--dns\",                        /* long flag format */\n        \"1.1.1.1\",                      /* default value */\n        \"Specify the target DNS server\" /* help string */\n    );\n    \n    /* You can optionally define a 'rest collection'.\n     * This will help the documentation string to name what you\n     * expect to be present after the [OPTIONS]. */\n    flg_define_rest_collection(\n        \"FILE\",            /* Name of the collection.\n                            * ./example [OPTIONS] [FILE]... */\n        0,                 /* minimum amount of args required */\n        \"Files to process\" /* Help string */\n    );\n\n    /* flg_parse_flags will populate the pointers \n     * It will also return an offset you can use to know when the \n     * [OPTIONS] and where the 'rest collection' starts. */\n    int offset = flg_parse_flags(argc, argv);\n\n    /* Optional demonstration on how to deal with the non-flag strings\n     * arguments. The _offset_ variable allows us to iterate over them.\n     */\n    int i;\n    for (i = offset; i \u003c argc; i++)\n    {\n        printf(\"Rest ARG: %i \\\"%s\\\"\\n\", i - offset, argv[i]);\n    }\n    \n    /* Demonstration on flag usage */\n    if (*keep)\n        puts(\"Keep is enabled\");\n    else\n        puts(\"Keep is not enabled\");\n    \n    printf(\"DNS server: \\\"%s\\\"\\n\", *dns);\n    printf(\"Amount: %d\\n\", *amount);\n\n    /* Clean-up */\n    free(amount);\n    free(keep);\n    free(*dns);\n    free(dns);\n\n    return 0;\n}\n```\n\n## Built-in `--help`\n\nThe `--help` flag is built-in and will list the cmdline \narguments when called together with their help string.\n\n![built-in help example](images/example-help.png)\n\n\n# Code guidelines\n\n- Compiles under **c89** and **C++17**. (run `make test`)\n- Identifiers use snake case\n- Uses the prefix `flg_` for public identifiers\n- Uses the prefix `_flg_` for private identifiers\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsir-ragna%2Fflag-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsir-ragna%2Fflag-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsir-ragna%2Fflag-parser/lists"}