Class: EpubTools::CLI::Runner
- Inherits:
-
Object
- Object
- EpubTools::CLI::Runner
- Defined in:
- lib/epub_tools/cli/runner.rb
Overview
Main runner for the CLI application
Instance Attribute Summary collapse
-
#program_name ⇒ Object
readonly
Returns the value of attribute program_name.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
-
#handle_command(cmd, args = ARGV) ⇒ Boolean
Handle a specific command.
-
#initialize(program_name = nil) ⇒ Runner
constructor
Initialize a new CLI Runner.
-
#run(args = ARGV) ⇒ Boolean
Run the CLI application.
Constructor Details
#initialize(program_name = nil) ⇒ Runner
Initialize a new CLI Runner
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_name ⇒ Object (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 |
#registry ⇒ Object (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
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 = command_config[:default_options].dup required_keys = command_config[:required_keys] builder = OptionBuilder.new(, required_keys) .("Usage: #{program_name} #{cmd} [options]") .with_help_option # Configure command-specific options (cmd, builder) # Parse arguments and run the command = builder.parse(args) command_class = command_config[:class] command_class.new().run true end |
#run(args = ARGV) ⇒ Boolean
Run the CLI application
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 |