Class: EpubTools::CLI::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/epub_tools/cli/runner.rb

Overview

Main runner for the CLI application

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program_name = nil) ⇒ Runner

Initialize a new CLI Runner

Parameters:

  • program_name (String) (defaults to: nil)

    Name of the program



12
13
14
15
# File 'lib/epub_tools/cli/runner.rb', line 12

def initialize(program_name = nil)
  @registry = CommandRegistry.new
  @program_name = program_name || File.basename($PROGRAM_NAME)
end

Instance Attribute Details

#program_nameObject (readonly)

Returns the value of attribute program_name.



8
9
10
# File 'lib/epub_tools/cli/runner.rb', line 8

def program_name
  @program_name
end

#registryObject (readonly)

Returns the value of attribute registry.



8
9
10
# File 'lib/epub_tools/cli/runner.rb', line 8

def registry
  @registry
end

Instance Method Details

#handle_command(cmd, args = ARGV) ⇒ Boolean

Handle a specific command

Parameters:

  • cmd (String)

    Command name

  • args (Array<String>) (defaults to: ARGV)

    Command line arguments

Returns:

  • (Boolean)

    true if the command was run successfully



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/epub_tools/cli/runner.rb', line 42

def handle_command(cmd, args = ARGV)
  command_config = registry.get(cmd)
  return false unless command_config

  options = command_config[:default_options].dup
  required_keys = command_config[:required_keys]

  builder = OptionBuilder.new(options, required_keys)
                         .with_banner("Usage: #{program_name} #{cmd} [options]")
                         .with_help_option

  # Configure command-specific options
  configure_command_options(cmd, builder)

  # Parse arguments and run the command
  options = builder.parse(args)
  command_class = command_config[:class]
  command_class.new(options).run
  true
end

#run(args = ARGV) ⇒ Boolean

Run the CLI application

Parameters:

  • args (Array<String>) (defaults to: ARGV)

    Command line arguments

Returns:

  • (Boolean)

    true if the command was run successfully



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/epub_tools/cli/runner.rb', line 20

def run(args = ARGV)
  # Handle global version flag
  if ['-v', '--version'].include?(args[0])
    puts EpubTools::VERSION
    exit 0
  end

  commands = registry.available_commands

  if args.empty? || !commands.include?(args[0])
    print_usage(commands)
    exit 1
  end

  cmd = args.shift
  handle_command(cmd, args)
end