argtable / argtable3

A single-file, ANSI C, command-line parsing library that parses GNU-style command-line options.
http://www.argtable.org
Other
377 stars 65 forks source link

Erroneous substring matching of long options. #99

Closed Olivio-K closed 5 months ago

Olivio-K commented 5 months ago

I have a long option by the name of "help" as illustrated by the code snippet below:

    char const programName[] = "substring";
    struct arg_lit * helpOption = NULL;
    struct arg_end * endOption = NULL;
    uint8_t maxNumberOfParserErrorsToHandle = 20;

    void * argtable[] = 
    {
        helpOption    = arg_litn(NULL, "help", 0, 1, "Display this information."),
        endOption     = arg_end(maxNumberOfParserErrorsToHandle),
    };

Unfortunately helpOption->count == 1 for the following command line input: ./substring --hel. Note: the long option entered is hel and not help, therefore I would expect a parser error here.

Here is the full source code for the executable named substring:

#include <stdlib.h>
#include <stdint.h>
#include "argtable3.h"

int main( int argc, char *argv[ ] )
{
    char const programName[] = "substring";
    struct arg_lit * helpOption = NULL;
    struct arg_end * endOption = NULL;
    uint8_t maxNumberOfParserErrorsToHandle = 20;

    void * argtable[] = 
    {
        helpOption    = arg_litn(NULL, "help", 0, 1, "Display this information."),
        endOption     = arg_end(maxNumberOfParserErrorsToHandle),
    };

    int8_t exitCode = EXIT_SUCCESS;

    if(arg_nullcheck(argtable) != 0)
    {
        printf("Error: Insufficient memory to allocate the command line parser.\n");
        exitCode = EXIT_FAILURE;
    }
    // If number of parser errors > 0
    else if(arg_parse(argc, argv, argtable) > 0)
    {
        arg_print_errors(stdout, endOption, programName);
        printf("Try '%s --help' for more information.\n", programName);

        exitCode = EXIT_FAILURE;
    }
    else
    {
        exitCode = EXIT_SUCCESS;

        if(helpOption->count > 0)
        {
            printf("Usage: %s", programName);
            arg_print_syntax(stdout, argtable, "\n");
            arg_print_glossary(stdout, argtable, "  %-25s %s\n");
        }
        else
        {
            // TODO: handle parsed CL options.
        }
    }

    arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));

    return exitCode;
}
Olivio-K commented 5 months ago

Never mind, this seems to be a getopt_long feature. Sorry for the noise.