ParseArguments (open)
Parse the arguments passed to a script
Syntax
LOADLIB "wh::os.whlib";
RECORD FUNCTION ParseArguments(STRING ARRAY arguments, RECORD ARRAY vars)Parameters
STRING ARRAY argumentsThe list of arguments to parse (e.g. the result of GetConsoleArguments)
RECORD ARRAY varsA list (RECORD ARRAY) of variables to look for
defaultvalueDefault value if argument is not provided (only used for "switch" arguments)
nameThe name of the variable, this will be used as the resulting record cell name
requiredShould the argument be provided (only used for "param" arguments)
typeOne of "switch" (returns a BOOLEAN), "stringopt" (STRING), "stringlist" (STRING ARRAY), "param" (STRING), "paramlist" (STRING ARRAY)
Return value
RECORDA record containing the parsed variables, or a non-existing record if the arguments could not be parsed
Description
This function reads a list of script arguments and interprets them as variables with values. This function will cause an error if executed without an active console (ie, not started by runscript).
Examples
// Some example argument list
STRING consolearguments :=
"-m foo --path=/test/ --option -o- -m=bar 3 one two three";
STRING ARRAY arguments_list := Tokenize(consolearguments, " ");
// Variables we are interested in
RECORD ARRAY vars :=
[ [ name := "option", type := "switch" ]
, [ name := "o", type := "switch", defaultvalue := TRUE ]
, [ name := "p", type := "switch", defaultvalue := TRUE ]
, [ name := "m", type := "stringlist" ]
, [ name := "path", type := "stringopt" ]
, [ name := "count", type := "param" ]
, [ name := "other", type := "paramlist" ]
];
// Parse the arguments
RECORD arguments := ParseArguments(arguments_list, vars);
// Now we can access the individual arguments:
// arguments.option = TRUE
// arguments.o = FALSE
// arguments.p = TRUE
// arguments.m = [ "foo", "bar" ]
// arguments.path = "/test/"
// arguments.count = "3"
// arguments.other = [ "one", "two", "three" ]