<s> from __future__ import absolute_import <EOL> from __future__ import unicode_literals <EOL> from inspect import getdoc <EOL> from docopt import docopt <EOL> from docopt import DocoptExit <EOL> def docopt_full_help ( docstring , * args , ** kwargs ) : <EOL> try : <EOL> return docopt ( docstring , * args , ** kwargs ) <EOL> except DocoptExit : <EOL> raise SystemExit ( docstring ) <EOL> class DocoptDispatcher ( object ) : <EOL> def __init__ ( self , command_class , options ) : <EOL> self . command_class = command_class <EOL> self . options = options <EOL> def parse ( self , argv ) : <EOL> command_help = getdoc ( self . command_class ) <EOL> options = docopt_full_help ( command_help , argv , ** self . options ) <EOL> command = options [ '<STR_LIT>' ] <EOL> if command is None : <EOL> raise SystemExit ( command_help ) <EOL> handler = get_handler ( self . command_class , command ) <EOL> docstring = getdoc ( handler ) <EOL> if docstring is None : <EOL> raise NoSuchCommand ( command , self ) <EOL> command_options = docopt_full_help ( docstring , options [ '<STR_LIT>' ] , options_first = True ) <EOL> return options , handler , command_options <EOL> def get_handler ( command_class , command ) : <EOL> command = command . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> if command == "<STR_LIT>" : <EOL> command = "<STR_LIT>" <EOL> if not hasattr ( command_class , command ) : <EOL> raise NoSuchCommand ( command , command_class ) <EOL> return getattr ( command_class , command ) <EOL> class NoSuchCommand ( Exception ) : <EOL> def __init__ ( self , command , supercommand ) : <EOL> super ( NoSuchCommand , self ) . __init__ ( "<STR_LIT>" % command ) <EOL> self . command = command <EOL> self . supercommand = supercommand </s>
