home *** CD-ROM | disk | FTP | other *** search
- #########################################################################
- # ^FILE: parseargs.tcl - parseargs for tcl scripts
- #
- # ^DESCRIPTION:
- # This file defines a tcl procedure named parseargs to parse
- # command-line arguments for tcl scripts.
- #
- # ^HISTORY:
- # 05/07/92 Brad Appleton <brad@ssd.csd.harris.com> Created
- ##^^#####################################################################
-
- ########
- # ^PROCEDURE: parseargs - parse command-line argument lists
- #
- # ^SYNOPSIS:
- # parseargs <options> -- $scriptName arg [arg ...]
- #
- # where <options> is any valid option combination for parseargs(1)
- #
- # ^DESCRIPTION:
- # Parseargs will invoke parseargs(1) with the options and arguments
- # specified by the caller.
- #
- # ^RETURN-VALUE:
- # A string of variable settings for the caller to evaluate
- #
- # ^EXAMPLE:
- # #!/usr/bin/tcl -q
- #
- # source parseargs.tcl
- #
- # set arguments {
- # { '?', ARGHIDDEN, argUsage, NULL, "Help : print usage and exit" },
- # { 'S', ARGVALOPT, argStr, string, "STRing : optional string arg" },
- # { 'g', ARGLIST, argStr, groups, "newsGROUPS : groups to test" },
- # { 'r', ARGOPT, argInt, count, "REPcount : group repeat count" },
- # { 'd', ARGOPT, argStr, dirname, "DIRectory : working directory" },
- # { 'x', ARGOPT, argBool, xflag, "Xflag : turn on X-mode" },
- # { 'y', ARGOPT, argUBool, yflag, "Yflag : turn off Y-mode" },
- # { 's', ARGOPT, argChar, sepch, "SEPchar : field separator" },
- # { 'f', ARGLIST, argStr, files, "files : files to process" },
- # { 'n', ARGREQ|ARGPOS, argStr, name, "name : name to use" },
- # { ' ', ARGLIST, argStr, argv, "argv : remaining arguments" },
- # ENDOFARGS
- # }
- #
- # set count 1 ; set dirname "." ; set sepch "," ;
- # set xflag "" ; set yflag "TRUE" ;
- # set files {} ; set groups {} ; set name "" ;
- # set string "" ; set string_flag "" ;
- #
- # eval [ parseargs -u -a $arguments $scriptName $argv ]
- #
- #
- # ^ALGORITHM:
- # We need to do an "eval exec parseargs $args" in order have exec
- # treat $args as many arguments instead of just one argument. Before
- # we can do that however, we must quote each argument in $args and
- # escape any special characters that it contains. Hence we have the
- # following algorithm:
- #
- # - quote and escape special character for each arg in $args
- # - do an "eval exec $args" and save the results
- # - if parseargs(1) exit-status is non-zero than exit (and make
- # sure the parseargs message(s) is/are printed).
- # else
- # return the standard-output of parseargs(1)
- # endif
- #
- #
- # ^BUGS:
- # Actually - this procedure doesnt work. It has some problems,
- # some of which I know about and can describe below:
- #
- # 1) TCL refuses to let ME do the error-checking after exec'ing parseargs(1).
- # What I need to do is look at the exit-status and exit if it is non-zero.
- # If parseargs happens to write anything to stderr (which it always does
- # if it prints usage or a syntax error) then TCL automatically terminates
- # my procedure (not the process) and doesnt let me check the exit-status.
- #
- # 2) Error messages printed by parseargs(1) are prefixed with "Error: "
- # and suffixed by some other error-message info added by TCL. I dont
- # want ANY of this, just let parseargs(1) print the error text and dont
- # embellish it. As a fix, I tried to have parseargs(1) write error messages
- # to stdout (for TCL only) and exit with a non-zero status. This didnt
- # work at all (and Im not completely sure as to why).
- #
- # 3) I ought to be able to use far fewer "regsub" statements below but I
- # couldnt seem to get "&" or "\0" to work as documented as substitution
- # strings.
- #
- # If you happen to get this procedure (along with test.tcl) working, then
- # please, PLEASE let me know and tell me how you did it!!
- #
- ###^^####
- proc parseargs args {
- set escaped_args {}
- foreach arg $args {
- regsub -all "\\\\" "$arg" "\\\\" arg
- regsub -all "\\\$" "$arg" "\\\$" arg
- regsub -all "\\\[" "$arg" "\\\[" arg
- regsub -all "\]" "$arg" "\\\]" arg
- regsub -all "\{" "$arg" "\\\{" arg
- regsub -all "\]" "$arg" "\\\]" arg
- regsub -all "\"" "$arg" "\\\"" arg
- regsub -all "\t" "$arg" "\\t" arg
- regsub -all "\n" "$arg" "\\n" arg
- regsub -all "\r" "$arg" "\\r" arg
- regsub -all "\v" "$arg" "\\v" arg
- regsub -all "\f" "$arg" "\\f" arg
- regsub -all "\b" "$arg" "\\b" arg
- append escaped_args " \"$arg\""
- }
- set errorCode {}
- set opt_settings [ eval exec parseargs -s tcl $escaped_args ]
- if {( $errorCode != {} )} {
- ## echo $opt_settings
- exit [lindex $errorCode 2]
- }
- return $opt_settings ;
- }
-
-