Commit 8009cc26 authored by sagnowski's avatar sagnowski
Browse files

CmdLnParser: Clean up handling of optional properties in CmdLnParser_Option

parent 0b29b188
Loading
Loading
Loading
Loading
+42 −20
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ static int16_t initOpts(
static int8_t stringLooksLikeOption(
    const char *str )
{
    if ( ( str[0] == '-' ) && is_number( str ) == false )
    if ( ( str[0] == '-' ) && !is_number( str ) )
    {
        return 1;
    }
@@ -150,25 +150,31 @@ static int8_t optionMatchesString(

    const char *optionName = stringToOptionName( str );

    char optionName_to_upper[FILENAME_MAX];
    char optionName_to_upper[MAX_OPTION_LENGTH + 1];
    strncpy( optionName_to_upper, optionName, sizeof( optionName_to_upper ) - 1 );
    optionName_to_upper[sizeof( optionName_to_upper ) - 1] = '\0';
    to_upper( optionName_to_upper );

    char match_to_upper[FILENAME_MAX];
    char match_to_upper[MAX_OPTION_LENGTH + 1];
    strncpy( match_to_upper, opt.props.match, sizeof( match_to_upper ) - 1 );
    optionName_to_upper[sizeof( match_to_upper ) - 1] = '\0';
    to_upper( match_to_upper );
    if ( strncmp( optionName_to_upper, match_to_upper, MAX_OPTION_LENGTH ) == 0 )
    {
        return 1;
    }

    char matchShort_to_upper[FILENAME_MAX];
    strncpy( matchShort_to_upper, opt.props.matchShort, sizeof( matchShort_to_upper ) - 1 );
    optionName_to_upper[sizeof( matchShort_to_upper ) - 1] = '\0';
    to_upper( matchShort_to_upper );
    if ( opt.props.matchShort != NULL )
    {
        strncpy( match_to_upper, opt.props.matchShort, sizeof( match_to_upper ) - 1 );
        optionName_to_upper[sizeof( match_to_upper ) - 1] = '\0';
        to_upper( match_to_upper );

    if ( strncmp( optionName_to_upper, match_to_upper, MAX_OPTION_LENGTH ) == 0 || strncmp( optionName_to_upper, matchShort_to_upper, MAX_OPTION_LENGTH ) == 0 )
        if (  strncmp( optionName_to_upper, match_to_upper, MAX_OPTION_LENGTH ) == 0 )
        {
            return 1;
        }
    }

    return 0;
}
@@ -307,7 +313,12 @@ static const char *getBasename(
static int32_t totalNumOptChars(
    const OptionProps opt )
{
    int32_t len = (int32_t) strlen( opt.match ) + strlen( opt.matchShort );
    int32_t len = (int32_t) strlen( opt.match );

    if ( opt.matchShort != NULL )
    {
        len += (int32_t) strlen( opt.matchShort ); 
    }

    if ( opt.placeholder != NULL )
    {
@@ -378,22 +389,33 @@ static void printOptions(

        numOptChars = totalNumOptChars( optionProps[i] );

        if ( opt.placeholder != NULL )
        fprintf( stderr, "  --%s", opt.match );
        numOptChars += 4;

        if ( opt.matchShort != NULL )
        {
            fprintf( stderr, "  --%s, -%s %s", opt.match, opt.matchShort, opt.placeholder );
            numOptChars += 8;
            fprintf( stderr, ", -%s", opt.matchShort );
            numOptChars += 3;
        }
        else

        if ( opt.placeholder != NULL )
        {
            fprintf( stderr, "  --%s, -%s", opt.match, opt.matchShort );
            numOptChars += 7;
            fprintf( stderr, " %s", opt.placeholder );
            numOptChars += 1;
        }

        if ( opt.description != NULL )
        {
            /* Done printing options column, fill with whitespace until description column */
            printWhitespace( descriptionColumnIdx - numOptChars - 2 /* account for ": " below */ );
            fprintf( stderr, ": " );
            printOptDescriptionAligned( opt.description, descriptionColumnIdx );
        }
        else
        {
            fprintf( stderr, "\n" );
        }
    }
}

static void printUsage(
+4 −2
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ typedef struct
{
    int32_t id;        /*  Unique ID for the option */
    const char *match; /*  String to match, e.g. "input" here will match "--input" on CLI */

    /* Struct members below are optional and can be omitted in option definition. If omitted, C will implicitly set them to 0. */
    const char *matchShort;  /* Short version of the string to match, e.g. "i" here will match "-i" on CLI */
    const char *placeholder; /* If not NULL, this will follow the match string in the usage printout, e.g. "<file>" here will print "--input <file>" on CLI */
    const char *description; /* Description of the option for the usage printout. May contain '\n' for line breaks. */