<s> "<STR_LIT>" <EOL> from __future__ import absolute_import , division , print_function <EOL> import inspect <EOL> import itertools <EOL> import sys <EOL> from begin . wrappable import Wrapping <EOL> from begin import cmdline , context , convert <EOL> __all__ = [ '<STR_LIT:start>' ] <EOL> class Program ( Wrapping ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func , auto_convert = False , cmd_delim = None , <EOL> sub_group = None , collector = None , ** kwargs ) : <EOL> if auto_convert : <EOL> func = convert ( _automatic = True ) ( func ) <EOL> Wrapping . __init__ ( self , func ) <EOL> self . _cmd_delim = cmd_delim <EOL> self . _group = sub_group <EOL> self . _collector = collector <EOL> self . _parser = cmdline . create_parser ( func , sub_group = self . _group , <EOL> collector = self . _collector , ** kwargs ) <EOL> def start ( self , args = None ) : <EOL> """<STR_LIT>""" <EOL> commands = [ [ ] ] <EOL> args = args if args is not None else sys . argv [ <NUM_LIT:1> : ] <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> commands = [ list ( group ) for key , group in itertools . groupby ( args , lambda x : x == self . _cmd_delim ) if not key ] <EOL> options = [ ] <EOL> for command in commands : <EOL> opts = self . _parser . parse_args ( command ) <EOL> options . append ( opts ) <EOL> context . clear ( ) <EOL> context . opts_next = tuple ( options ) <EOL> for count , opts in enumerate ( options ) : <EOL> context . opts_next = context . opts_next [ <NUM_LIT:1> : ] <EOL> context . opts_current = opts <EOL> cmdline . apply_options ( self . __wrapped__ , opts , <EOL> run_main = ( count == <NUM_LIT:0> ) , sub_group = self . _group , <EOL> collector = self . _collector ) <EOL> context . opts_previous += ( opts , ) <EOL> context . opts_current = None <EOL> return context . last_return <EOL> def start ( func = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def _start ( func ) : <EOL> prog = Program ( func , ** kwargs ) <EOL> if func . __module__ == '<STR_LIT:__main__>' : <EOL> try : <EOL> prog . start ( ) <EOL> except KeyboardInterrupt : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return prog <EOL> if func is None and len ( kwargs ) > <NUM_LIT:0> : <EOL> return _start <EOL> elif func is None : <EOL> stack = inspect . stack ( ) <EOL> if len ( stack ) < <NUM_LIT:1> : <EOL> return False <EOL> frame = stack [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if not inspect . isframe ( frame ) : <EOL> return False <EOL> return frame . f_globals [ '<STR_LIT>' ] == '<STR_LIT:__main__>' <EOL> elif not callable ( func ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( func ) ) <EOL> return _start ( func ) </s>
