Revision fad06963

b/autotools/build-bash-completion
655 655
  elif kind == "manyinstances":
656 656
    # FIXME: no support for many instances
657 657
    return cli.cli_option(*opts, type="string")
658
  elif kind.startswith("choices "):
659
    choices = kind[len("choices "):].split(",")
658
  elif kind.startswith("choices="):
659
    choices = kind[len("choices="):].split(",")
660 660
    return cli.cli_option(*opts, type="choice", choices=choices)
661 661
  else:
662 662
    # FIXME: there are many other currently unused completion types,
663 663
    # should be added on an as-needed basis
664
    raise Exception("Unhnadled option kind '%s'" % kind)
664
    raise Exception("Unhandled option kind '%s'" % kind)
665

  
666

  
667
#: serialised kind to arg type
668
_ARG_MAP = {
669
  "command": cli.ArgCommand,
670
  "file": cli.ArgFile,
671
  "host": cli.ArgHost,
672
  "jobid": cli.ArgJobId,
673
  "onegroup": cli.ArgGroup,
674
  "oneinstance": cli.ArgInstance,
675
  "onenode": cli.ArgNode,
676
  "oneos": cli.ArgOs,
677
  "string": cli.ArgUnknown,
678
  }
679

  
680

  
681
def HaskellArgToCliArg(kind, min_cnt, max_cnt):
682
  """Converts a Haskell options to Python _Argument.
683

  
684
  @type kind: string
685
  @param kind: type generated by Common.hs/argComplToText; needs to be
686
      kept in sync
687

  
688
  """
689
  min_cnt = int(min_cnt)
690
  if max_cnt == "none":
691
    max_cnt = None
692
  else:
693
    max_cnt = int(max_cnt)
694
  # pylint: disable=W0142
695
  # since we pass **kwargs
696
  kwargs = {"min": min_cnt, "max": max_cnt}
697

  
698
  if kind.startswith("choices=") or kind.startswith("suggest="):
699
    (kind, choices) = kind.split("=", 1)
700
    kwargs["choices"] = choices.split(",")
701

  
702
  if kind not in _ARG_MAP:
703
    raise Exception("Unhandled argument kind '%s'" % kind)
704
  else:
705
    return _ARG_MAP[kind](**kwargs)
665 706

  
666 707

  
667 708
def WriteHaskellCompletion(sw, script, htools=True, debug=True):
......
684 725
    func_name = script
685 726
  output = utils.RunCmd([cmd, "--help-completion"], env=env, cwd=".").output
686 727
  cli_opts = []
728
  args = []
687 729
  for line in output.splitlines():
688
    v = line.split(None, 1)
689
    if len(v) != 2:
690
      raise Exception("Invalid help completion output from %s: %s" %
691
                      (script, v))
692
    (opts, kind) = v
693
    cli_opts.append(HaskellOptToOptParse(opts, kind))
694
  WriteCompletion(sw, script_name, func_name, debug, opts=cli_opts, args=[])
730
    v = line.split(None)
731
    exc = lambda msg: Exception("Invalid %s output from %s: %s" %
732
                                (msg, script, v))
733
    if len(v) < 2:
734
      raise exc("help completion")
735
    if v[0].startswith("-"):
736
      if len(v) != 2:
737
        raise exc("option format")
738
      (opts, kind) = v
739
      cli_opts.append(HaskellOptToOptParse(opts, kind))
740
    else:
741
      if len(v) != 3:
742
        raise exc("argument format")
743
      (kind, min_cnt, max_cnt) = v
744
      args.append(HaskellArgToCliArg(kind, min_cnt, max_cnt))
745
  WriteCompletion(sw, script_name, func_name, debug, opts=cli_opts, args=args)
695 746

  
696 747

  
697 748
def main():
b/htools/Ganeti/Common.hs
91 91

  
92 92
-- | Text serialisation for 'OptCompletion', used on the Python side.
93 93
complToText :: OptCompletion -> String
94
complToText (OptComplChoices choices) = "choices " ++ intercalate "," choices
95
complToText (OptComplSuggest choices) = "suggest " ++ intercalate "," choices
94
complToText (OptComplChoices choices) = "choices=" ++ intercalate "," choices
95
complToText (OptComplSuggest choices) = "suggest=" ++ intercalate "," choices
96 96
complToText compl =
97 97
  let show_compl = show compl
98 98
      stripped = stripPrefix "OptCompl" show_compl

Also available in: Unified diff