Class: Ukiryu::CliCommands::OptsCommand

Inherits:
BaseCommand show all
Defined in:
lib/ukiryu/cli_commands/opts_command.rb

Overview

Show options for a tool or specific command

Instance Attribute Summary

Attributes inherited from BaseCommand

#config, #options

Instance Method Summary collapse

Methods inherited from BaseCommand

#apply_cli_options_to_config, #default_register_path, #initialize, #setup_register, #stringify_keys

Constructor Details

This class inherits a constructor from Ukiryu::CliCommands::BaseCommand

Instance Method Details

#run(tool_name, command_name = nil) ⇒ Object

Execute the opts command

Parameters:

  • tool_name (String)

    the tool name

  • command_name (String, nil) (defaults to: nil)

    optional command name



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ukiryu/cli_commands/opts_command.rb', line 11

def run(tool_name, command_name = nil)
  setup_register

  # Use find_by for interface-based discovery (ping -> ping_bsd/ping_gnu)
  tool = Ukiryu::Tool.find_by(tool_name.to_sym)
  error!("Tool not found: #{tool_name}\nAvailable tools: #{Ukiryu::Register.tools.sort.join(', ')}") unless tool

  tool_commands = tool.commands
  error! "No commands defined for #{tool_name}" unless tool_commands

  # Find the command
  cmds = if command_name
           tool_commands.find { |c| c.name.to_s == command_name.to_s || c.name.to_sym == command_name.to_sym }
           cmds ? [cmds] : []
         else
           tool_commands
         end

  cmds.each do |cmd|
    cmd_title = command_name ? "#{tool_name} #{command_name}" : tool_name
    say '', :clear
    say "Options for #{cmd_title}:", :cyan
    say cmd.description.to_s if cmd.description

    # Arguments
    if cmd.arguments && !cmd.arguments.empty?
      say '', :clear
      say 'Arguments:', :yellow
      cmd.arguments.each do |arg|
        name = arg.name || 'unnamed'
        type = arg.type || 'unknown'
        position = arg.position || 'default'
        variadic = arg.variadic ? '(variadic)' : ''

        say "  #{name} (#{type}#{variadic})", :white
        say "    Position: #{position}", :dim if position != 'default'
        say "    Description: #{arg.description}", :dim if arg.description
      end
    end

    # Options
    if cmd.options && !cmd.options.empty?
      say '', :clear
      say 'Options:', :yellow
      cmd.options.each do |opt|
        name = opt.name || 'unnamed'
        cli = opt.cli || 'N/A'
        type = opt.type || 'unknown'
        description = opt.description || ''

        say "  --#{name.ljust(20)} #{cli}", :white
        say "    Type: #{type}", :dim
        say "    #{description}", :dim if description
        say "    Values: #{opt.values.join(', ')}", :dim if opt.values
        say "    Range: #{opt.range.join('..')}", :dim if opt.range
      end
    end

    # Post-options (options between input and output)
    if cmd.post_options && !cmd.post_options.empty?
      say '', :clear
      say 'Post-Options (between input and output):', :yellow
      cmd.post_options.each do |opt|
        name = opt.name || 'unnamed'
        cli = opt.cli || 'N/A'
        type = opt.type || 'unknown'
        description = opt.description || ''

        say "  --#{name.ljust(20)} #{cli}", :white
        say "    Type: #{type}", :dim
        say "    #{description}", :dim if description
      end
    end

    # Flags
    next unless cmd.flags && !cmd.flags.empty?

    say '', :clear
    say 'Flags:', :yellow
    cmd.flags.each do |flag|
      name = flag.name || 'unnamed'
      cli = flag.cli || 'N/A'
      default = flag.default
      default_str = default.nil? ? '' : " (default: #{default})"

      say "  #{cli.ljust(25)} #{name}#{default_str}", :white
      say "    #{flag.description}", :dim if flag.description
    end
  end
end