Good morning Clinton,

Please find attached a copy of my modified version of options.icn. The
modification list as follows:

1). allows --help (or options of that format) to be used, entered by
using --help in the list of options,
2). allows an option to occur multiple time and collects the option
values as a list
3). allows specification of terminating option string or list of
terminating option strings (default is "--", but use what you will)

An example of a short test code is also attached.

As for the string balance procedure, I will follow up as you have suggested.

regards

Bruce Rennie
On 27/05/15 09:27, Jeffery, Clint ([email protected]) wrote:
> Hi Bruce,
>
> Regarding options.icn your discovery that punctuation marks can be options is 
> interesting. Some programs definitely might use them.
>
> Compatible extensions to options.icn that make it flexible enough to handle 
> Unicon's own command line option processing would be welcome as a submission 
> to the main Unicon distribution.  If we can then eliminate a bunch of code in 
> the Unicon translator that would be swell.
>
> I think Jafar maybe had some interesting things to say in regards to the Icon 
> (and Unicon) -x processing, which disables all options processing after the 
> -x.  Anyone who uses -x probably understands why it terminates options 
> processing on later arguments: because subsequent options, which are allowed, 
> are passed into and interpreted by the Icon/Unicon program being executed, 
> not by the Unicon translator.
>
> Regarding balancing multi-character strings, I hope someone finds a procedure 
> you can use. You might also try the Icon mailing list.  Ralph Griswold was 
> fond of writing replacements for built-ins in Icon, so a version of bal() 
> written in Icon may very well be floating around.
>
> If you are to write your own, you might start with slashbal() from 
> ipl/procs/scan.icn and just tweak its use of the relevant parameters.
>
> Cheers,
> Clint
>
> ________________________________________
> From: Bruce & Breeanna Rennie <[email protected]>
> Sent: Tuesday, May 26, 2015 4:50 AM
> To: Unicon group
> Subject: [Unicon-group] modified version of options.icn file from ipl
>
> For anyone interested,
>
> I have made some minor modifications to the options.icn file from the
> IPL. Just by accident, I found that it allows a single non-alphanumeric
> character to be an option name. Examples are -#, -[, etc. This ability
> is not described within the documentation in the file.
>
> The minor modifications that i have made is to add an additional
> parameter to the procedure. This parameter allows one to specify an
> option or a list of options that will stop the extraction of options
> from the command line arguments. The standard value for this in the
> original procedure was --. This is still the default in my code.
>
> The reason for making these changes is due to the outcome of a brief
> conversation with Steve Wampler. we were discussing some possible
> changes for the unicon compiler and unicon language. In following up
> Steve's suggestions, I have been looking at the unicon compiler code. i
> noticed that the option processing didn't use the options procedure. I
> understand this is due to the specific requirements of the unicon compiler.
>
> I have made changes to options.icn to allow its use within the unicon
> compiler.
>
> If anyone is interested in my updates, please contact me and I'll pass
> them onto you.
>
> A question to you all. Has anyone created a version of the inbuilt
> function bal which uses two strings for the balance criteria instead of
> csets? I have a specific need for this and if someone has already done
> so, I would like to utilise your expertise instead of reinventing the wheel.
>
> regards to all this wonderful autumn evening.
>
> Bruce Rennie
>
>
>
>
>
> ------------------------------------------------------------------------------
> One dashboard for servers and applications across Physical-Virtual-Cloud
> Widest out-of-the-box monitoring support with 50+ applications
> Performance metrics, stats and reports that give you Actionable Insights
> Deep dive visibility with transaction tracing using APM Insight.
> http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
> _______________________________________________
> Unicon-group mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/unicon-group
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Unicon-group mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/unicon-group
>

