accept_args.py 944 B

123456789101112131415161718192021222324252627282930
  1. import getopt
  2. import re
  3. import sys
  4. def accept_args( args_spec, args, options, usage ):
  5. defaults_num = len(options)
  6. ( option_pairs, rest_args ) = getopt.getopt( args, '', args_spec )
  7. map( lambda x: options.__setitem__( x[0], x[1] ), option_pairs )
  8. if ( options.has_key( '--help' ) or len( options.keys() ) == defaults_num ):
  9. usage()
  10. sys.exit( 1 )
  11. if len( rest_args ) > 0 and rest_args[0][0] == '@':
  12. f = open( rest_args[0][1:], 'r' )
  13. config_lines = f.read().splitlines()
  14. f.close()
  15. for l in config_lines:
  16. if re.search( r'^\s*#', l ): continue
  17. if re.search( r'^\s*$', l ): continue
  18. m = re.match( r'^(?P<name>.*?)=(?P<value>.*)', l )
  19. if m:
  20. options[ '--%s' % m.group( 'name' ) ] = m.group( 'value' )
  21. else:
  22. raise 'Invalid format of config line "%s"' % l
  23. return rest_args
粤ICP备19079148号