<s> import warnings <EOL> from functools import partial <EOL> from itertools import chain <EOL> from collections import defaultdict <EOL> from sigtools import modifiers , specifiers <EOL> from clize import runner , parser , util , errors <EOL> def _convert_coerce ( func ) : <EOL> if func not in parser . _implicit_converters : <EOL> func = parser . value_converter ( func ) <EOL> return func <EOL> def _clize ( fn , alias = { } , force_positional = ( ) , coerce = { } , <EOL> require_excess = False , extra = ( ) , <EOL> use_kwoargs = None ) : <EOL> sig = specifiers . signature ( fn ) <EOL> has_kwoargs = False <EOL> annotations = defaultdict ( list ) <EOL> ann_positional = [ ] <EOL> for param in sig . parameters . values ( ) : <EOL> coerce_set = False <EOL> if param . kind == param . KEYWORD_ONLY : <EOL> has_kwoargs = True <EOL> elif param . kind == param . VAR_KEYWORD : <EOL> annotations [ param . name ] . append ( parser . Parameter . IGNORE ) <EOL> elif require_excess and param . kind == param . VAR_POSITIONAL : <EOL> annotations [ param . name ] . append ( parser . Parameter . REQUIRED ) <EOL> if param . annotation != param . empty : <EOL> for thing in util . maybe_iter ( param . annotation ) : <EOL> if thing == clize . POSITIONAL : <EOL> ann_positional . append ( param . name ) <EOL> continue <EOL> elif callable ( thing ) : <EOL> coerce_set = True <EOL> thing = _convert_coerce ( thing ) <EOL> annotations [ param . name ] . append ( thing ) <EOL> try : <EOL> func = coerce [ param . name ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> annotations [ param . name ] . append ( _convert_coerce ( func ) ) <EOL> coerce_set = True <EOL> annotations [ param . name ] . extend ( alias . get ( param . name , ( ) ) ) <EOL> if not coerce_set and param . default != param . empty : <EOL> annotations [ param . name ] . append ( <EOL> _convert_coerce ( type ( param . default ) ) ) <EOL> fn = modifiers . annotate ( ** annotations ) ( fn ) <EOL> use_kwoargs = has_kwoargs if use_kwoargs is None else use_kwoargs <EOL> if not use_kwoargs : <EOL> fn = modifiers . autokwoargs ( <EOL> exceptions = chain ( ann_positional , force_positional ) ) ( fn ) <EOL> return runner . Clize ( fn , extra = extra ) <EOL> @ specifiers . forwards_to ( _clize , <NUM_LIT:1> ) <EOL> def clize ( fn = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> if fn is None : <EOL> return partial ( _clize , ** kwargs ) <EOL> else : <EOL> return _clize ( fn , ** kwargs ) <EOL> clize . kwo = partial ( clize , use_kwoargs = True ) <EOL> clize . POSITIONAL = clize . P = parser . ParameterFlag ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> class MakeflagParameter ( parser . NamedParameter ) : <EOL> def __init__ ( self , takes_argument , ** kwargs ) : <EOL> super ( MakeflagParameter , self ) . __init__ ( ** kwargs ) <EOL> self . takes_argument = takes_argument <EOL> def get_value ( self , ba , i ) : <EOL> assert self . takes_argument != <NUM_LIT:0> <EOL> arg = ba . in_args [ i ] <EOL> if ( <EOL> self . takes_argument == <NUM_LIT:1> <EOL> or arg [ <NUM_LIT:1> ] != '<STR_LIT:->' and len ( arg ) > <NUM_LIT:2> <EOL> or '<STR_LIT:=>' in arg <EOL> ) : <EOL> return super ( MakeflagParameter , self ) . get_value ( ba , i ) <EOL> args = ba . in_args [ i + <NUM_LIT:1> : i + <NUM_LIT:1> + self . takes_argument ] <EOL> if len ( args ) != self . takes_argument : <EOL> raise errors . NotEnoughValues <EOL> ba . skip = self . takes_argument <EOL> return '<STR_LIT:U+0020>' . join ( args ) <EOL> class MakeflagFuncParameter ( MakeflagParameter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func , ** kwargs ) : <EOL> super ( MakeflagFuncParameter , self ) . __init__ ( ** kwargs ) <EOL> self . func = func <EOL> def noop ( self , * args , ** kwargs ) : <EOL> pass <EOL> def read_argument ( self , ba , i ) : <EOL> val = True <EOL> ret = self . func ( name = ba . name , command = ba . sig , <EOL> val = val , params = ba . kwargs ) <EOL> if ret : <EOL> ba . func = self . noop <EOL> class MakeflagOptionParameter ( MakeflagParameter , parser . OptionParameter ) : <EOL> pass <EOL> class MakeflagIntOptionParameter ( MakeflagParameter , parser . IntOptionParameter ) : <EOL> pass <EOL> def make_flag ( source , names , default = False , type = None , <EOL> help = '<STR_LIT>' , takes_argument = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> kwargs = { } <EOL> kwargs [ '<STR_LIT>' ] = [ util . name_py2cli ( alias , kw = True ) <EOL> for alias in names ] <EOL> if callable ( source ) : <EOL> return MakeflagFuncParameter ( source , takes_argument = takes_argument , <EOL> ** kwargs ) <EOL> cls = MakeflagOptionParameter <EOL> kwargs [ '<STR_LIT>' ] = source <EOL> kwargs [ '<STR_LIT>' ] = type or parser . is_true <EOL> if not takes_argument : <EOL> return parser . FlagParameter ( value = True , ** kwargs ) <EOL> kwargs [ '<STR_LIT:default>' ] = default <EOL> kwargs [ '<STR_LIT>' ] = takes_argument <EOL> if takes_argument == <NUM_LIT:1> and type is int : <EOL> cls = MakeflagIntOptionParameter <EOL> return cls ( ** kwargs ) </s>
