|
| 1 | +import argparse |
| 2 | +from string import Template |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +FILENAME = "completer_template" |
| 7 | + |
| 8 | +def completer_extractor(parser): |
| 9 | + completer_dict = {"opts": [], |
| 10 | + "subparser":{}} |
| 11 | + |
| 12 | + print "parser: %s" % str(parser) |
| 13 | + for a in parser._actions: |
| 14 | + if type(a) is argparse._SubParsersAction: |
| 15 | + subparser_dict = a.choices |
| 16 | + for sarg in subparser_dict: |
| 17 | + sub_argument = subparser_dict[sarg] |
| 18 | + |
| 19 | + completer_dict["subparser"][sarg] = {"opts":[]} |
| 20 | + for sa in sub_argument._actions: |
| 21 | + completer_dict["subparser"][sarg]["opts"].extend(sa.option_strings) |
| 22 | + continue |
| 23 | + |
| 24 | + |
| 25 | + if "option_strings" in dir(a): |
| 26 | + completer_dict["opts"].extend(a.option_strings) |
| 27 | + |
| 28 | + |
| 29 | + name = parser.prog |
| 30 | + if "." in name: |
| 31 | + name = name.partition(".")[0] |
| 32 | + generate_completer(completer_dict, name) |
| 33 | + print "parser dict: %s" % str(completer_dict) |
| 34 | + |
| 35 | +def generate_completer(completer_dict, name): |
| 36 | + print "name: %s" % name |
| 37 | + completer_file = FILENAME |
| 38 | + f = open(completer_file) |
| 39 | + completer = Template(f.read()) |
| 40 | + f.close() |
| 41 | + |
| 42 | + |
| 43 | + completer = completer.safe_substitute(NAME = name) |
| 44 | + completer = Template(completer).safe_substitute(GLOBAL_OPTS = generate_opt_string(completer_dict)) |
| 45 | + completer = Template(completer).safe_substitute(CMDS = generate_sub_commands(completer_dict["subparser"])) |
| 46 | + |
| 47 | + print "completer:\n%s" % completer |
| 48 | + f = open("%s_bash_completer" % name, "w") |
| 49 | + f.write(completer) |
| 50 | + f.close() |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +def generate_sub_commands(command_dict): |
| 55 | + subcommands = "" |
| 56 | + subcommands_opts = False |
| 57 | + subcommands += "\t\tcase \"${prev}\" in\n" |
| 58 | + for cmd in command_dict: |
| 59 | + if len(command_dict[cmd]["opts"]) == 0: |
| 60 | + continue |
| 61 | + subcommands_opts = True |
| 62 | + subcommands += "\t\t\t%s)\n" % cmd |
| 63 | + subcommands += "\t\t\t\tlocal %s_opts = \"%s\"\n" % (cmd, generate_opt_string(command_dict[cmd])) |
| 64 | + subcommands += "\t\t\t\tCOMPREPLY=( $(compgen -W ${%s_opts} -- ${curr}) )\n" % (cmd) |
| 65 | + subcommands += "\t\t\t\treturn 0\n" |
| 66 | + subcommands += "\t\t\t\t;;\n" |
| 67 | + |
| 68 | + subcommands += "\t\t\t*)\n" |
| 69 | + subcommands += "\t\t\t;;\n" |
| 70 | + subcommands += "\t\tesac\n" |
| 71 | + |
| 72 | + if not subcommands_opts: |
| 73 | + return "" |
| 74 | + return subcommands |
| 75 | + |
| 76 | +def generate_opt_string(completer_dict): |
| 77 | + opts = completer_dict["opts"] |
| 78 | + opt_string = "" |
| 79 | + if "subparser" in completer_dict.keys(): |
| 80 | + sub_commands = completer_dict["subparser"].keys() |
| 81 | + |
| 82 | + for sub_command in sub_commands: |
| 83 | + opt_string += sub_command |
| 84 | + opt_string += " " |
| 85 | + |
| 86 | + for i in range(len(opts)): |
| 87 | + opt = opts[i] |
| 88 | + opt_string += opt |
| 89 | + if i < (len(opts) - 1): |
| 90 | + opt_string += " " |
| 91 | + |
| 92 | + |
| 93 | + return opt_string |
0 commit comments