<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from argparse import SUPPRESS , ArgumentParser <EOL> import re <EOL> from io import BytesIO <EOL> import logger <EOL> logger = logger . get ( ) . getLogger ( __name__ ) <EOL> class LinotParser ( ArgumentParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> ArgumentParser . __init__ ( self , * args , ** kwargs ) <EOL> self . _sub_parser = None <EOL> self . _direct_commands = [ ] <EOL> def get_sub_parser ( self ) : <EOL> if self . _sub_parser is None : <EOL> self . _sub_parser = self . add_subparsers ( ) <EOL> return self . _sub_parser <EOL> def add_direct_command ( self , func , pattern , flags = <NUM_LIT:0> ) : <EOL> self . _direct_commands . append ( [ func , pattern , flags ] ) <EOL> def process_direct_commands ( self , cmd , sender ) : <EOL> matched = False <EOL> for direct_command in self . _direct_commands : <EOL> match_list = re . findall ( direct_command [ <NUM_LIT:1> ] , cmd , direct_command [ <NUM_LIT:2> ] ) <EOL> if len ( match_list ) > <NUM_LIT:0> : <EOL> matched = True <EOL> direct_command [ <NUM_LIT:0> ] ( match_list , cmd , sender ) <EOL> return matched <EOL> class LinotArgParser : <EOL> def __init__ ( self , subcmd , parser , default_process ) : <EOL> self . _parser = parser <EOL> sub_cmd_parser = parser . get_sub_parser ( ) <EOL> ap = sub_cmd_parser . add_parser ( subcmd , add_help = False ) <EOL> self . _sub_parser = ap . add_mutually_exclusive_group ( ) <EOL> self . _subcmd = subcmd <EOL> self . _default_process = default_process <EOL> self . _arg_list = { } <EOL> self . _sub_parser . set_defaults ( proc = self . _process_args ) <EOL> self . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , func = self . print_help , help = '<STR_LIT>' ) <EOL> def add_direct_command ( self , func , pattern , flags = <NUM_LIT:0> ) : <EOL> self . _parser . add_direct_command ( func , pattern , flags ) <EOL> def add_argument ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in kwargs : <EOL> func = kwargs [ '<STR_LIT>' ] <EOL> kwargs . pop ( '<STR_LIT>' ) <EOL> else : <EOL> func = None <EOL> for arg_str in args : <EOL> if not arg_str . startswith ( self . _sub_parser . prefix_chars ) : <EOL> raise ValueError ( '<STR_LIT>' + <EOL> self . _sub_parser . prefix_chars ) <EOL> option = arg_str . lstrip ( self . _sub_parser . prefix_chars ) <EOL> self . _arg_list [ option ] = { } <EOL> self . _arg_list [ option ] [ '<STR_LIT>' ] = func <EOL> self . _arg_list [ option ] [ '<STR_LIT>' ] = arg_str <EOL> if '<STR_LIT>' in kwargs : <EOL> self . _arg_list [ option ] [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> self . _arg_list [ option ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . _sub_parser . add_argument ( * args , ** kwargs ) <EOL> def _process_args ( self , input_args , sender ) : <EOL> for args in self . _arg_list : <EOL> value = getattr ( input_args , args , None ) <EOL> if value is not None and value is not False : <EOL> func = self . _arg_list [ args ] [ '<STR_LIT>' ] <EOL> if func is not None : <EOL> func ( getattr ( input_args , args ) , sender ) <EOL> else : <EOL> self . _default_process ( input_args , sender ) <EOL> return <EOL> self . _default_process ( None , sender ) <EOL> def print_help ( self , args , sender = None ) : <EOL> msg = BytesIO ( ) <EOL> print ( '<STR_LIT>' . format ( self . _subcmd ) , file = msg ) <EOL> print ( '<STR_LIT>' . format ( self . _subcmd , '<STR_LIT>' ) , file = msg ) <EOL> print ( '<STR_LIT>' , file = msg ) <EOL> print ( '<STR_LIT>' , file = msg ) <EOL> for arg in self . _arg_list : <EOL> if self . _arg_list [ arg ] [ '<STR_LIT>' ] is SUPPRESS : <EOL> continue <EOL> if arg == '<STR_LIT>' or arg == '<STR_LIT:h>' : <EOL> continue <EOL> help_text = self . _arg_list [ arg ] [ '<STR_LIT>' ] <EOL> print ( '<STR_LIT>' . format ( self . _subcmd , self . _arg_list [ arg ] [ '<STR_LIT>' ] ) , file = msg ) <EOL> if help_text != '<STR_LIT>' : <EOL> print ( '<STR_LIT>' + help_text , file = msg ) <EOL> print ( '<STR_LIT>' , file = msg ) <EOL> if sender is not None : <EOL> sender . send_message ( msg . getvalue ( ) ) </s>
