Skip to content

Commit

Permalink
Add Statement.get_option() helper.
Browse files Browse the repository at this point in the history
Makes it easier to get parsed options from statements. It's especially
useful if a statement can have multiple options like FOR IN ZIP can
soonish (#4682) have `mode` and `fill`.
  • Loading branch information
pekkaklarck committed Mar 13, 2023
1 parent 41db927 commit d56637d
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/robot/parsing/model/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ def get_values(self, *types):
"""Return values of tokens having any of the given ``types``."""
return tuple(t.value for t in self.tokens if t.type in types)

def get_option(self, name):
options = dict(opt.split('=', 1) for opt in self.get_values(Token.OPTION))
return options.get(name)

@property
def lines(self):
line = []
Expand Down Expand Up @@ -821,11 +825,7 @@ def flavor(self):

@property
def start(self):
if self.flavor == 'IN ENUMERATE':
value = self.get_value(Token.OPTION)
if value:
return value[len('start='):]
return None
return self.get_option('start') if self.flavor == 'IN ENUMERATE' else None

def validate(self, ctx: 'ValidationContext'):
if not self.variables:
Expand Down Expand Up @@ -969,8 +969,7 @@ def patterns(self):

@property
def pattern_type(self):
value = self.get_value(Token.OPTION)
return value[len('type='):] if value else None
return self.get_option('type')

@property
def variable(self):
Expand Down Expand Up @@ -1021,8 +1020,7 @@ def condition(self):

@property
def limit(self):
value = self.get_value(Token.OPTION)
return value[len('limit='):] if value else None
return self.get_option('limit')

def validate(self, ctx: 'ValidationContext'):
values = self.get_values(Token.ARGUMENT)
Expand Down

0 comments on commit d56637d

Please sign in to comment.