def autocomplete(parser, arg_completer=None, opt_completer=None, subcmd_completer=None, subcommands=None):
 if ('OPTPARSE_AUTO_COMPLETE' not in os.environ):
  return
 if (arg_completer is None):
  arg_completer = NoneCompleter()
 if (opt_completer is None):
  opt_completer = NoneCompleter()
 if (subcmd_completer is None):
  subcmd_completer = NoneCompleter()
 completer = arg_completer
 if ('COMP_WORDS' not in os.environ):
  os.environ['COMP_WORDS'] = os.environ['COMP_LINE']
 cwords = os.environ['COMP_WORDS'].split()
 cline = os.environ['COMP_LINE']
 cpoint = int(os.environ['COMP_POINT'])
 cword = int(os.environ['COMP_CWORD'])
 if subcommands:
  assert isinstance(subcommands, types.DictType)
  value = guess_first_nonoption(parser, subcommands)
  if value:
   if isinstance(value, (types.ListType, types.TupleType)):
    parser = value[0]
    if ((len(value) > 1) and value[1]):
     completer = value[1]
    else:
     completer = subcmd_completer
    return autocomplete(parser, completer)
   elif hasattr(value, 'autocomplete'):
    return value.autocomplete(subcmd_completer)
   else:
    sys.exit(1)
 (prefix, suffix) = optcomplete.extract_word(cline, cpoint)
 optarg = False
 try:
  prev = None
  if (cword < len(cwords)):
   mo = re.search('(--.*)=(.*)', cwords[cword])
   if mo:
    (prev, prefix) = mo.groups()
  if (not prev):
   prev = cwords[(cword - 1)]
  if (prev and prev.startswith('-')):
   option = parser.get_option(prev)
   if option:
    if (option.nargs > 0):
     optarg = True
     if hasattr(option, 'completer'):
      completer = option.completer
     elif (option.type != 'string'):
      completer = NoneCompleter()
     else:
      completer = opt_completer
    elif hasattr(option, 'completer'):
     raise SystemExit(('Error: optparse option with a completer does not take arguments: %s' % str(option)))
 except KeyError:
  pass
 completions = []
 if ((not optarg) and ((not prefix) or prefix.startswith('-'))):
  completions += parser._short_opt.keys()
  completions += parser._long_opt.keys()
 if (completer and ((not prefix) or (not prefix.startswith('-')))):
  if isinstance(completer, (types.StringType, types.ListType, types.TupleType)):
   completer = RegexCompleter(completer)
   completions += completer(os.getcwd(), cline, cpoint, prefix, suffix)
  elif isinstance(completer, (types.FunctionType, types.LambdaType, types.ClassType, types.ObjectType)):
   completions += completer(os.getcwd(), cline, cpoint, prefix, suffix)
 if prefix:
  completions = filter((lambda x: x.startswith(prefix)), completions)
 print ' '.join(completions)
 if debugfn:
  f = open(debugfn, 'a')
  print >>f, '---------------------------------------------------------'
  print >>f, 'CWORDS', cwords
  print >>f, 'CLINE', cline
  print >>f, 'CPOINT', cpoint
  print >>f, 'CWORD', cword
  print >>f, '\nShort options'
  print >>f, pformat(parser._short_opt)
  print >>f, '\nLong options'
  print >>f, pformat(parser._long_opt)
  print >>f, 'Prefix/Suffix:', prefix, suffix
  print >>f, 'completions', completions
  f.close()
 sys.exit(1)

def autocomplete ( parser , arg_completer = None , opt_completer = None , subcmd_completer = None , subcommands = None ) : if ( 'OPTPARSE_AUTO_COMPLETE' not in os . environ ) : return if ( arg_completer is None ) : arg_completer = NoneCompleter ( ) if ( opt_completer is None ) : opt_completer = NoneCompleter ( ) if ( subcmd_completer is None ) : subcmd_completer = NoneCompleter ( ) completer = arg_completer if ( 'COMP_WORDS' not in os . environ ) : os . environ [ 'COMP_WORDS' ] = os . environ [ 'COMP_LINE' ] cwords = os . environ [ 'COMP_WORDS' ] . split ( ) cline = os . environ [ 'COMP_LINE' ] cpoint = int ( os . environ [ 'COMP_POINT' ] ) cword = int ( os . environ [ 'COMP_CWORD' ] ) if subcommands : assert isinstance ( subcommands , types . DictType ) value = guess_first_nonoption ( parser , subcommands ) if value : if isinstance ( value , ( types . ListType , types . TupleType ) ) : parser = value [ 0 ] if ( ( len ( value ) > 1 ) and value [ 1 ] ) : completer = value [ 1 ] else : completer = subcmd_completer return autocomplete ( parser , completer ) elif hasattr ( value , 'autocomplete' ) : return value . autocomplete ( subcmd_completer ) else : sys . exit ( 1 ) ( prefix , suffix ) = optcomplete . extract_word ( cline , cpoint ) optarg = False try : prev = None if ( cword < len ( cwords ) ) : mo = re . search ( '(--.*)=(.*)' , cwords [ cword ] ) if mo : ( prev , prefix ) = mo . groups ( ) if ( not prev ) : prev = cwords [ ( cword - 1 ) ] if ( prev and prev . startswith ( '-' ) ) : option = parser . get_option ( prev ) if option : if ( option . nargs > 0 ) : optarg = True if hasattr ( option , 'completer' ) : completer = option . completer elif ( option . type != 'string' ) : completer = NoneCompleter ( ) else : completer = opt_completer elif hasattr ( option , 'completer' ) : raise SystemExit ( ( 'Error: optparse option with a completer does not take arguments: %s' % str ( option ) ) ) except KeyError : pass completions = [ ] if ( ( not optarg ) and ( ( not prefix ) or prefix . startswith ( '-' ) ) ) : completions += parser . _short_opt . keys ( ) completions += parser . _long_opt . keys ( ) if ( completer and ( ( not prefix ) or ( not prefix . startswith ( '-' ) ) ) ) : if isinstance ( completer , ( types . StringType , types . ListType , types . TupleType ) ) : completer = RegexCompleter ( completer ) completions += completer ( os . getcwd ( ) , cline , cpoint , prefix , suffix ) elif isinstance ( completer , ( types . FunctionType , types . LambdaType , types . ClassType , types . ObjectType ) ) : completions += completer ( os . getcwd ( ) , cline , cpoint , prefix , suffix ) if prefix : completions = filter ( ( lambda x : x . startswith ( prefix ) ) , completions ) print ' ' . join ( completions ) if debugfn : f = open ( debugfn , 'a' ) print >> f , '---------------------------------------------------------' print >> f , 'CWORDS' , cwords print >> f , 'CLINE' , cline print >> f , 'CPOINT' , cpoint print >> f , 'CWORD' , cword print >> f , '\nShort options' print >> f , pformat ( parser . _short_opt ) print >> f , '\nLong options' print >> f , pformat ( parser . _long_opt ) print >> f , 'Prefix/Suffix:' , prefix , suffix print >> f , 'completions' , completions f . close ( ) sys . exit ( 1 )
return autocomplete values for a query .