def parse(flags): """Parse comma separated list of flags.""" state = 0 name = None value = [] tokens = list(FlagsParser.parseString(flags, parseAll=True)) for pos, token in enumerate(tokens): token = token.strip() if state == 0 and token == ",": pass elif state == 0: # Handle aliases name = FLAG_ALIASES.get(token, token) value = [name] state = 1 elif state == 1 and token == ",": # End of flag state = 0 yield name elif state in (1, 3) and token == ":": # Value separator state = 2 elif state == 2 and token == ",": # Flag with empty parameter state = 0 value.append("") yield tuple(value) elif state == 2 and token == ":": # Empty param value.append("") elif state == 2: if (token == "r" and pos + 1 < len(tokens) and tokens[pos + 1] not in (",", ":")): # Regex prefix, value follows state = 4 else: # Value value.append(token) state = 3 elif state == 4: # Regex value value.append(re.compile(token)) state = 3 elif state == 3 and token == ",": # Last value yield tuple(value) state = 0 else: raise ValueError(f"Unexpected token: {token}, state={state}") # With state 0 there was nothing parsed yet if state > 0: if state == 2: # There was empty value value.append("") # Is this flag or flag with value if len(value) > 1: yield tuple(value) else: yield name
def parse(flags): """Parse comma separated list of flags.""" state = 0 name = None value = [] for token in FlagsParser.parseString(flags, parseAll=True): token = token.strip() if state == 0 and token == ",": pass elif state == 0: # Handle aliases if token in FLAG_ALIASES: name = FLAG_ALIASES[token] else: name = token value = [name] state = 1 elif state == 1 and token == ",": # End of flag state = 0 yield name elif state in (1, 3) and token == ":": # Value separator state = 2 elif state == 2 and token == ",": # Flag with empty parameter state = 0 value.append("") yield tuple(value) elif state == 2: # Value value.append(token) state = 3 elif state == 3 and token == ",": # Last value yield tuple(value) state = 0 else: raise ValueError("Unexpected token: {}, state={}".format( token, state)) # With state 0 there was nothing parsed yet if state > 0: if state == 2: # There was empty value value.append("") # Is this flag or flag with value if len(value) > 1: yield tuple(value) else: yield name