<s> from types import * <EOL> from cStringIO import StringIO <EOL> def splitLine ( line , COLS = <NUM_LIT> , indent = <NUM_LIT:10> ) : <EOL> indent = "<STR_LIT:U+0020>" * indent <EOL> width = COLS - ( len ( indent ) + <NUM_LIT:1> ) <EOL> if indent and width < <NUM_LIT:15> : <EOL> width = COLS - <NUM_LIT:2> <EOL> indent = "<STR_LIT:U+0020>" <EOL> s = StringIO ( ) <EOL> i = <NUM_LIT:0> <EOL> for word in line . split ( ) : <EOL> if i == <NUM_LIT:0> : <EOL> s . write ( indent + word ) <EOL> i = len ( word ) <EOL> continue <EOL> if i + len ( word ) >= width : <EOL> s . write ( '<STR_LIT:\n>' + indent + word ) <EOL> i = len ( word ) <EOL> continue <EOL> s . write ( '<STR_LIT:U+0020>' + word ) <EOL> i += len ( word ) + <NUM_LIT:1> <EOL> return s . getvalue ( ) <EOL> def formatDefinitions ( options , COLS , presets = { } ) : <EOL> s = StringIO ( ) <EOL> for ( longname , default , doc ) in options : <EOL> s . write ( '<STR_LIT>' + longname + '<STR_LIT>' ) <EOL> default = presets . get ( longname , default ) <EOL> if type ( default ) in ( IntType , LongType ) : <EOL> try : <EOL> default = int ( default ) <EOL> except : <EOL> pass <EOL> if default is not None : <EOL> doc += '<STR_LIT>' + repr ( default ) + '<STR_LIT:)>' <EOL> s . write ( splitLine ( doc , COLS , <NUM_LIT:10> ) ) <EOL> s . write ( '<STR_LIT>' ) <EOL> return s . getvalue ( ) <EOL> def usage ( string ) : <EOL> raise ValueError ( string ) <EOL> def defaultargs ( options ) : <EOL> l = { } <EOL> for ( longname , default , doc ) in options : <EOL> if default is not None : <EOL> l [ longname ] = default <EOL> return l <EOL> def parseargs ( argv , options , minargs = None , maxargs = None , presets = { } ) : <EOL> config = { } <EOL> longkeyed = { } <EOL> for option in options : <EOL> longname , default , doc = option <EOL> longkeyed [ longname ] = option <EOL> config [ longname ] = default <EOL> for longname in presets . keys ( ) : <EOL> config [ longname ] = presets [ longname ] <EOL> options = [ ] <EOL> args = [ ] <EOL> pos = <NUM_LIT:0> <EOL> while pos < len ( argv ) : <EOL> if argv [ pos ] [ : <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> args . append ( argv [ pos ] ) <EOL> pos += <NUM_LIT:1> <EOL> else : <EOL> if pos == len ( argv ) - <NUM_LIT:1> : <EOL> usage ( '<STR_LIT>' ) <EOL> key , value = argv [ pos ] [ <NUM_LIT:2> : ] , argv [ pos + <NUM_LIT:1> ] <EOL> pos += <NUM_LIT:2> <EOL> if not longkeyed . has_key ( key ) : <EOL> usage ( '<STR_LIT>' + key ) <EOL> longname , default , doc = longkeyed [ key ] <EOL> try : <EOL> t = type ( config [ longname ] ) <EOL> if t is NoneType or t is StringType : <EOL> config [ longname ] = value <EOL> elif t in ( IntType , LongType ) : <EOL> config [ longname ] = long ( value ) <EOL> elif t is FloatType : <EOL> config [ longname ] = float ( value ) <EOL> else : <EOL> assert <NUM_LIT:0> <EOL> except ValueError , e : <EOL> usage ( '<STR_LIT>' % ( key , str ( e ) ) ) <EOL> for key , value in config . items ( ) : <EOL> if value is None : <EOL> usage ( "<STR_LIT>" % key ) <EOL> if minargs is not None and len ( args ) < minargs : <EOL> usage ( "<STR_LIT>" % minargs ) <EOL> if maxargs is not None and len ( args ) > maxargs : <EOL> usage ( "<STR_LIT>" % maxargs ) <EOL> return ( config , args ) <EOL> def test_parseargs ( ) : <EOL> assert parseargs ( ( '<STR_LIT:d>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:e>' , '<STR_LIT>' , '<STR_LIT:3>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:f>' ) , ( ( '<STR_LIT:a>' , '<STR_LIT:x>' , '<STR_LIT>' ) , ( '<STR_LIT:b>' , <NUM_LIT:1> , '<STR_LIT>' ) , ( '<STR_LIT:c>' , <NUM_LIT> , '<STR_LIT>' ) ) ) == ( { '<STR_LIT:a>' : '<STR_LIT>' , '<STR_LIT:b>' : <NUM_LIT:3> , '<STR_LIT:c>' : <NUM_LIT> } , [ '<STR_LIT:d>' , '<STR_LIT:e>' , '<STR_LIT:f>' ] ) <EOL> assert parseargs ( [ ] , [ ( '<STR_LIT:a>' , '<STR_LIT:x>' , '<STR_LIT>' ) ] ) == ( { '<STR_LIT:a>' : '<STR_LIT:x>' } , [ ] ) <EOL> assert parseargs ( [ '<STR_LIT>' , '<STR_LIT:x>' , '<STR_LIT>' , '<STR_LIT:y>' ] , [ ( '<STR_LIT:a>' , '<STR_LIT>' , '<STR_LIT>' ) ] ) == ( { '<STR_LIT:a>' : '<STR_LIT:y>' } , [ ] ) <EOL> try : <EOL> parseargs ( [ ] , [ ( '<STR_LIT:a>' , '<STR_LIT:x>' , '<STR_LIT>' ) ] ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> parseargs ( [ '<STR_LIT>' , '<STR_LIT:x>' ] , [ ] ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> parseargs ( [ '<STR_LIT>' ] , [ ( '<STR_LIT:a>' , '<STR_LIT:x>' , '<STR_LIT>' ) ] ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> parseargs ( [ ] , [ ] , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> except ValueError : <EOL> pass <EOL> assert parseargs ( [ '<STR_LIT:x>' ] , [ ] , <NUM_LIT:1> , <NUM_LIT:2> ) == ( { } , [ '<STR_LIT:x>' ] ) <EOL> assert parseargs ( [ '<STR_LIT:x>' , '<STR_LIT:y>' ] , [ ] , <NUM_LIT:1> , <NUM_LIT:2> ) == ( { } , [ '<STR_LIT:x>' , '<STR_LIT:y>' ] ) <EOL> try : <EOL> parseargs ( [ '<STR_LIT:x>' , '<STR_LIT:y>' , '<STR_LIT:z>' ] , [ ] , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> parseargs ( [ '<STR_LIT>' , '<STR_LIT>' ] , [ ( '<STR_LIT:a>' , <NUM_LIT:3> , '<STR_LIT>' ) ] ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> parseargs ( [ '<STR_LIT>' , '<STR_LIT:z>' ] , [ ( '<STR_LIT:a>' , <NUM_LIT> , '<STR_LIT>' ) ] ) <EOL> except ValueError : <EOL> pass </s>
