Skip to main content

withOption

The withOption functions change the behavior of a block pattern.

withOption(pattern: PatternBlocks<T>, option: UnmatchedConfigOption) : PatternBlocks<T>

withOption(pattern, option) returns a new pattern that is identical to pattern, but which uses the specified option for handling unmatched configuration blocks. For example, to specify that pattern should only match if there is no unmatched config lines, use withOption(pattern, UnmatchedConfigOption.FORBID).

Note that block patterns constructed using block pattern literal syntax or via the blockPattern function use UnmatchedConfigOption.ALLOW option. In other words, by default, block patterns match their subpatterns against configuration lines, allowing for extra lines to be present in the configuration.

withOption(pattern: PatternBlocks<T>, option: LineMatchingOption) : PatternBlocks<T>

withOption(pattern, option) can be used to obtain a new pattern that is identical to pattern, but which uses the specified option when matching individual pattern lines to config lines. For example, to specify that each line of pattern should only match a config line if it completely matches the line (not just a prefix of the line), use withOption(pattern, LineMatchingOption.COMPLETE).

Note that block patterns constructed using block pattern literal syntax or via the blockPattern function use LineMatchingOption.PREFIX option. In other words, by default, block pattern lines match against lines of configuration as long as the pattern line matches a prefix of the configuration line.

A line in a block pattern literal that ends with {eof} will only match configuration lines where every part of the configuration line is matched up with elements in the block pattern line. If the block pattern line does not end with {eof} then the block pattern line can match configuration lines which have a prefix that matches the block pattern line. In other words, pattern1 and pattern2, defined below are equivalent:

pattern1 = ```
interface {string} {eof}
switchport access vlan {number} {eof}
```;

pattern2 = withOption(```
interface {string}
switchport access vlan {number}
```, LineMatchingOption.COMPLETE);

See also

Functions