I am calling a vbScript from PowerShell, with arguments that are defined in a text string, and I want to validate that the arguments being used are correct. Say my valid arguments are ARG1, ARG2 & ARG3, and someone has a string of "/ARGG1:one /ARG2:two", I want to validate the named arguments against an array of valid arguments and catch that ARGG1.
Where I am failing is in getting the names of the named arguments. I can reference a particular named argument with WScript.Arguments.Named("ARG1") for example. But I can't seem to loop through WScript.Arguments.Named and return the actual argument names used.
Something like this
For Each Argument In WScript.Arguments.Named
msgBox Argument.name
Next
When iterating the Named argument collection you are accessing the key (argument name) not the WshNamed object itself. If you pass the key back into the collection you will be able to access the value for that Named Argument.
Dim key
For Each key In WScript.Arguments.Named
Dim argument: argument = WScript.Arguments.Named.Item(key)
Call WScript.Echo(key) 'Return argument name
Call WScript.Echo(argument) 'Return argument value
Next
Output:
ARG1
one
ARG2
two
Related
I am trying to subtract two integer values as such:
But I get this error:
Unable to process template language expressions in action 'Negative_Index_of_Snabel-a' inputs at line '0' and column '0': 'The template language function 'sub' expects its first parameter to be an integer or a decimal number. The provided value is of type 'String'. Please see https://aka.ms/logicexpressions#sub for usage details.'.
I know that my variables in the sub() function is in quotes. But I cannot save it otherwise.
You're passing in literal string values, you need to specify that the values you want to evaluate are variables, like thus ...
sub(variables('VarMailInt'),variables('VarSnabelaIndex'))
Please explain "keyword arguments" and give some examples of their use.
A function may use positional arguments and/or keyword arguments. Function arguments are used to provide functions with values developed outside of the function itself, values the function needs to know. For a given function, the actual value assigned to a specific argument may change from one call to the next.
A positional argument holds the place for specified value by its ordinal position in the function's argument list. For example, x and real, imag are positional arguments in these function definitions:
sqrt(x) = x^0.5
complex(real, imag) = real + imag*im
A keyword argument holds the place of a specified value by its name. The last (rightmost) positional argument comes before the first keyword argument. A semicolon (;) demarks the start of keyword arguments, even when there are no positional arguments. When both kinds of argurment are used, the semicolon separates the positional from the keyword arguments. For example, digitcount and digits are keyword arguments in this function definition:
roundedstring(x; digitcount) = string(round(x, digits=digitcount))
Here is an example that only uses keyword arguments:
function pathname(; dirpath, filename)
return joinpath(dirpath, filename)
end
dir_path = "/home/workfiles"
file_name = "current.txt"
path_name = pathname(dirpath=dir_path, filename=file_name)
# pathname == "/home/workfiles/current.txt"
Here is almost the same example, using both kinds of argument:
function pathname(dirpath; filename)
return joinpath(dirpath, filename)
end
dir_path = "/home/workfiles"
file_name = "current.txt"
path_name = pathname(dir_path, filename=file_name)
# pathname == "/home/workfiles/current.txt"
One reason to use keyword arguments:
function copyfile(; source, dest)
# function body
end
src_pathname = "~/current.txt"
dst_pathname = "/home/workfiles/current.txt"
# now both of these work
copyfile(source = src_pathname, dest = dst_pathname)
copyfile(dest = dst_pathname, source = src_pathname)
Using a keyword argument to allow changing a default setting:
function translate(text; language="en")
# translation function body
end
Using a keyword argument to require something:
#=
If the keyword `language` is not specified
when calling this function, a error occurs.
=#
function translate(text; language)
# translation function body
end
Both kinds of argument may have default values to use when an argument is omitted in a function call. All positional arguments that do not specify a default value must preceed all positional arguments that do specify default values. Keyword arguments have the same requirement, any with a default value must follow all keyword arguments that do not specify a default value.
Please see the docs for more information on keyword arguments and optional keyword arguments.
How can I access command line arguments in Nim?
The documentation shows only how to run the compiled Nim code with command line arguments
nim compile --run greetings.nim arg1 arg2
but I didn't find how to use their values in code.
Here's an example that prints the number of arguments and the first argument:
import os
echo paramCount(), " ", paramStr(1)
Personally I find paramCount and paramStr a bit confusing to work with, because the paramCount value differs from C conventions (see the document links).
Fortunately, there are additional convenient functions which do not require to be aware of the conventions:
commandLineParams returns a seq of only command line params.
getAppFilename returns the executable file name (what is argv[0] in the C world).
I have not checked when it was added, but the parseopt seems to me, the default and the best way for this.
commandLineParams isn't available on Posix.
os.commandLineParams() returns a sequence of command-line arguments provided to the program.
os.quoteShellCommand(<openArray[string]>) takes a sequence of command-line arguments and turns it into a single string with quotes around items containing spaces, so the string can be parsed properly.
parseopt.initOptParser(<string>) takes a full command-line string and parses it, returning an OptParser object.
parseopt.getopt(<OptParser>) is an iterator that yields parsed argument info.
You can combine them to parse a program's input arguments:
import std/[os, parseopt]
proc writeHelp() = discard
proc writeVersion() = discard
var positionalArgs = newSeq[string]()
var directories = newSeq[string]()
var optparser = initOptParser(quoteShellCommand(commandLineParams()))
for kind, key, val in optparser.getopt():
case kind
of cmdArgument:
positionalArgs.add(key)
of cmdLongOption, cmdShortOption:
case key
of "help", "h": writeHelp()
of "version", "v": writeVersion()
of "dir", "d":
directories.add(val)
of cmdEnd: assert(false) # cannot happen
echo "positionalArgs: ", positionalArgs
echo "directories: ", directories
Running this with nim c -r main.nim -d:foo --dir:bar dir1 dir2 dir3 prints:
positionalArgs: #["dir1", "dir2", "dir3"]
directories: #["foo", "bar"]
I'm creating a function that displays a lot of variables with the format Variable + Variable Name.
Define LibPub out(list)=
Func
Local X
for x,1,dim(list)
list[x]->name // How can I get the variable name here?
Disp name+list[x]
EndFor
Return 1
EndFunc
Given a list value, there is no way to find its name.
Consider this example:
a:={1,2,3,4}
b:=a ; this stores {1,2,3,4} in b
out(b)
Line 1: First the value {1,2,3,4} is created. Then an variable with name a is created and its value is set to {1,2,3,4}.
Line 2: The expression a is evaluated; the result is {1,2,3,4}. A new variable with the name b is created and its value is set to `{1,2,3,4}.
Line 3: The expression b is evaluated. The variable reference looks up what value is stored in b. The result is {1,2,3,4}. This value is then passed to the function out.
The function out receives the value {1,2,3,4}. Given the value, there is no way of knowing whether the value happened to be stored in a variable. Here the value is stored in both a and b.
However we can also look at out({1,1,1,1}+{0,2,3,4}).
The system will evaluate {1,1,1,1}+{0,2,3,4} and get {1,2,3,4}. Then out is called. The value out received the result of an expression, but an equivalent value happens to be stored in a and b. This means that the values doesn't have a name.
In general: Variables have a name and a value. Values don't have names.
If you need to print a name, then look into strings.
This will be memory intensive, but you could keep a string of variable names, and separate each name by some number of characters and get a substring based on the index of the variable in the list that you want to get. For instance, say you want to access index zero, then you take a substring starting at (index of variable * length of variable name, indexofvariable *length + length+1).
The string will be like this: say you had the variables foo, bas, random, impetus
the string will be stored like so: "foo bas random impetus "
Is there any way to get the value of a variable that has been named in a string.
For instance:
Dim number As Integer = 12
Dim numberCopy As Integer = Convert("number")
' numberCopy will now equal 12
' Convert is the function to convert the string to the variable
Something like that can be done only with Introspection, and only if the variable holding the value is a property of a class. Your example, where the variable is only temporarily known inside a function, cannot be made work as you like.
To learn more about Introspection, see the documentation and examples of the same name.
You can also use a Dictionary instead of straight properties and variables.
For instance
dim d as new dictionary
d.value("number") = 12
Then when you need the value, you can do
Dim numberCopy As Integer = d.value("number")
Nice thing about Dictionary is since keys as well as values are variant, you can throw at it pretty much anything, all variable types, as well as objects.
See Language Reference