{"id":24091713,"url":"https://github.com/RT-Thread-packages/optparse","last_synced_at":"2025-09-17T01:31:51.432Z","repository":{"id":234606716,"uuid":"789231717","full_name":"RT-Thread-packages/optparse","owner":"RT-Thread-packages","description":"getopt-like command-line parameter parser for RT-Thread","archived":false,"fork":false,"pushed_at":"2024-04-20T15:42:38.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-21T02:35:48.637Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RT-Thread-packages.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2024-04-20T01:38:02.000Z","updated_at":"2024-04-20T15:42:42.000Z","dependencies_parsed_at":"2024-04-20T02:33:07.088Z","dependency_job_id":"dbb4b23d-a87e-4988-8242-8b5058292d34","html_url":"https://github.com/RT-Thread-packages/optparse","commit_stats":null,"previous_names":["rt-thread-packages/optparse"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RT-Thread-packages%2Foptparse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RT-Thread-packages%2Foptparse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RT-Thread-packages%2Foptparse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RT-Thread-packages%2Foptparse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RT-Thread-packages","download_url":"https://codeload.github.com/RT-Thread-packages/optparse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233312692,"owners_count":18657089,"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":[],"created_at":"2025-01-10T07:44:40.662Z","updated_at":"2025-09-17T01:31:46.155Z","avatar_url":"https://github.com/RT-Thread-packages.png","language":"C","readme":"# optparse #\n\noptparse是一个开源, 可移植的, 可重入的和可嵌入的类getopt命令行参数解析器. 它支持POSIX getopt选项字符串, GNU风格的长参数解析, 参数置换和子命令处理.\n\n## 1. 为什么不在RTT下直接使用getopt函数?\n\nPOSIX getopt解析器有三个致命的缺陷. 这些缺陷都是可以通过optparse来解决的:\n\n1. getopt的解析状态存储在全局变量中, 其中一些变量是静态不可访问的. 这意味着在RTT(RT-Thread)下只能有一个msh命令可以使用getopt, 其他msh命令再次使用getopt会出现bug. 而optparse将每次解析的状态存储在一个属于msh的私有结构体中, 每一个optparse命令解析器不会相互影响.\n\n2. 在RTT中BSP一般支持Keil, IAR和GCC, 对于keil环境下是无法支持原生的POSIX getopt解析器, 这样就造成了在RTT下实现命令解析是无法移植到Keil中使用的, 代码的兼容性降低!\n\n3. 在getopt中, 错误消息被打印到stderr. 使用opterr可以禁用此功能, 但消息本身仍然不可访问. optparse通过将错误消息写入它的errmsg字段来解决这个问题, 该字段可以被打印到任何地方. optparse的缺点是, 这个错误消息总是用英语而不是当前语言环境.\n\n## 2. 命令顺序\n\n默认情况下, argv在被解析时会将非选项的参数移动到数组的末尾. 可以通过在初始化后将`permute`字段设置为0来禁用它.\n\n~~~c\nstruct optparse options;\noptparse_init(\u0026options, argc, argv);\noptions.permute = 0;\n~~~\n\n## 3. 对比\n\noptparse的接口应该是任何熟悉getopt的人都熟悉的. 选项字符串具有相同的格式, 解析器结构体成员变量与getopt全局变量(optarg、optind、optopt)具有相同的名称.\n\nlong选项解析器`optparse_long()`API与GNU的`getopt_long()`非常相似，可以作为替代api.\n\noptparse没有分配内存. 此外optparse没有依赖项, 包括libc本身. 因此可以在标准C库无法使用的情况下使用.\n\n查看`optparse.h`文件可以了解完整的API.\n\n## 4. 示例使用\n\n这是一个通用的getopt命令解析代码:\n\n~~~c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \u003cstdbool.h\u003e\n#include \u003cgetopt.h\u003e\n\nint main(int argc, char **argv)\n{\n    bool amend = false;\n    bool brief = false;\n    const char *color = \"white\";\n    int delay = 0;\n\n    int option;\n    while ((option = getopt(argc, argv, \"abc:d::\")) != -1) {\n        switch (option) {\n        case 'a':\n            amend = true;\n            break;\n        case 'b':\n            brief = true;\n            break;\n        case 'c':\n            color = optarg;\n            break;\n        case 'd':\n            delay = optarg ? atoi(optarg) : 1;\n            break;\n        case '?':\n            return EXIT_FAILURE;\n        }\n    }\n\n    /* Print remaining arguments. */\n    for (; optind \u003c argc; optind++)\n        printf(\"%s\\n\", argv[optind]);\n    return 0;\n}\n~~~\n\n如果使用optparse在RTT下解析, 同样的功能将使用如下的代码的实现:\n\n~~~c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \u003cstdbool.h\u003e\n#include \"optparse.h\"\n\nint cmd_parse(int argc, char **argv)\n{\n    bool amend = false;\n    bool brief = false;\n    const char *color = \"white\";\n    int delay = 0;\n\n    char *arg;\n    int option;\n    struct optparse options;\n\n    optparse_init(\u0026options, argc, argv);\n    while ((option = optparse(\u0026options, \"abc:d::\")) != -1) {\n        switch (option) {\n        case 'a':\n            amend = true;\n            break;\n        case 'b':\n            brief = true;\n            break;\n        case 'c':\n            color = options.optarg;\n            break;\n        case 'd':\n            delay = options.optarg ? atoi(options.optarg) : 1;\n            break;\n        case '?':\n            fprintf(stderr, \"%s: %s\\n\", argv[0], options.errmsg);\n            return EXIT_FAILURE;\n        }\n    }\n\n    /* Print remaining arguments. */\n    while ((arg = optparse_arg(\u0026options)))\n        printf(\"%s\\n\", arg);\n    return 0;\n}\nMSH_CMD_EXPORT(cmd_parse, cmd description);\n~~~\n\n这是optparse对长选项参数的支持:\n\n~~~c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \u003cstdbool.h\u003e\n#include \"optparse.h\"\n\nint main(int argc, char **argv)\n{\n    struct optparse_long longopts[] = {\n        {\"amend\", 'a', OPTPARSE_NONE},\n        {\"brief\", 'b', OPTPARSE_NONE},\n        {\"color\", 'c', OPTPARSE_REQUIRED},\n        {\"delay\", 'd', OPTPARSE_OPTIONAL},\n        {0}\n    };\n\n    bool amend = false;\n    bool brief = false;\n    const char *color = \"white\";\n    int delay = 0;\n\n    char *arg;\n    int option;\n    struct optparse options;\n\n    optparse_init(\u0026options, argc, argv);\n    while ((option = optparse_long(\u0026options, longopts, NULL)) != -1) {\n        switch (option) {\n        case 'a':\n            amend = true;\n            break;\n        case 'b':\n            brief = true;\n            break;\n        case 'c':\n            color = options.optarg;\n            break;\n        case 'd':\n            delay = options.optarg ? atoi(options.optarg) : 1;\n            break;\n        case '?':\n            fprintf(stderr, \"%s: %s\\n\", argv[0], options.errmsg);\n            return EXIT_FAILURE;\n        }\n    }\n\n    /* Print remaining arguments. */\n    while ((arg = optparse_arg(\u0026options)))\n        printf(\"%s\\n\", arg);\n\n    return 0;\n}\n~~~\n\n## 5. 感谢\n\n1. 该库移植于https://github.com/skeeto/optparse.\n2. 感谢skeeto. 本移植是修改了部分原作者的代码针对RTT在线包实现的版本, 该仓库保留原作者的许可声明! 具体原作者许可请查看 https://github.com/skeeto/optparse/blob/master/UNLICENSE.\n\n## 6. 仓库\n\nhttps://github.com/RT-Thread-packages/optparse\n\n","funding_links":[],"categories":["Packages"],"sub_categories":["Misc"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRT-Thread-packages%2Foptparse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRT-Thread-packages%2Foptparse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRT-Thread-packages%2Foptparse/lists"}