Thursday, November 07, 2024

Significant Re-write of how Gnocl Handle Commands

offset idx to accommodate point at which options occur 

idx = objc
no options
gnocl::device 

idx = 1
no subcommand but with options
gnocl::device -option AAA -option AAA... 

idx = 2
subcommand with options
gnocl::device cmdName -option AAA -option AAA... 

idx = 3
gnocl::device cmdName string -option AAA -option AAA... 

 

gnoclParseCommandOptions

 

/**
\brief    Command Template.
**/
int gnoclTemplateCmd ( ClientData data, Tcl_Interp *interp, int objc, Tcl_Obj * const objv[] )
{
    static const char *description = "description";

    static GnoclCommand commands[] = {
        { "cmdName", "args", "description", "default" },
        { NULL }
    };

    static GnoclOption options[] = {
        /* command/widget specfic options */
        {
            "-option", GNOCL_STRING, NULL, NULL,
            "default",
            "description",
            GNOCL_OPTIONS_IMAGE
        },
        { NULL }
    };

    if ( gnoclGetDocumentation ( interp, objc, objv, description, commands, options ) == TCL_OK ) {
        return TCL_OK;
    }

    enum cmdIdx { CmdNameIdx };
    enum optIdx { OptIdx };
    
    gint idx;

    if ( gnoclParseCommandOptions ( interp, 2, objc, objv, options ) != TCL_OK ) {
        gnoclClearOptions ( options );
        return TCL_ERROR;
    }


    if ( Tcl_GetIndexFromObjStruct ( interp, objv[1], ( char ** ) &commands[0].cmdName, sizeof ( GnoclCommand ), "command", TCL_EXACT, &idx ) != TCL_OK ) {
        return TCL_ERROR;
    }

    switch ( idx ) {
        case CmdNameIdx:  {
                GNOCL_DEBUG_LINE
                if ( options[OptIdx].status == GNOCL_STATUS_CHANGED ) {
                    GNOCL_DEBUG_MSG( "YAY! It's working!!!" )
                    GNOCL_DEBUG_MSG (options[OptIdx].val.str)
                }
            }; break;

        case 1:  {
                GNOCL_DEBUG_LINE
            }; break;

        default: {
                GNOCL_DEBUG_LINE
            };
    }

    return TCL_OK;
}


 

Saturday, November 02, 2024

Remove Stray Repeated Spaces from a String

 

 

 

#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require -exact Gnocl 3.24

set str "\tAbc  def  ghi   klm    a. \tYes!   "

## reduce multiple spaces within a string but retain \t, trim all external spaces.
# args: str        string to modify
# returns:        updated string
#
proc removeSpaces { str } {
    while 1 {
        if { [string first "  " $str] == -1 } { break }
        set str [string map [list "  " " "] $str]
    }
    return [string trim $str " "]
}

puts >[removeSpaces $str]<