cargs: A Lightweight Cross-Platform Command Line Argument Parsing Library

cargs: A Lightweight Cross-Platform Command Line Argument Parsing Library

In C++ development, handling command line arguments is a common requirement. Whether for developing tools, automation scripts, or system monitoring programs, an efficient and concise way to parse command line input is needed. The cargs library is designed to address this issue. It is a lightweight, cross-platform command line argument parsing library suitable for C and C++ projects.

Features

The main features of the cargs library include:

  • Cross-Platform: Supports various operating systems including Windows, Linux, macOS, and FreeBSD.
  • Easy to Use: Provides a simple interface that requires only a single header file to use.
  • No Dynamic Memory Allocation: In environments where dynamic memory allocation is restricted, the advantages of cargs are particularly evident.
  • Automatic Help Information: Can automatically generate help information, making it easier for users to understand available command line options.
  • Support for Short and Long Options: Allows developers to define short options (e.g., -s) and long options (e.g., --simple), providing users with more choices.
  • Parameter Value Support: Can handle options with parameter values, such as -k=VALUE.

Usage

Using the cargs library is very simple. First, you need to define a set of options, and then loop through to parse the command line arguments. Here is a simple example:

#include <cargs.h>
#include <stdbool.h>
#include <stdio.h>

static struct cag_option options[] = {
    {.identifier = 's', .access_letters = "s", .description = "Simple flag"},
    {.identifier = 'm', .access_letters = "mMoO", .description = "Multiple access letters"},

    {.identifier = 'l', .access_name = "long", .description = "Long parameter name"},
    {.identifier = 'k', .access_letters = "k", .access_name = "key", .value_name = "VALUE", .description = "Parameter value"},
    {.identifier = 'h', .access_letters = "h", .access_name = "help", .description = "Shows the command help"}
};

int main(int argc, char *argv[]) {
    bool simple_flag = false, multiple_flag = false, long_flag = false;
    const char *value = NULL;

    cag_option_context context;
    cag_option_init(&context, options, CAG_ARRAY_SIZE(options), argc, argv);

    while (cag_option_fetch(&context)) {
        switch (cag_option_get_identifier(&context)) {
            case 's':
                simple_flag = true;
                break;
            case 'm':
                multiple_flag = true;
                break;
            case 'l':
                long_flag = true;
                break;
            case 'k':
                value = cag_option_get_value(&context);
                break;
            case 'h':
                printf("Usage: cargsdemo [OPTION]...\n");
                printf("Demonstrates the cargs library.\n\n");
                cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
                return EXIT_SUCCESS;
            case '?':
                cag_option_print_error(&context, stdout);
                break;
        }
    }

    printf("simple_flag: %i, multiple_flag: %i, long_flag: %i, key: %s\n", simple_flag, multiple_flag, long_flag, value ? value : "-");
    return EXIT_SUCCESS;
}

In this example, the cag_option structure is used to define command line options, the cag_option_init function initializes the parsing context, and the cag_option_fetch function is used to parse the parameters one by one.

Application Scenarios

cargs is suitable for various scenarios, including but not limited to:

  • Configuration Tools: Parsing user-input configuration parameters.
  • Automation Scripts: Handling command line arguments during script execution.
  • System Monitoring Programs: Adjusting monitoring behavior based on user-input parameters.

Conclusion

cargs is a powerful and easy-to-use command line argument parsing library. With its concise API and cross-platform support, it provides C and C++ developers with an efficient way to handle command line input. Whether in small tools or large projects, cargs can help developers quickly implement command line interaction features.

Leave a Comment