def guess_first_nonoption(gparser, subcmds_map):
 gparser = copy.deepcopy(gparser)
 def print_usage_nousage(self, file=None):
  pass
 gparser.print_usage = print_usage_nousage
 prev_interspersed = gparser.allow_interspersed_args
 gparser.disable_interspersed_args()
 cwords = os.environ['COMP_WORDS'].split()
 error_func = gparser.error
 try:
  instancemethod = type(OptionParser.error)
  gparser.error = instancemethod(error_override, gparser, OptionParser)
  (gopts, args) = gparser.parse_args(cwords[1:])
 except SystemExit:
  return None
 finally:
  gparser.error = instancemethod(error_func, gparser, OptionParser)
 value = None
 if args:
  subcmdname = args[0]
  try:
   value = subcmds_map[subcmdname]
  except KeyError:
   pass
 gparser.allow_interspersed_args = prev_interspersed
 return value

def guess_first_nonoption ( gparser , subcmds_map ) : gparser = copy . deepcopy ( gparser ) def print_usage_nousage ( self , file = None ) : pass gparser . print_usage = print_usage_nousage prev_interspersed = gparser . allow_interspersed_args gparser . disable_interspersed_args ( ) cwords = os . environ [ 'COMP_WORDS' ] . split ( ) error_func = gparser . error try : instancemethod = type ( OptionParser . error ) gparser . error = instancemethod ( error_override , gparser , OptionParser ) ( gopts , args ) = gparser . parse_args ( cwords [ 1 : ] ) except SystemExit : return None finally : gparser . error = instancemethod ( error_func , gparser , OptionParser ) value = None if args : subcmdname = args [ 0 ] try : value = subcmds_map [ subcmdname ] except KeyError : pass gparser . allow_interspersed_args = prev_interspersed return value
given a global options parser .