argtable / argtable3

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

A question mark can't be used as a short option. #98

Closed Olivio-K closed 4 months ago

Olivio-K commented 4 months ago

Hi,

It seems that a question can't be used as a short option. I don't know if this is a bug or a feature. (I am using the latest argtable3 from the master branch.) Here is the command line I am using:

./questionmark -?

Here is what arg_print_errors prints out:

questionmark: invalid option "-?"

Here is the full source for the questionmark executable:


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

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

    void * argtable[] = 
    {
        helpOption    = arg_litn("?", "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 4 months ago

This is a getopt feature. See: https://stackoverflow.com/a/12540999 . Sorry for the noise again.