How to get an organizational structure of commands, subcommands, options of a Ruby class that inherits from Thor?

92 Views Asked by At

I'm trying to implement an autocomplete feature for a CLI that inherits from Thor.

Right now I have a bash script written that has hard-coded autocompletions based on the previous word. For example

~ custom_cli actor

The bash script checks that the last word entered is actor and sets the COMPREPLY to further subcommands on pressing TAB.

~ custom_cli actor <TAB>
sub_1 sub_2 sub_3

What I need is that since custom_cli inherits from Thor and on doing custom_cli actor --help I get all the subcommands and options as text (--help is provided as an option in Thor), I should be able to get a organizational structure (like JSON) of all the subcommands and options from that point forward which I can parse in my bash autocompletion script and return options accordingly. For example something of the sort:

custom_cli: [
  actor: [
    sub_1: [--sub_1_option1], 
    sub_2: [--sub_2_option1, --sub_2_option2], 
    sub_3
  ] 
]

If I can get something of this sort I'll be able to know all the future commands beforehand. Is there anything in Thor that can provide something similar to this?

0

There are 0 best solutions below