Reputation: 65
In a shell script I have:
/usr/local/bin/pybot --variablefile variables.py:$var1:$var2 test_cases.tsv
inside variables.py
how can I access var1
and var2
arguments?
I have tried:
import sys
var1 = sys.argv[1]
var1 = sys.argv[2]
it seems like this doesn't work.
Upvotes: 1
Views: 378
Reputation: 386342
For you to access the variables, your variable file must define the function get_variables
, which will be given the arguments passed from the command line. This function needs to return a dictionary where the keys are the robot variable names.
For example:
def get_variables(arg1, arg2):
variables = {
"var1": arg1,
"var2": arg2
}
return variables
If your variable file is based on a class, the class needs to have the get_variables
method.
For example:
# variables.py
class variables(object):
def get_variables(self, arg1, arg2):
variables = {
"var1": arg1,
"var2": arg2
}
return variables
When you do the above, your test will have two variables set: ${var1}
and ${var2}
which will have the values that were passed via the --variablefile
argument.
Here is a test that can be used to verify the above:
# example.robot
*** Test cases ***
Example
should be equal ${var1} hello
should be equal ${var2} world
Here is how to run the test in order for it to pass:
$ var1=hello
$ var2=world
$ /usr/local/bin/pybot --variablefile variables.py:$var1:$var2 example.robot
Of course, var1
and var2
are completely arbitrary. You can pass raw strings, too:
$ /usr/local/bin/pybot --variablefile variables.py:hello:world example.robot
Passing arguments is described in the user guide section titled Getting variables from a special function
Upvotes: 2
Reputation: 189830
You seem to make assumptions about how the arguments are parsed which are not true. Here's how these arguments are passed from the shell to Python:
sys.argv[0]
is /usr/local/bin/pybot
sys.argv[1]
is --variablefile
sys.argv[2]
is variables.py:$var1:$var2
where the values of the shell variables var1
and var2
are substituted.sys.argv[n]
is test_cases.tsv
The last one is [n]
because without quotes around the argument, sys.argv[2]
might actually be split into multiple values. For example, if var1
contains = foo * bar=
then actually
sys.argv[2]
is variables.py:=
sys.argv[3]
is foo
sys.argv[4..n-2]
is a list of files in the current directory, andsys.argv[n-1]
is =bar:$var2
where similar further processing for the value of var2
may take place.There are Python argument parsing modules which assign further semantics e.g. to arguments which start with a dash (these will be interpreted as options) but by itself, Python does no such thing. If that's what you want, maybe look at argparse
or one of its replacements; but you still need to understand how the basic mechanics work. A common arrangement is to avoid internal structure in arguments, and instead require the user to pass each value as a separate argument -- so perhaps
--variablefile variables.py --variablefile "$var1" --variablefile "$var2"
with quoting to prevent the shell from attempting to perform whitespace tokenization and wildcard expansion on the variable values, and then probably in your script an argparse
definition which says to merge multiple option arguments into a list.
parser = argparse.ArgumentParser()
parser.add_argument('--variablefile', action='append')
Upvotes: 0
Reputation: 280
sys
reads the arguments fron the command line, as they appears to it:
sys.argv[0]
contains the script namesys.argv[1]
, the first argument (whatever it is)sys.argv[2]
, the second, and so on.You should use argparse
, it helps to build comprehensive CLIs. A nice tutorial exists on the Python website.
Upvotes: 0