Module: CommanderIntrospector
- Defined in:
- lib/renuo/cli/helpers/commander_introspector.rb
Constant Summary collapse
- HELP_FLAGS =
["-h", "--help"].freeze
Class Method Summary collapse
- .build_command_data(name, command) ⇒ Object
- .clean(text) ⇒ Object
- .commands_data ⇒ Object
- .extract_examples(command_name, examples) ⇒ Object
-
.extract_options(options) ⇒ Object
rubocop:disable Metrics/AbcSize.
- .first_line(text) ⇒ Object
- .help_entry ⇒ Object
- .pick_command(pair, prefix) ⇒ Object
- .safe_split(text) ⇒ Object
Class Method Details
.build_command_data(name, command) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/renuo/cli/helpers/commander_introspector.rb', line 17 def build_command_data(name, command) { name: name, summary: clean(command.summary), description: first_line(clean(command.description.to_s)), syntax: command.syntax.to_s, options: (command.), examples: extract_examples(name, command.examples) } end |
.clean(text) ⇒ Object
76 77 78 |
# File 'lib/renuo/cli/helpers/commander_introspector.rb', line 76 def clean(text) text.to_s.gsub(/['\[\]]/, "").strip end |
.commands_data ⇒ Object
10 11 12 13 14 15 |
# File 'lib/renuo/cli/helpers/commander_introspector.rb', line 10 def commands_data runner_commands = Commander::Runner.instance.commands entries = runner_commands.reject { |name, _| name.to_s == "help" } .map { |name, cmd| build_command_data(name.to_s, cmd) } (entries + [help_entry]).sort_by { |cmd| cmd[:name] } end |
.extract_examples(command_name, examples) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/renuo/cli/helpers/commander_introspector.rb', line 54 def extract_examples(command_name, examples) return [] if examples.blank? prefix = ["renuo", *command_name.split] examples.filter_map { |pair| pick_command(pair, prefix) }.uniq end |
.extract_options(options) ⇒ Object
rubocop:disable Metrics/AbcSize
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/renuo/cli/helpers/commander_introspector.rb', line 38 def () # rubocop:disable Metrics/AbcSize .filter_map do |opt| switches = opt[:switches] next if switches.intersect?(HELP_FLAGS) long = switches.find { |s| s.start_with?("--") } next unless long { flags: [switches.find { |s| s.match?(/\A-[^-]/) }, long.split.first].compact, description: clean(opt[:description]), argument: switches.filter_map { |s| s[/<([^>]+)>/, 1] }.first } end end |
.first_line(text) ⇒ Object
80 81 82 |
# File 'lib/renuo/cli/helpers/commander_introspector.rb', line 80 def first_line(text) text.split("\n").map(&:strip).reject(&:empty?).first.to_s end |
.help_entry ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/renuo/cli/helpers/commander_introspector.rb', line 28 def help_entry { name: "help", summary: "Display global or command help documentation", description: "Display global or command help documentation", syntax: "renuo help [command]", options: [], examples: [] } end |
.pick_command(pair, prefix) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/renuo/cli/helpers/commander_introspector.rb', line 61 def pick_command(pair, prefix) Array(pair).find do |text| next false unless text.is_a?(String) tokens = safe_split(text) tokens && tokens.first(prefix.size) == prefix end end |
.safe_split(text) ⇒ Object
70 71 72 73 74 |
# File 'lib/renuo/cli/helpers/commander_introspector.rb', line 70 def safe_split(text) Shellwords.split(text) rescue ArgumentError nil end |