<s> import re <EOL> import os <EOL> from . exceptions import CleoException <EOL> from . inputs . input_argument import InputArgument <EOL> from . inputs . input_option import InputOption <EOL> class Parser ( object ) : <EOL> @ classmethod <EOL> def parse ( cls , expression ) : <EOL> """<STR_LIT>""" <EOL> parsed = { <EOL> '<STR_LIT:name>' : None , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> if not expression . strip ( ) : <EOL> raise CleoException ( '<STR_LIT>' ) <EOL> expression = expression . replace ( os . linesep , '<STR_LIT>' ) <EOL> matches = re . match ( '<STR_LIT>' , expression ) <EOL> if not matches : <EOL> raise CleoException ( '<STR_LIT>' ) <EOL> name = matches . group ( <NUM_LIT:0> ) <EOL> parsed [ '<STR_LIT:name>' ] = name <EOL> tokens = re . findall ( '<STR_LIT>' , expression ) <EOL> if tokens : <EOL> parsed . update ( cls . _parameters ( tokens ) ) <EOL> return parsed <EOL> @ classmethod <EOL> def _parameters ( cls , tokens ) : <EOL> """<STR_LIT>""" <EOL> arguments = [ ] <EOL> options = [ ] <EOL> for token in tokens : <EOL> if not token . startswith ( '<STR_LIT>' ) : <EOL> arguments . append ( cls . _parse_argument ( token ) ) <EOL> else : <EOL> options . append ( cls . _parse_option ( token ) ) <EOL> return { <EOL> '<STR_LIT>' : arguments , <EOL> '<STR_LIT>' : options <EOL> } <EOL> @ classmethod <EOL> def _parse_argument ( cls , token ) : <EOL> """<STR_LIT>""" <EOL> description = None <EOL> validator = None <EOL> if '<STR_LIT>' in token : <EOL> token , description = tuple ( token . split ( '<STR_LIT>' , <NUM_LIT:2> ) ) <EOL> token = token . strip ( ) <EOL> description = description . strip ( ) <EOL> matches = re . match ( '<STR_LIT>' , token ) <EOL> if matches : <EOL> token = matches . group ( <NUM_LIT:1> ) . strip ( ) <EOL> validator = matches . group ( <NUM_LIT:2> ) . strip ( ) <EOL> if token . endswith ( '<STR_LIT>' ) : <EOL> return InputArgument ( <EOL> token . rstrip ( '<STR_LIT>' ) , <EOL> InputArgument . IS_LIST , <EOL> description <EOL> ) <EOL> elif token . endswith ( '<STR_LIT:*>' ) : <EOL> return InputArgument ( <EOL> token . rstrip ( '<STR_LIT:*>' ) , <EOL> InputArgument . IS_LIST | InputArgument . REQUIRED , <EOL> description <EOL> ) <EOL> elif token . endswith ( '<STR_LIT:?>' ) : <EOL> return InputArgument ( <EOL> token . rstrip ( '<STR_LIT:?>' ) , <EOL> InputArgument . OPTIONAL , <EOL> description <EOL> ) <EOL> matches = re . match ( '<STR_LIT>' , token ) <EOL> if matches : <EOL> return InputArgument ( <EOL> matches . group ( <NUM_LIT:1> ) , <EOL> InputArgument . OPTIONAL , <EOL> description , <EOL> matches . group ( <NUM_LIT:2> ) <EOL> ) <EOL> return InputArgument ( token , InputArgument . REQUIRED , description , <EOL> validator = validator ) <EOL> @ classmethod <EOL> def _parse_option ( cls , token ) : <EOL> """<STR_LIT>""" <EOL> description = None <EOL> validator = None <EOL> if '<STR_LIT>' in token : <EOL> token , description = tuple ( token . split ( '<STR_LIT>' , <NUM_LIT:2> ) ) <EOL> token = token . strip ( ) <EOL> description = description . strip ( ) <EOL> matches = re . match ( '<STR_LIT>' , token ) <EOL> if matches : <EOL> token = matches . group ( <NUM_LIT:1> ) . strip ( ) <EOL> validator = matches . group ( <NUM_LIT:2> ) . strip ( ) <EOL> shortcut = None <EOL> matches = re . split ( '<STR_LIT>' , token , <NUM_LIT:2> ) <EOL> if len ( matches ) > <NUM_LIT:1> : <EOL> shortcut = matches [ <NUM_LIT:0> ] . lstrip ( '<STR_LIT:->' ) <EOL> token = matches [ <NUM_LIT:1> ] <EOL> default = None <EOL> mode = InputOption . VALUE_NONE <EOL> if token . endswith ( '<STR_LIT>' ) : <EOL> mode = InputOption . VALUE_REQUIRED | InputOption . VALUE_IS_LIST <EOL> token = token . rstrip ( '<STR_LIT>' ) <EOL> elif token . endswith ( '<STR_LIT>' ) : <EOL> mode = InputOption . VALUE_OPTIONAL | InputOption . VALUE_IS_LIST <EOL> token = token . rstrip ( '<STR_LIT>' ) <EOL> elif token . endswith ( "<STR_LIT>" ) : <EOL> mode = InputOption . VALUE_OPTIONAL <EOL> token = token . rstrip ( '<STR_LIT>' ) <EOL> elif token . endswith ( '<STR_LIT:=>' ) : <EOL> mode = InputOption . VALUE_REQUIRED <EOL> token = token . rstrip ( '<STR_LIT:=>' ) <EOL> matches = re . match ( '<STR_LIT>' , token ) <EOL> if matches : <EOL> token = matches . group ( <NUM_LIT:1> ) <EOL> operator = matches . group ( <NUM_LIT:2> ) <EOL> default = matches . group ( <NUM_LIT:3> ) <EOL> if operator == '<STR_LIT>' : <EOL> mode = InputOption . VALUE_REQUIRED | InputOption . VALUE_IS_LIST <EOL> elif operator == '<STR_LIT>' : <EOL> mode = InputOption . VALUE_OPTIONAL | InputOption . VALUE_IS_LIST <EOL> elif operator == '<STR_LIT>' : <EOL> mode = InputOption . VALUE_OPTIONAL <EOL> elif operator == '<STR_LIT:=>' : <EOL> mode = InputOption . VALUE_REQUIRED <EOL> return InputOption ( token , shortcut , mode , description , default , <EOL> validator = validator ) </s>
