Keywords: argument option switch parameter parse parser function lib library regex module read command string variable value name pair fast bobo download free
License:
New BSD License
Look for a new version with objects:
http://www.autohotkey.com/forum/viewtop ... 004#458004
Download (~23 kb)
The archive contains the library, demo script and full documentation.
Online DocumentationSyntax Code:
count := argp_parse( source [, maxcount, keyname1, value1, ... keyname32, value32] )
optlist := argp_getopt( source [, keylist, caseSense, value1, ... value32] )
There are two independently working functions. They have the same regular expression at the core, but the functionality and interface differs. One creates variables for each key name and the other one returns a list with names, checking against another list of possible names (either with case sense on or off and, removing dublicates). After calling one of the functions, the variables or the list must be parsed. This is left to be done by the user. For the beginner, it is recommended to use argp_getopt() and then check with InStr() if a name exists in resulting list (with surrounding spaces).
No globals are created, no special objects or frameworks are needed.
DescriptionSometimes we need to work with many optional parameters. Some of the parameters may be just logical flags only (true/false). If the function have too many such parameters, the usability is endangered. To workaround that disgrace, we can make one option string to the function. From now on, this string contains all parameters. This is comparable to the commandline.
It must be parsed to find all parts. Writing such parsers could become difficult and slow down the complete process. The parsing is tried to be made with one regex call only (with some other non dramatic lines). As long as the string is keeped simple, there should be no problem. But with growing complexity, with different values of subparameters, it is a pain to make the difference.
SpecificationIt works with complex option strings and the formatting is quite free. Any option must begin either with "-" or "/". Every option can have values. The spaces around values are lost, unless it is enclosed between single or double quotes. The name/value separator is optional and can be ":", "=" or " " (space). Spaces around the options are also optional. You see, its not that strict. This example string:
Code:
"-A -f:'file name.ahk'/i"
would be resolved to these 3 options
Code:
A
f
i
The option "f" would hold the value without quotes
Code:
file name.ahk
This nice example visualizes how the argp-functions sees the complex string. The
green-parts are the parameter names. The
orange-parts are the corresponding optional values of the parameters. Any
grey-part is badly formatted and ignored, according the rules of the internally used regular expression. The
red-parts are punctuation characters for separating the value from its name and marking the end of value.
This string:
Code:
-A='3/3'-/ c8-x'2 '-i--test.num: '44' /yy0 -f /f: c:"\tes"t-d\\E4 d\E\d -k:0/-t /file:'c:\t /e:1 st \file.ahk' -e 2 /i -extra:'test/e:3 //e:4 /' /time:5:10 -date:11.08.2009
... would be seen as:
Code:
-A='3/3'-/ c8-x'2 '-i--test.num: '44' /yy0 -f /f: c:"\tes"t-d\\E4 d\E\d -k:0/-t /file:'c:\t /e:1 st \file.ahk' -e 2 /i -extra:'test/e:3 //e:4 /' /time:5:10 -date:11.08.2009
"
A", "
x", "
i", "
test.num", "
yy0", "
f", "
f", "
d", "
k", "
t", "
file", "
e", "
i", "
extra", "
time" and "
date".
- An argument must start with "-" or "/".
- Then argument name consisting of these characters follows: a-z, A-Z, 0-9, _, ., ?, ! or @
- The value/name separator ":" "=" after the name is optional. A space can be used also for this.
- After the separator, the argument can have a value.
- The value can be enclosed between single or double quotes (then a separator ":" or "=" is needed).
- If the value is not enclosed between quotes, the next space or argument ends the value.
- Any space around the value is lost.
Some usage examplesThe options variable holds the string given by the end user:
Code:
options := "-a:'foo -bar ' -B -c hello world"
- Get the count of found options. Restrict to a maximum of 8 items.
Code:
count := argp_parse(options, 8)
- Get a maximum of 3 options. Save the resulting names in variables 'name1' to 'name3' and corresponding values in 'value1' to 'value3'.
Code:
argp_parse(options, 3, name1, value1, name2, value2, name3, value3)
- Check if the options namely a", "b" and "c" exist and get a list with existing option names (with case sense on).
Code:
optlist := argp_getopt(options, "a b c")
The documentation contains usage examples and detailed descriptions of all functions.
Full usage example of argp_getopt() functionCode:
display(options)
{
; Default values of options.
sentence = hello world
; Parsing options.
optlist := argp_getopt(options, "sentence caseMod html", false, sentence_value, caseMod_value)
Loop, Parse, optlist, %A_Space%, %A_Space%
{
If ("sentence" = A_LoopField)
sentence := sentence_value
Else If ("caseMod" = A_LoopField)
{
If (caseMod_value = 1)
StringUpper, sentence, sentence, T
Else If (caseMod_value = 2)
StringUpper, sentence, sentence
Else If (caseMod_value = 3)
StringLower, sentence, sentence
}
Else If ("html" = A_LoopField)
Transform, sentence, HTML, %sentence%
}
MsgBox %sentence%
}
Lets see how a call could look now
Code:
display("-caseMod 2 -html")
Last changesLicense changed to New BSD without changing scriptrevision.
- breaks compatibility with last version
- general rework at internal used regular expression
- changed: renamed library and all functions from "args" to "argp" prefix and now args() is argp_parse() and args_names() is argp_getopt()
- changed: the special optimization with 24 elements of argp_parse() is not longer present anymore, but added optimization maxcount "8" at argp_parse()
- changed: the resulting (return value) list of argp_getopt() reflects the sorting order from original args source, and not from specified given list
- new: first option of argp_getopt() (argp_parse() does not support this) can be a value without key, but it is not included in count and result is saved in ErrorLevel
- extended: key names can contain one of these characters now a-z, A-Z, 0-9, _, ., ?, ! and @
Alternative libraries
You may also want to check Yook's
Parameters and switches parser
or majkinetor's
Parse
or ahklerner's
options.ahkl - an options object for AutoHotkey_L libraries.