Yes, there is a Python API. It is documented here under the function Command.complete (text, word)
The default completer will do filename and sub-command completion but you can extend that by providing your own custom complete method. Here is an example:
class MyGdbCommand(gdb.Command):
def __init__(self):
super().__init__("mycmd", gdb.COMMAND_USER) # or whatever command class you deem appropriate
def complete(self, arguments_string, last):
is_brk_char = (len(arguments_string) < len(last))
args = gdb.string_to_argv(arguments_string)
if arguments_string == "" or arguments_string[-1:] == " ":
args.append("") # Add dummy argument to complete
argc = len(args)
if argc == 1:
if is_brk_char:
return gdb.COMPLETE_NONE
if args[0] in ['-l', '-o']:
return args[0] # it's complete
return ['-l', '-o'] # valid option flags
if argc == 2 and (args[0] == '-l' or args[0] == '-o'):
if is_brk_char:
return gdb.COMPLETE_FILENAME # -l and -o take file arguments
(head, tail) = os.path.split(curr)
return getMatchingFiles(head, tail) # implement appropriate heuristic; pass args[0] flag if it matters
if argc == 3:
if is_brk_char:
return gdb.COMPLETE_NONE
if args[0] == '-l':
return ['-o']
if args[0] == '-o':
return ['-l']
return []
if argc == 4:
if is_brk_char:
return gdb.COMPLETE_FILENAME
(head, tail) = os.path.split(curr)
return getMatchingFiles(head, tail)
if is_brk_char:
return gdb.COMPLETE_NONE
return [] # No more valid options
NOTE!! The completer is not responsible for validation. That should be done once your command's invoke() method is called.