<s> import argparse <EOL> import sys <EOL> import traceback <EOL> from . command import Command <EOL> class HelpAction ( argparse . Action ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , parser , namespace , values , option_string = None ) : <EOL> app = self . default <EOL> parser . print_help ( app . stdout ) <EOL> app . stdout . write ( '<STR_LIT>' ) <EOL> command_manager = app . command_manager <EOL> for name , ep in sorted ( command_manager ) : <EOL> try : <EOL> factory = ep . load ( ) <EOL> except Exception as err : <EOL> app . stdout . write ( '<STR_LIT>' % ep ) <EOL> if namespace . debug : <EOL> traceback . print_exc ( file = app . stdout ) <EOL> continue <EOL> try : <EOL> cmd = factory ( app , None ) <EOL> if cmd . deprecated : <EOL> continue <EOL> except Exception as err : <EOL> app . stdout . write ( '<STR_LIT>' % ( ep , err ) ) <EOL> if namespace . debug : <EOL> traceback . print_exc ( file = app . stdout ) <EOL> continue <EOL> one_liner = cmd . get_description ( ) . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> app . stdout . write ( '<STR_LIT>' % ( name , one_liner ) ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> class HelpCommand ( Command ) : <EOL> """<STR_LIT>""" <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( HelpCommand , self ) . get_parser ( prog_name ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> nargs = '<STR_LIT:*>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> return parser <EOL> def take_action ( self , parsed_args ) : <EOL> if parsed_args . cmd : <EOL> try : <EOL> the_cmd = self . app . command_manager . find_command ( <EOL> parsed_args . cmd , <EOL> ) <EOL> cmd_factory , cmd_name , search_args = the_cmd <EOL> except ValueError : <EOL> cmd = parsed_args . cmd [ <NUM_LIT:0> ] <EOL> fuzzy_matches = [ k [ <NUM_LIT:0> ] for k in self . app . command_manager <EOL> if k [ <NUM_LIT:0> ] . startswith ( cmd ) <EOL> ] <EOL> if not fuzzy_matches : <EOL> raise <EOL> self . app . stdout . write ( '<STR_LIT>' % cmd ) <EOL> for fm in sorted ( fuzzy_matches ) : <EOL> self . app . stdout . write ( '<STR_LIT>' % fm ) <EOL> return <EOL> self . app_args . cmd = search_args <EOL> cmd = cmd_factory ( self . app , self . app_args ) <EOL> full_name = ( cmd_name <EOL> if self . app . interactive_mode <EOL> else '<STR_LIT:U+0020>' . join ( [ self . app . NAME , cmd_name ] ) <EOL> ) <EOL> cmd_parser = cmd . get_parser ( full_name ) <EOL> else : <EOL> action = HelpAction ( None , None , default = self . app ) <EOL> action ( self . app . parser , self . app . parser , None , None ) <EOL> cmd_parser . print_help ( self . app . stdout ) <EOL> return <NUM_LIT:0> </s>