############################################################################
#
#       File:     options.icn
#
#       Subject:  Procedure to get command-line options
#
#       Authors:  Robert J. Alexander and Gregg M. Townsend
#
#       Modification: Bruce Rennie 26-May-2015
#
#       Date:     May 5, 2000
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#       options(arg, optstring,errproc) removes command options from the
#       argument list of an Icon main procedure, returning a table of
#       option values.
#
############################################################################
#  
#     options(arg,optstring,errproc) -- Get command line options.
#
#     This procedure separates and interprets command options included in 
#  the main program argument list.  Option names and values are removed
#  from the argument list and returned in a table.
#
#     On the command line, options are introduced by a "-" character.  An
#  option name is either a single printable character, as in "-n" or "-?",
#  or a string of letters, numbers, and underscores, as in "-geometry".
#  Valueless single-character options may appear in combination, for
#  example as "-qtv".
#
#     Some options require values.  Generally, the option name is one
#  argument and the value appears as the next argument, for example
#  "-F file.txt".   However, with a single-character argument name
#  (as in that example), the value may be concatenated: "-Ffile.txt"
#  is accepted as equivalent.
#  
#     Options may be freely interspersed with non-option arguments.
#  An argument of "-" is treated as a non-option.  The special argument
#  "--" terminates option processing.  Non-option arguments are returned
#  in the original argument list for interpretation by the caller.
#
#     An argument of the form @filename (a "@" immediately followed
#  by a file name) causes options() to replace that argument with
#  arguments retrieved from the file "filename".  Each line of the file
#  is taken as a separate argument, exactly as it appears in the file.
#  Arguments beginning with - are processed as options, and those
#  starting with @ are processed as nested argument files.  An argument
#  of "--" causes all remaining arguments IN THAT FILE ONLY to be
#  treated as non-options (including @filename arguments).
#
#     The parameters of options(arg,optstring,errproc) are:
#  
#       arg         the argument list as passed to the main procedure.
#
#       optstring   a string specifying the allowable options.  This is
#                   a concatenation, with optional spaces between, of
#                   one or more option specs of the form
#                       -name%*
#                   where
#                       -       introduces the option
#                       name    is either a string of alphanumerics
#                               (any of a-z, A-Z, 0-9, and _)
#                               or any single printable character
#                       %       is one of the following flag characters:
#                               !       No value is required or allowed
#                               :       A string value is required
#                               +       An integer value is required
#                               .       A real value is required
#                               $       Optional value - anything
#                       *       indicates that the option can occur multiple 
#                               times in the command options and must follow
#                               the % specifier, if used.
#
#                   The leading "-" may be omitted for a single-character
#                   option.  The "!" flag may be omitted except when
#                   needed to terminate a multi-character name.
#                   Thus, the following optstrings are equivalent:
#                       "-n+ -t -v -q -F: -geometry: -silent"
#                       "n+tvqF:-geometry:-silent"
#                       "-silent!n+tvqF:-geometry:"
#
#                   If "optstring" is omitted any single letter is
#                   assumed to be valid and require no data.
#
#       errproc     a procedure which will be called if an error is
#                   is detected in the command line options.  The
#                   procedure is called with one argument:  a string
#                   describing the error that occurred.  After errproc()
#                   is called, options() immediately returns the outcome
#                   of errproc(), without processing further arguments.
#                   Already processed arguments will have been removed
#                   from "arg".  If "errproc" is omitted, stop() is
#                   called if an error is detected.
#  
#     A table is returned containing the options that were specified.
#  The keys are the specified option names.  The assigned values are the
#  data values following the options converted to the specified type.
#  A value of 1 is stored for options that accept no values.
#  The table's default value is &null.
#
#     Upon return, the option arguments are removed from arg, leaving
#  only the non-option arguments.
#  
############################################################################

procedure options(arg,optstring,errproc,finshoption[])
    local f,fList,fileArg,fn,ignore,optname,opttable,opttype,p,x,option,optcs
    local tmpopt, mulopt
    #
    #  Initialize.
    #
    /optstring := string(&letters++"_")
    /errproc := stop
    /finshoption := ["--"]
    option := table()
    fList := []
    opttable := table()
    optcs := &letters ++ &digits ++ '_'
    #
    #  Scan the option specification string.
    #
    optstring ? {
        while optname := move(1) do {
            if optname == " " then next
            if optname == "-" then {
                optname := tab(many(optcs)) | move(1) | break
            }
            if optname == "-" then {
                optname ||:= tab(many(optcs))
            }
            opttype := tab(any('!:+.$')) | "!"
            if ="*" then {
                opttype ||:= "*"
            }
            opttable[optname] := opttype
            }
        }
    write(ximage(opttable))
    #
    #  Iterate over program invocation argument words.
    #
    while x := get(arg) do {
        if /x then {
            ignore := &null     # if end of args from file, stop ignoring
        } else {
            x ? {
                if =!finshoption & pos(0) & /ignore then {
                    ignore := 1   # ignore following args if found in list 
finshoption
                    push(arg, x)
                } else if ="-" & not pos(0) & /ignore then {
                    tab(0) ? until pos(0) do {
                        if opttype := \opttable[
                                optname := ((pos(1),tab(0)) | move(1))] then {
                            if *opttype = 2 & opttype[2] == "*" then {
                                /option[optname] := []
                                mulopt := 1
                            } else {
                                mulopt := &null
                            }
                            tmpopt :=
                                if any(':+.',opttype) then {
                                    p := "" ~== tab(0) | get(arg) |
                                        return errproc(
                                                "No parameter following -" || 
optname)
                                    case opttype[1] of {
                                        ":" : { 
                                            p
                                        }
                                        "+" : {
                                            integer(p) |
                                                return errproc("-" || optname ||
                                                    " needs numeric parameter")
                                        }
                                        "." : {
                                            real(p) |
                                                return errproc("-" || optname ||
                                                    " needs numeric parameter")
                                        }
                                    }
                                } else if opttype[1] == "$" then {
                                    p := "" ~== tab(0) | get(arg)
                                    if match("-", p) then {
                                        push(arg, p)
                                        p := 1
                                    } else {
                                        p
                                    }
                                } else {
                                    1
                                }
                            if \mulopt then {
                                put(option[optname], tmpopt)
                            } else {
                                option[optname] := tmpopt
                            }
                        } else {
                            return errproc("Unrecognized option: -" || optname)
                        }
                    }
                #
                #  If the argument begins with the character "@", fetch option
                #  words from lines of a text file.
                #
                } else if ="@" & not pos(0) & /ignore then {
                    f := open(fn := tab(0)) |
                        return errproc("Can't open " || fn)
                    fileArg := []
                    while put(fileArg,read(f))
                    close(f)
                    push(arg)   # push null to signal end of args from file
                    while push(arg,pull(fileArg))
                    
                } else {
                    put(fList,x)
                }
            }
        }
    }
    while push(arg,pull(fList))
    return option
end
link ximage
procedure main(args)
    local argtab := 
options(args,"--help!-?-B-c-C-D:*-E-features!-fs!-G-I:*-L:*-log$-M-o:-O-quiet-s-t-u-v+-version!-y-yydbg!-Z",,"-x")
    write(ximage(argtab))
    write(ximage(args))
end
link options
link ximage
------------------------------------------------------------------------------
_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to