1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
'''Functions for parsing the `when` attribute.'''
import shlex
from .errors import CrazyError
# pylint: disable=too-few-public-methods
class OptionIs:
'''Class for holding `option_is`.'''
def __init__(self, args):
self.options = []
self.values = []
has_end_of_options = False
for arg in args:
if arg == '--':
has_end_of_options = True
elif not has_end_of_options:
self.options.append(arg)
elif has_end_of_options:
self.values.append(arg)
if not self.options:
raise CrazyError('OptionIs: Empty options')
if not self.values:
raise CrazyError('OptionIs: Empty values')
class HasOption:
'''Class for holding `has_option`.'''
def __init__(self, args):
self.options = args
if not self.options:
raise CrazyError('HasOption: Empty options')
def parse_when(s):
'''Parse `when` string and return an object.'''
split = shlex.split(s)
if not split:
raise CrazyError("parse_when: Empty arguments")
cmd = split[0]
del split[0]
if cmd == 'option_is':
return OptionIs(split)
if cmd == 'has_option':
return HasOption(split)
raise CrazyError("Invalid command: %r" % cmd)
|