Skip to content

Commit 7781d8f

Browse files
committed
Command line variable conversion
Fixes #2946. Documentation missing, but it will be done as part of documenting variable conversion in general (#3278).
1 parent 5b121be commit 7781d8f

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

atest/robot/variables/variable_types.robot

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
*** Settings ***
2-
Suite Setup Run Tests ${EMPTY} variables/variable_types.robot
2+
Suite Setup Run Tests -v "CLI: date:2025-05-20" -v NOT:INT:1 variables/variable_types.robot
33
Resource atest_resource.robot
4+
Resource ../cli/runner/cli_resource.robot
45

56
*** Test Cases ***
7+
Command line
8+
Check Test Case ${TESTNAME}
9+
610
Variable section
711
Check Test Case ${TESTNAME}
812

@@ -145,15 +149,15 @@ User keyword: Invalid value
145149
User keyword: Invalid type
146150
Check Test Case ${TESTNAME}
147151
Error In File
148-
... 0 variables/variable_types.robot 471
152+
... 0 variables/variable_types.robot 475
149153
... Creating keyword 'Bad type' failed:
150154
... Invalid argument specification: Invalid argument '\${arg: bad}':
151155
... Unrecognized type 'bad'.
152156

153157
User keyword: Invalid assignment with kwargs k_type=v_type declaration
154158
Check Test Case ${TESTNAME}
155159
Error In File
156-
... 1 variables/variable_types.robot 475
160+
... 1 variables/variable_types.robot 479
157161
... Creating keyword 'Kwargs does not support key=value type syntax' failed:
158162
... Invalid argument specification: Invalid argument '\&{kwargs: int=float}':
159163
... Unrecognized type 'int=float'.
@@ -173,7 +177,7 @@ Embedded arguments: Invalid value from variable
173177
Embedded arguments: Invalid type
174178
Check Test Case ${TESTNAME}
175179
Error In File
176-
... 2 variables/variable_types.robot 495
180+
... 2 variables/variable_types.robot 499
177181
... Creating keyword 'Embedded invalid type \${x: invalid}' failed:
178182
... Invalid embedded argument '\${x: invalid}':
179183
... Unrecognized type 'invalid'.
@@ -216,3 +220,13 @@ Inline IF
216220

217221
Set global/suite/test/local variable: No support
218222
Check Test Case ${TESTNAME}
223+
224+
Invalid value on CLI
225+
Run Should Fail
226+
... -v "BAD_VALUE: int:bad" ${DATADIR}/misc/pass_and_fail.robot
227+
... Command line variable '\${BAD_VALUE: int}' got value 'bad' that cannot be converted to integer.
228+
229+
Invalid type on CLI
230+
Run Should Fail
231+
... -v "BAD TYPE: bad:whatever" ${DATADIR}/misc/pass_and_fail.robot
232+
... Invalid command line variable '\${BAD TYPE: bad}': Unrecognized type 'bad'.

atest/testdata/variables/variable_types.robot

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ ${${NAME}} 42
2727

2828

2929
*** Test Cases ***
30+
Command line
31+
Should Be Equal ${CLI} 2025-05-20 type=date
32+
Should Be Equal ${NOT} INT:1
33+
3034
Variable section
31-
Should be equal ${INTEGER} ${42}
35+
Should be equal ${INTEGER} 42 type=int
3236
Variable should not exist ${INTEGER: int}
3337
Should be equal ${INT_LIST} [42, 1] type=list
3438
Variable should not exist ${INT_LIST: list[int]}

src/robot/variables/scopes.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
# limitations under the License.
1515

1616
import os
17+
import re
1718
import tempfile
1819

20+
from robot.errors import DataError
1921
from robot.model import Tags
2022
from robot.output import LOGGER
2123
from robot.utils import abspath, DotDict, find_file, get_error_details, NormalizedDict
@@ -191,12 +193,29 @@ def _set_cli_variables(self, settings):
191193
LOGGER.error(msg)
192194
LOGGER.info(details)
193195
for varstr in settings.variables:
194-
try:
196+
match = re.fullmatch("([^:]+): ([^:]+):(.*)", varstr)
197+
if match:
198+
name, typ, value = match.groups()
199+
value = self._convert_cli_variable(name, typ, value)
200+
elif ":" in varstr:
195201
name, value = varstr.split(":", 1)
196-
except ValueError:
202+
else:
197203
name, value = varstr, ""
198204
self[f"${{{name}}}"] = value
199205

206+
def _convert_cli_variable(self, name, typ, value):
207+
from robot.running import TypeInfo
208+
209+
var = f"${{{name}: {typ}}}"
210+
try:
211+
info = TypeInfo.from_variable(var)
212+
except DataError as err:
213+
raise DataError(f"Invalid command line variable '{var}': {err}")
214+
try:
215+
return info.convert(value, var, kind="Command line variable")
216+
except ValueError as err:
217+
raise DataError(err)
218+
200219
def _set_built_in_variables(self, settings):
201220
options = DotDict(
202221
rpa=settings.rpa,

0 commit comments

Comments
 (0)