79627788

Date: 2025-05-18 20:11:02
Score: 0.5
Natty:
Report link

If you're looking for a more flexible way to handle multiple values for a single option in C, you might consider using Hopt, a lightweight and modular library designed for command-line option parsing. Hopt allows you to define options that can accept multiple arguments seamlessly. Here's an example demonstrating how to use Hopt to parse multiple values for a single option:

#include "hopt.h"

typedef struct s_opt
{
    char*   users[3];
    char*   name;
}   t_opt;

int main(int ac, char** av)
{
    t_opt opt = {0};

    //hopt_disable_sort(); // The AV will be automatically sort after parsing, so you can disable it
    hopt_help_option("h=-help=?", 0, 0);
    hopt_add_option("l=-list", 3, HOPT_TYPE_STR, &opt.users, "List of users");
    hopt_add_option("n=-name", 1, HOPT_TYPE_STR, &opt.name, NULL);
    int count = hopt(ac, av);
    ac -= (count + 1);
    av += (count + 1);

    // ... rest of your program
}

In this example, the -l or --list option can be followed by multiple user names, and Hopt will collect them into the opt.users array. Hopt supports both short and long options (endlessly), optional and required arguments, and provides a clean API for parsing command-line options.

Note: I developed this library to address some limitations I encountered with Argp and Getopt.

Reasons:
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Bama