<s> from __future__ import division , print_function , absolute_import <EOL> import os <EOL> import sys <EOL> import functools <EOL> from textwrap import TextWrapper <EOL> from collections import defaultdict <EOL> from plumbum . lib import six , getdoc <EOL> from plumbum . cli . terminal import get_terminal_size <EOL> from plumbum . cli . switches import ( SwitchError , UnknownSwitch , MissingArgument , WrongArgumentType , <EOL> MissingMandatorySwitch , SwitchCombinationError , PositionalArgumentsError , switch , <EOL> SubcommandError , Flag , CountOf ) <EOL> from plumbum import colors , local <EOL> class ShowHelp ( SwitchError ) : <EOL> pass <EOL> class ShowHelpAll ( SwitchError ) : <EOL> pass <EOL> class ShowVersion ( SwitchError ) : <EOL> pass <EOL> class SwitchParseInfo ( object ) : <EOL> __slots__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:index>" , "<STR_LIT>" ] <EOL> def __init__ ( self , swname , val , index ) : <EOL> self . swname = swname <EOL> self . val = val <EOL> self . index = index <EOL> class Subcommand ( object ) : <EOL> def __init__ ( self , name , subapplication ) : <EOL> self . name = name <EOL> self . subapplication = subapplication <EOL> def get ( self ) : <EOL> if isinstance ( self . subapplication , str ) : <EOL> modname , clsname = self . subapplication . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> mod = __import__ ( modname , None , None , "<STR_LIT:*>" ) <EOL> try : <EOL> cls = getattr ( mod , clsname ) <EOL> except AttributeError : <EOL> raise ImportError ( "<STR_LIT>" % ( clsname , ) ) <EOL> self . subapplication = cls <EOL> return self . subapplication <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . subapplication ) <EOL> class Application ( object ) : <EOL> """<STR_LIT>""" <EOL> PROGNAME = None <EOL> DESCRIPTION = None <EOL> VERSION = None <EOL> USAGE = None <EOL> COLOR_USAGE = None <EOL> COLOR_GROUPS = None <EOL> CALL_MAIN_IF_NESTED_COMMAND = True <EOL> SUBCOMMAND_HELPMSG = "<STR_LIT>" <EOL> parent = None <EOL> nested_command = None <EOL> _unbound_switches = ( ) <EOL> def __new__ ( cls , executable = None ) : <EOL> """<STR_LIT>""" <EOL> if executable is None : <EOL> return cls . run ( ) <EOL> else : <EOL> return super ( Application , cls ) . __new__ ( cls ) <EOL> def __init__ ( self , executable ) : <EOL> if self . PROGNAME is None : <EOL> self . PROGNAME = os . path . basename ( executable ) <EOL> elif isinstance ( self . PROGNAME , colors . _style ) : <EOL> self . PROGNAME = self . PROGNAME | os . path . basename ( executable ) <EOL> elif colors . filter ( self . PROGNAME ) == '<STR_LIT>' : <EOL> self . PROGNAME = colors . extract ( self . PROGNAME ) | os . path . basename ( executable ) <EOL> if self . DESCRIPTION is None : <EOL> self . DESCRIPTION = getdoc ( self ) <EOL> self . COLOR_GROUPS = defaultdict ( lambda : colors . do_nothing , dict ( ) if type ( self ) . COLOR_GROUPS is None else type ( self ) . COLOR_GROUPS ) <EOL> if type ( self ) . COLOR_USAGE is None : <EOL> self . COLOR_USAGE = colors . do_nothing <EOL> self . executable = executable <EOL> self . _switches_by_name = { } <EOL> self . _switches_by_func = { } <EOL> self . _switches_by_envar = { } <EOL> self . _subcommands = { } <EOL> for cls in reversed ( type ( self ) . mro ( ) ) : <EOL> for obj in cls . __dict__ . values ( ) : <EOL> if isinstance ( obj , Subcommand ) : <EOL> name = colors . filter ( obj . name ) <EOL> if name . startswith ( "<STR_LIT:->" ) : <EOL> raise SubcommandError ( "<STR_LIT>" ) <EOL> self . _subcommands [ name ] = obj <EOL> continue <EOL> swinfo = getattr ( obj , "<STR_LIT>" , None ) <EOL> if not swinfo : <EOL> continue <EOL> for name in swinfo . names : <EOL> if name in self . _unbound_switches : <EOL> continue <EOL> if name in self . _switches_by_name and not self . _switches_by_name [ name ] . overridable : <EOL> raise SwitchError ( "<STR_LIT>" % ( name , ) ) <EOL> self . _switches_by_name [ name ] = swinfo <EOL> self . _switches_by_func [ swinfo . func ] = swinfo <EOL> if swinfo . envname : <EOL> self . _switches_by_envar [ swinfo . envname ] = swinfo <EOL> @ property <EOL> def root_app ( self ) : <EOL> return self . parent . root_app if self . parent else self <EOL> @ classmethod <EOL> def unbind_switches ( cls , * switch_names ) : <EOL> """<STR_LIT>""" <EOL> cls . _unbound_switches += tuple ( name . lstrip ( "<STR_LIT:->" ) for name in switch_names if name ) <EOL> @ classmethod <EOL> def subcommand ( cls , name , subapp = None ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( subapp ) : <EOL> attrname = "<STR_LIT>" % ( subapp if isinstance ( subapp , str ) else subapp . __name__ , ) <EOL> setattr ( cls , attrname , Subcommand ( name , subapp ) ) <EOL> return subapp <EOL> return wrapper ( subapp ) if subapp else wrapper <EOL> def _parse_args ( self , argv ) : <EOL> tailargs = [ ] <EOL> swfuncs = { } <EOL> index = <NUM_LIT:0> <EOL> while argv : <EOL> index += <NUM_LIT:1> <EOL> a = argv . pop ( <NUM_LIT:0> ) <EOL> val = None <EOL> if a == "<STR_LIT>" : <EOL> tailargs . extend ( argv ) <EOL> break <EOL> if a in self . _subcommands : <EOL> subcmd = self . _subcommands [ a ] . get ( ) <EOL> self . nested_command = ( subcmd , [ self . PROGNAME + "<STR_LIT:U+0020>" + self . _subcommands [ a ] . name ] + argv ) <EOL> break <EOL> elif a . startswith ( "<STR_LIT>" ) and len ( a ) >= <NUM_LIT:3> : <EOL> eqsign = a . find ( "<STR_LIT:=>" ) <EOL> if eqsign >= <NUM_LIT:0> : <EOL> name = a [ <NUM_LIT:2> : eqsign ] <EOL> argv . insert ( <NUM_LIT:0> , a [ eqsign : ] ) <EOL> else : <EOL> name = a [ <NUM_LIT:2> : ] <EOL> swname = "<STR_LIT>" + name <EOL> if name not in self . _switches_by_name : <EOL> raise UnknownSwitch ( "<STR_LIT>" % ( swname , ) ) <EOL> swinfo = self . _switches_by_name [ name ] <EOL> if swinfo . argtype : <EOL> if not argv : <EOL> raise MissingArgument ( "<STR_LIT>" % ( swname , ) ) <EOL> a = argv . pop ( <NUM_LIT:0> ) <EOL> if a and a [ <NUM_LIT:0> ] == "<STR_LIT:=>" : <EOL> if len ( a ) >= <NUM_LIT:2> : <EOL> val = a [ <NUM_LIT:1> : ] <EOL> else : <EOL> if not argv : <EOL> raise MissingArgument ( "<STR_LIT>" % ( swname ) ) <EOL> val = argv . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> val = a <EOL> elif a . startswith ( "<STR_LIT:->" ) and len ( a ) >= <NUM_LIT:2> : <EOL> name = a [ <NUM_LIT:1> ] <EOL> swname = "<STR_LIT:->" + name <EOL> if name not in self . _switches_by_name : <EOL> raise UnknownSwitch ( "<STR_LIT>" % ( swname , ) ) <EOL> swinfo = self . _switches_by_name [ name ] <EOL> if swinfo . argtype : <EOL> if len ( a ) >= <NUM_LIT:3> : <EOL> val = a [ <NUM_LIT:2> : ] <EOL> else : <EOL> if not argv : <EOL> raise MissingArgument ( "<STR_LIT>" % ( swname , ) ) <EOL> val = argv . pop ( <NUM_LIT:0> ) <EOL> elif len ( a ) >= <NUM_LIT:3> : <EOL> argv . insert ( <NUM_LIT:0> , "<STR_LIT:->" + a [ <NUM_LIT:2> : ] ) <EOL> else : <EOL> if a . startswith ( "<STR_LIT:->" ) : <EOL> raise UnknownSwitch ( "<STR_LIT>" % ( a , ) ) <EOL> tailargs . append ( a ) <EOL> continue <EOL> val = self . _handle_argument ( val , swinfo . argtype , name ) <EOL> if swinfo . func in swfuncs : <EOL> if swinfo . list : <EOL> swfuncs [ swinfo . func ] . val [ <NUM_LIT:0> ] . append ( val ) <EOL> else : <EOL> if swfuncs [ swinfo . func ] . swname == swname : <EOL> raise SwitchError ( "<STR_LIT>" % ( swname , ) ) <EOL> else : <EOL> raise SwitchError ( "<STR_LIT>" % ( <EOL> swfuncs [ swinfo . func ] . swname , swname ) ) <EOL> else : <EOL> if swinfo . list : <EOL> swfuncs [ swinfo . func ] = SwitchParseInfo ( swname , ( [ val ] , ) , index ) <EOL> elif val is NotImplemented : <EOL> swfuncs [ swinfo . func ] = SwitchParseInfo ( swname , ( ) , index ) <EOL> else : <EOL> swfuncs [ swinfo . func ] = SwitchParseInfo ( swname , ( val , ) , index ) <EOL> envindex = <NUM_LIT:0> <EOL> for env , swinfo in self . _switches_by_envar . items ( ) : <EOL> envindex -= <NUM_LIT:1> <EOL> envval = local . env . get ( env ) <EOL> if envval is None : <EOL> continue <EOL> if swinfo . func in swfuncs : <EOL> continue <EOL> val = self . _handle_argument ( envval , swinfo . argtype , env ) <EOL> envname = "<STR_LIT>" % ( env , ) <EOL> if swinfo . list : <EOL> swfuncs [ swinfo . func ] = SwitchParseInfo ( envname , ( [ val ] , ) , envindex ) <EOL> elif val is NotImplemented : <EOL> swfuncs [ swinfo . func ] = SwitchParseInfo ( envname , ( ) , envindex ) <EOL> else : <EOL> swfuncs [ swinfo . func ] = SwitchParseInfo ( envname , ( val , ) , envindex ) <EOL> return swfuncs , tailargs <EOL> @ classmethod <EOL> def autocomplete ( cls , argv ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ staticmethod <EOL> def _handle_argument ( val , argtype , name ) : <EOL> if argtype : <EOL> try : <EOL> return argtype ( val ) <EOL> except ( TypeError , ValueError ) : <EOL> ex = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> raise WrongArgumentType ( "<STR_LIT>" % ( <EOL> name , argtype , val , ex ) ) <EOL> else : <EOL> return NotImplemented <EOL> def _validate_args ( self , swfuncs , tailargs ) : <EOL> if six . get_method_function ( self . help ) in swfuncs : <EOL> raise ShowHelp ( ) <EOL> if six . get_method_function ( self . helpall ) in swfuncs : <EOL> raise ShowHelpAll ( ) <EOL> if six . get_method_function ( self . version ) in swfuncs : <EOL> raise ShowVersion ( ) <EOL> requirements = { } <EOL> exclusions = { } <EOL> for swinfo in self . _switches_by_func . values ( ) : <EOL> if swinfo . mandatory and not swinfo . func in swfuncs : <EOL> raise MissingMandatorySwitch ( "<STR_LIT>" % <EOL> ( "<STR_LIT:/>" . join ( ( "<STR_LIT:->" if len ( n ) == <NUM_LIT:1> else "<STR_LIT>" ) + n for n in swinfo . names ) , ) ) <EOL> requirements [ swinfo . func ] = set ( self . _switches_by_name [ req ] for req in swinfo . requires ) <EOL> exclusions [ swinfo . func ] = set ( self . _switches_by_name [ exc ] for exc in swinfo . excludes ) <EOL> gotten = set ( swfuncs . keys ( ) ) <EOL> for func in gotten : <EOL> missing = set ( f . func for f in requirements [ func ] ) - gotten <EOL> if missing : <EOL> raise SwitchCombinationError ( "<STR_LIT>" % <EOL> ( swfuncs [ func ] . swname , [ self . _switches_by_func [ f ] . names [ <NUM_LIT:0> ] for f in missing ] ) ) <EOL> invalid = set ( f . func for f in exclusions [ func ] ) & gotten <EOL> if invalid : <EOL> raise SwitchCombinationError ( "<STR_LIT>" % <EOL> ( swfuncs [ func ] . swname , [ swfuncs [ f ] . swname for f in invalid ] ) ) <EOL> m = six . getfullargspec ( self . main ) <EOL> max_args = six . MAXSIZE if m . varargs else len ( m . args ) - <NUM_LIT:1> <EOL> min_args = len ( m . args ) - <NUM_LIT:1> - ( len ( m . defaults ) if m . defaults else <NUM_LIT:0> ) <EOL> if len ( tailargs ) < min_args : <EOL> raise PositionalArgumentsError ( "<STR_LIT>" % <EOL> ( min_args , tailargs ) ) <EOL> elif len ( tailargs ) > max_args : <EOL> raise PositionalArgumentsError ( "<STR_LIT>" % <EOL> ( max_args , tailargs ) ) <EOL> if hasattr ( self . main , '<STR_LIT>' ) : <EOL> tailargs = self . _positional_validate ( tailargs , self . main . positional , self . main . positional_varargs , m . args [ <NUM_LIT:1> : ] , m . varargs ) <EOL> elif hasattr ( m , '<STR_LIT>' ) : <EOL> args_names = list ( m . args [ <NUM_LIT:1> : ] ) <EOL> positional = [ None ] * len ( args_names ) <EOL> varargs = None <EOL> for item in m . annotations : <EOL> if item == m . varargs : <EOL> varargs = m . annotations [ item ] <EOL> else : <EOL> positional [ args_names . index ( item ) ] = m . annotations [ item ] <EOL> tailargs = self . _positional_validate ( tailargs , positional , varargs , <EOL> m . args [ <NUM_LIT:1> : ] , m . varargs ) <EOL> ordered = [ ( f , a ) for _ , f , a in <EOL> sorted ( [ ( sf . index , f , sf . val ) for f , sf in swfuncs . items ( ) ] ) ] <EOL> return ordered , tailargs <EOL> def _positional_validate ( self , args , validator_list , varargs , argnames , varargname ) : <EOL> """<STR_LIT>""" <EOL> out_args = list ( args ) <EOL> for i in range ( min ( len ( args ) , len ( validator_list ) ) ) : <EOL> if validator_list [ i ] is not None : <EOL> out_args [ i ] = self . _handle_argument ( args [ i ] , validator_list [ i ] , argnames [ i ] ) <EOL> if len ( args ) > len ( validator_list ) : <EOL> if varargs is not None : <EOL> out_args [ len ( validator_list ) : ] = [ <EOL> self . _handle_argument ( a , varargs , varargname ) for a in args [ len ( validator_list ) : ] ] <EOL> else : <EOL> out_args [ len ( validator_list ) : ] = args [ len ( validator_list ) : ] <EOL> return out_args <EOL> @ classmethod <EOL> def run ( cls , argv = None , exit = True ) : <EOL> """<STR_LIT>""" <EOL> if argv is None : <EOL> argv = sys . argv <EOL> cls . autocomplete ( argv ) <EOL> argv = list ( argv ) <EOL> inst = cls ( argv . pop ( <NUM_LIT:0> ) ) <EOL> retcode = <NUM_LIT:0> <EOL> try : <EOL> swfuncs , tailargs = inst . _parse_args ( argv ) <EOL> ordered , tailargs = inst . _validate_args ( swfuncs , tailargs ) <EOL> except ShowHelp : <EOL> inst . help ( ) <EOL> except ShowHelpAll : <EOL> inst . helpall ( ) <EOL> except ShowVersion : <EOL> inst . version ( ) <EOL> except SwitchError : <EOL> ex = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> print ( "<STR_LIT>" % ( ex , ) ) <EOL> print ( "<STR_LIT>" ) <EOL> inst . help ( ) <EOL> retcode = <NUM_LIT:2> <EOL> else : <EOL> for f , a in ordered : <EOL> f ( inst , * a ) <EOL> cleanup = None <EOL> if not inst . nested_command or inst . CALL_MAIN_IF_NESTED_COMMAND : <EOL> retcode = inst . main ( * tailargs ) <EOL> cleanup = functools . partial ( inst . cleanup , retcode ) <EOL> if not retcode and inst . nested_command : <EOL> subapp , argv = inst . nested_command <EOL> subapp . parent = inst <EOL> inst , retcode = subapp . run ( argv , exit = False ) <EOL> if cleanup : <EOL> cleanup ( ) <EOL> if retcode is None : <EOL> retcode = <NUM_LIT:0> <EOL> if exit : <EOL> sys . exit ( retcode ) <EOL> else : <EOL> return inst , retcode <EOL> @ classmethod <EOL> def invoke ( cls , * args , ** switches ) : <EOL> """<STR_LIT>""" <EOL> inst = cls ( "<STR_LIT>" ) <EOL> swfuncs = inst . _parse_kwd_args ( switches ) <EOL> ordered , tailargs = inst . _validate_args ( swfuncs , args ) <EOL> for f , a in ordered : <EOL> f ( inst , * a ) <EOL> cleanup = None <EOL> if not inst . nested_command or inst . CALL_MAIN_IF_NESTED_COMMAND : <EOL> retcode = inst . main ( * tailargs ) <EOL> cleanup = functools . partial ( inst . cleanup , retcode ) <EOL> if not retcode and inst . nested_command : <EOL> subapp , argv = inst . nested_command <EOL> subapp . parent = inst <EOL> inst , retcode = subapp . run ( argv , exit = False ) <EOL> if cleanup : <EOL> cleanup ( ) <EOL> return inst , retcode <EOL> def _parse_kwd_args ( self , switches ) : <EOL> """<STR_LIT>""" <EOL> swfuncs = { } <EOL> for index , ( swname , val ) in enumerate ( switches . items ( ) , <NUM_LIT:1> ) : <EOL> switch = getattr ( type ( self ) , swname ) <EOL> swinfo = self . _switches_by_func [ switch . _switch_info . func ] <EOL> if isinstance ( switch , CountOf ) : <EOL> p = ( range ( val ) , ) <EOL> elif swinfo . list and not hasattr ( val , "<STR_LIT>" ) : <EOL> raise SwitchError ( "<STR_LIT>" % ( swname , ) ) <EOL> elif not swinfo . argtype : <EOL> if val not in ( True , False , None , Flag ) : <EOL> raise SwitchError ( "<STR_LIT>" % ( swname , ) ) <EOL> p = ( ) <EOL> else : <EOL> p = ( val , ) <EOL> swfuncs [ swinfo . func ] = SwitchParseInfo ( swname , p , index ) <EOL> return swfuncs <EOL> def main ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if self . _subcommands : <EOL> if args : <EOL> print ( "<STR_LIT>" % ( args [ <NUM_LIT:0> ] , ) ) <EOL> print ( "<STR_LIT>" ) <EOL> self . help ( ) <EOL> return <NUM_LIT:1> <EOL> if not self . nested_command : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> self . help ( ) <EOL> return <NUM_LIT:1> <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> return <NUM_LIT:1> <EOL> def cleanup ( self , retcode ) : <EOL> """<STR_LIT>""" <EOL> @ switch ( [ "<STR_LIT>" ] , overridable = True , group = "<STR_LIT>" ) <EOL> def helpall ( self ) : <EOL> """<STR_LIT>""" <EOL> self . help ( ) <EOL> print ( "<STR_LIT>" ) <EOL> if self . _subcommands : <EOL> for name , subcls in sorted ( self . _subcommands . items ( ) ) : <EOL> subapp = ( subcls . get ( ) ) ( "<STR_LIT>" % ( self . PROGNAME , name ) ) <EOL> subapp . parent = self <EOL> for si in subapp . _switches_by_func . values ( ) : <EOL> if si . group == "<STR_LIT>" : <EOL> si . group = "<STR_LIT>" <EOL> subapp . helpall ( ) <EOL> @ switch ( [ "<STR_LIT>" , "<STR_LIT>" ] , overridable = True , group = "<STR_LIT>" ) <EOL> def help ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _get_prog_version ( ) : <EOL> self . version ( ) <EOL> print ( "<STR_LIT>" ) <EOL> if self . DESCRIPTION : <EOL> print ( self . DESCRIPTION . strip ( ) + '<STR_LIT:\n>' ) <EOL> m = six . getfullargspec ( self . main ) <EOL> tailargs = m . args [ <NUM_LIT:1> : ] <EOL> if m . defaults : <EOL> for i , d in enumerate ( reversed ( m . defaults ) ) : <EOL> tailargs [ - i - <NUM_LIT:1> ] = "<STR_LIT>" % ( tailargs [ - i - <NUM_LIT:1> ] , d ) <EOL> if m . varargs : <EOL> tailargs . append ( "<STR_LIT>" % ( m . varargs , ) ) <EOL> tailargs = "<STR_LIT:U+0020>" . join ( tailargs ) <EOL> with self . COLOR_USAGE : <EOL> print ( "<STR_LIT>" ) <EOL> if not self . USAGE : <EOL> if self . _subcommands : <EOL> self . USAGE = "<STR_LIT>" <EOL> else : <EOL> self . USAGE = "<STR_LIT>" <EOL> print ( self . USAGE % { "<STR_LIT>" : colors . filter ( self . PROGNAME ) , "<STR_LIT>" : tailargs } ) <EOL> by_groups = { } <EOL> for si in self . _switches_by_func . values ( ) : <EOL> if si . group not in by_groups : <EOL> by_groups [ si . group ] = [ ] <EOL> by_groups [ si . group ] . append ( si ) <EOL> def switchs ( by_groups , show_groups ) : <EOL> for grp , swinfos in sorted ( by_groups . items ( ) , key = lambda item : item [ <NUM_LIT:0> ] ) : <EOL> if show_groups : <EOL> print ( self . COLOR_GROUPS [ grp ] | grp ) <EOL> for si in sorted ( swinfos , key = lambda si : si . names ) : <EOL> swnames = "<STR_LIT:U+002CU+0020>" . join ( ( "<STR_LIT:->" if len ( n ) == <NUM_LIT:1> else "<STR_LIT>" ) + n for n in si . names <EOL> if n in self . _switches_by_name and self . _switches_by_name [ n ] == si ) <EOL> if si . argtype : <EOL> if isinstance ( si . argtype , type ) : <EOL> typename = si . argtype . __name__ <EOL> else : <EOL> typename = str ( si . argtype ) <EOL> argtype = "<STR_LIT>" % ( si . argname . upper ( ) , typename ) <EOL> else : <EOL> argtype = "<STR_LIT>" <EOL> prefix = swnames + argtype <EOL> yield si , prefix , self . COLOR_GROUPS [ grp ] <EOL> if show_groups : <EOL> print ( "<STR_LIT>" ) <EOL> sw_width = max ( len ( prefix ) for si , prefix , color in switchs ( by_groups , False ) ) + <NUM_LIT:4> <EOL> cols , _ = get_terminal_size ( ) <EOL> description_indent = "<STR_LIT>" <EOL> wrapper = TextWrapper ( width = max ( cols - min ( sw_width , <NUM_LIT> ) , <NUM_LIT:50> ) - <NUM_LIT:6> ) <EOL> indentation = "<STR_LIT:\n>" + "<STR_LIT:U+0020>" * ( cols - wrapper . width ) <EOL> for si , prefix , color in switchs ( by_groups , True ) : <EOL> help = si . help <EOL> if si . list : <EOL> help += "<STR_LIT>" <EOL> if si . mandatory : <EOL> help += "<STR_LIT>" <EOL> if si . requires : <EOL> help += "<STR_LIT>" % ( "<STR_LIT:U+002CU+0020>" . join ( ( ( "<STR_LIT:->" if len ( s ) == <NUM_LIT:1> else "<STR_LIT>" ) + s ) for s in si . requires ) ) <EOL> if si . excludes : <EOL> help += "<STR_LIT>" % ( "<STR_LIT:U+002CU+0020>" . join ( ( ( "<STR_LIT:->" if len ( s ) == <NUM_LIT:1> else "<STR_LIT>" ) + s ) for s in si . excludes ) ) <EOL> msg = indentation . join ( wrapper . wrap ( "<STR_LIT:U+0020>" . join ( l . strip ( ) for l in help . splitlines ( ) ) ) ) <EOL> if len ( prefix ) + wrapper . width >= cols : <EOL> padding = indentation <EOL> else : <EOL> padding = "<STR_LIT:U+0020>" * max ( cols - wrapper . width - len ( prefix ) - <NUM_LIT:4> , <NUM_LIT:1> ) <EOL> print ( description_indent % ( color | prefix , padding , color | msg ) ) <EOL> if self . _subcommands : <EOL> gc = self . COLOR_GROUPS [ "<STR_LIT>" ] <EOL> print ( gc | "<STR_LIT>" ) <EOL> for name , subcls in sorted ( self . _subcommands . items ( ) ) : <EOL> with gc : <EOL> subapp = subcls . get ( ) <EOL> doc = subapp . DESCRIPTION if subapp . DESCRIPTION else getdoc ( subapp ) <EOL> if self . SUBCOMMAND_HELPMSG : <EOL> help = doc + "<STR_LIT>" if doc else "<STR_LIT>" <EOL> help += self . SUBCOMMAND_HELPMSG . format ( parent = self . PROGNAME , sub = name ) <EOL> else : <EOL> help = doc if doc else "<STR_LIT>" <EOL> msg = indentation . join ( wrapper . wrap ( "<STR_LIT:U+0020>" . join ( l . strip ( ) for l in help . splitlines ( ) ) ) ) <EOL> if len ( name ) + wrapper . width >= cols : <EOL> padding = indentation <EOL> else : <EOL> padding = "<STR_LIT:U+0020>" * max ( cols - wrapper . width - len ( name ) - <NUM_LIT:4> , <NUM_LIT:1> ) <EOL> if colors . contains_colors ( subcls . name ) : <EOL> bodycolor = colors . extract ( subcls . name ) <EOL> else : <EOL> bodycolor = gc <EOL> print ( description_indent <EOL> % ( subcls . name , padding , <EOL> bodycolor | colors . filter ( msg ) ) ) <EOL> def _get_prog_version ( self ) : <EOL> ver = None <EOL> curr = self <EOL> while curr is not None : <EOL> ver = getattr ( curr , "<STR_LIT>" , None ) <EOL> if ver is not None : <EOL> return ver <EOL> curr = curr . parent <EOL> return ver <EOL> @ switch ( [ "<STR_LIT>" , "<STR_LIT>" ] , overridable = True , group = "<STR_LIT>" ) <EOL> def version ( self ) : <EOL> """<STR_LIT>""" <EOL> ver = self . _get_prog_version ( ) <EOL> ver_name = ver if ver is not None else "<STR_LIT>" <EOL> print ( '<STR_LIT>' . format ( self . PROGNAME , ver_name ) ) </s>
