1

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.

3
  • As an aside, you should make sure /usr/local/bin is in your PATH and not hard-code the path to pybot in the script. Commented Dec 13, 2017 at 10:42
  • @tripleee: I think should is a bit strong. It's convenient, but it doesn't really affect the running of the command at all. Commented Dec 14, 2017 at 1:19
  • It affects the usability of the script. A common technique is to put something in your personal PATH which shadows a command used by a system script in order to extend or debug it; hard-coding the path prevents that from working. Commented Dec 14, 2017 at 4:42

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

I want to use the values of $var1 and $var2 inside the python file itself. Say if I call get_variables() function like this -> vars = get_variables(?, ?) ... what argumetns should I pass to the function to get the values of $var1 and $var2 ?
"I want to use the values of $var1 and $var2 inside the python file itself." - my code shows you how to use them. They are being used to create the dictionary, but you can use them any other way. There's nothing special about them. Whatever you pass on the command line become arguments to get_variables. It's not any more complicated than that.
If I want to use values of dictionary, do I need to call the function or not? if not how can I access the dictionary values?
@Rashid: you don't have to use the dictionary. Just use the variables. If you want the variables available to a test script, you have to return a dictionary.
if you could show me how to use arg1, arg2 values outside of get_variable function, would be great. Thanks for the help
|
0

sys reads the arguments fron the command line, as they appears to it:

  • sys.argv[0] contains the script name
  • sys.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.

4 Comments

"You should use argparse" is IMHO a bit too strong. It's part of the standard library and the OP should probably use something like it; but there is a reason there are many competing third-party replacements and add-ons -- argparse by itself is complex and clunky at the same time IMHO.
You are right, @tripleee; argparse needs time to get understood. But, within the Python argument parsers, I know no package that equals it.
It adds a lot of complexity to support use cases which were hard to cover with the old optparse but if that's not what you need, a much simpler library would be much easier to grasp and take into use. I'm hoping there will be a common and popular argparse wrapper for the simple use case of "I need a couple of --option arguments" but as of right now, I can't point to any good one.
robot supports passing arguments to variable files. There's no need to use sys.argv and manually parse the values.
0

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, and
  • sys.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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.