Skip to content

Command-line parser library, fully tested, highly customizable. 灵活好用的命令行解析库。

License

Notifications You must be signed in to change notification settings

XUJINKAI/cmdparser

Repository files navigation

cmdparser

Command parser library (C/C++), with highly customizable.

命令行参数解析库,支持子命令嵌套,支持自定义错误语言,以及更多可定制化内容。

Feature

  • Friendly API, configuration in ONE struct

  • Follow GNU/POSIX option conversion, short/long option supported

  • Nested sub-commands support, and they can share same logic

  • Global option supported

  • Parse strict: repeat option detect

  • Highly customizable: Hide/Disable options or sub-commands, Language (error message), help option, doc generator, output stream, etc.

Demo:

Short option: -a -b -n XJK can be shorten to -abn XJK (-nab XJK as well)

Long option: --name=XJK can be write as --name XJK

End of option: -all -- -abc, -abc will be treat as argument

Global option can behind sub-command: git status --git-dir=.git :)

Repeat option detect: -a -b -a will cause error

Examples

demo_simple is an basic demo about parse options.

demo_git follows git commands demos how to handle complex sub-commands/options.

test Unit Tests covers most features.

xx-crypto-cli A real example with plenty of commands.

Document

Get Started

Define an global struct and call cmdp_run.

static cmdp_command_st cmdp = {
    .options = {
        {'i', "Int", "Input Int Option", CMDP_TYPE_INT4, &arg.i},
        {0},                        // ends with {0}
    },
    .fn_process = callback,         // called after options parsed
};

int main(int argc, char **argv)
{
    return cmdp_run(argc - 1, argv + 1, &cmdp, NULL);
}

Nested Sub-commands

Set sub_commands field, nested can be infinite.

static cmdp_command_st cmdp = {
    .options = {...},
    .sub_commands = (cmdp_command_st*[]){
        &(cmdp_command_st){
            .name = "sub",
            .options = {...},
            .fn_process = sub_callback,
        },
        NULL,                        // ends with NULL
    },
};

Custom Error Message

See test/chinese_error_msg_test.c

You can custom your own error message.

Credits

utest.h

License

Copyright 2022 github.com/XUJINKAI/cmdparser. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Command-line parser library, fully tested, highly customizable. 灵活好用的命令行解析库。

Topics

Resources

License

Stars

Watchers

Forks