Class: EpubTools::CLI::OptionBuilder

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

Overview

Builds and manages command line options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_options = {}, required_keys = []) ⇒ OptionBuilder

Initialize a new OptionBuilder

Parameters:

  • default_options (Hash) (defaults to: {})

    Default options to start with

  • required_keys (Array<Symbol>) (defaults to: [])

    Keys that must be present in the final options



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

def initialize(default_options = {}, required_keys = [])
  @options = default_options.dup
  @required_keys = required_keys
  @parser = OptionParser.new
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/epub_tools/cli/option_builder.rb', line 7

def options
  @options
end

#parserObject (readonly)

Returns the value of attribute parser.



7
8
9
# File 'lib/epub_tools/cli/option_builder.rb', line 7

def parser
  @parser
end

#required_keysObject (readonly)

Returns the value of attribute required_keys.



7
8
9
# File 'lib/epub_tools/cli/option_builder.rb', line 7

def required_keys
  @required_keys
end

Instance Method Details

#parse(args = ARGV) ⇒ Hash

Parse the command line arguments

Parameters:

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

    Command line arguments

Returns:

  • (Hash)

    Parsed options

Raises:

  • (SystemExit)

    If required options are missing



136
137
138
139
140
141
142
143
144
# File 'lib/epub_tools/cli/option_builder.rb', line 136

def parse(args = ARGV)
  begin
    @parser.parse!(args.dup)
    validate_required_keys
  rescue ArgumentError => e
    abort "#{e.message}\n#{@parser}"
  end
  @options
end

#with_author_optionself

Add author option to the parser

Returns:

  • (self)

    for method chaining



98
99
100
101
# File 'lib/epub_tools/cli/option_builder.rb', line 98

def with_author_option
  @parser.on('-a AUTHOR', '--author AUTHOR', 'Author name (required)') { |v| @options[:author] = v }
  self
end

#with_banner(text) ⇒ self

Add banner to the option parser

Parameters:

  • text (String)

    Banner text

Returns:

  • (self)

    for method chaining



21
22
23
24
# File 'lib/epub_tools/cli/option_builder.rb', line 21

def with_banner(text)
  @parser.banner = text
  self
end

#with_cover_optionself

Add cover option to the parser

Returns:

  • (self)

    for method chaining



105
106
107
108
# File 'lib/epub_tools/cli/option_builder.rb', line 105

def with_cover_option
  @parser.on('-c PATH', '--cover PATH', 'Cover image file path (optional)') { |v| @options[:cover_image] = v }
  self
end

#with_custom_options {|OptionParser, Hash| ... } ⇒ self

Add a custom block to configure options

Yields:

  • (OptionParser, Hash)

    Yields the parser and options hash

Returns:

  • (self)

    for method chaining



127
128
129
130
# File 'lib/epub_tools/cli/option_builder.rb', line 127

def with_custom_options
  yield @parser, @options if block_given?
  self
end

#with_help_optionself

Add help option to the parser

Returns:

  • (self)

    for method chaining



28
29
30
31
32
33
34
# File 'lib/epub_tools/cli/option_builder.rb', line 28

def with_help_option
  @parser.on('-h', '--help', 'Print this help') do
    puts @parser
    exit
  end
  self
end

#with_input_dir(description = 'Input directory', required = true) ⇒ self

Add input directory option to the parser

Parameters:

  • description (String) (defaults to: 'Input directory')

    Option description

  • required (Boolean) (defaults to: true)

    Whether this option is required

Returns:

  • (self)

    for method chaining



58
59
60
61
62
# File 'lib/epub_tools/cli/option_builder.rb', line 58

def with_input_dir(description = 'Input directory', required = true)
  desc = required ? "#{description} (required)" : description
  @parser.on('-i DIR', '--input-dir DIR', desc) { |v| @options[:input_dir] = v }
  self
end

#with_input_file(description = 'Input file', required = true) ⇒ self

Add input file option to the parser

Parameters:

  • description (String) (defaults to: 'Input file')

    Option description

  • required (Boolean) (defaults to: true)

    Whether this option is required

Returns:

  • (self)

    for method chaining



48
49
50
51
52
# File 'lib/epub_tools/cli/option_builder.rb', line 48

def with_input_file(description = 'Input file', required = true)
  desc = required ? "#{description} (required)" : description
  @parser.on('-i FILE', '--input-file FILE', desc) { |v| @options[:input_file] = v }
  self
end

#with_option(short, long, description, option_key) ⇒ self

Add a custom option to the parser

Parameters:

  • short (String)

    Short option flag

  • long (String)

    Long option flag

  • description (String)

    Option description

  • option_key (Symbol)

    Key in the options hash

  • block (Proc)

    Optional block for custom processing

Returns:

  • (self)

    for method chaining



117
118
119
120
121
122
# File 'lib/epub_tools/cli/option_builder.rb', line 117

def with_option(short, long, description, option_key)
  @parser.on(short, long, description) do |v|
    @options[option_key] = block_given? ? yield(v) : v
  end
  self
end

#with_output_dir(description = 'Output directory', default = nil) ⇒ self

Add output directory option to the parser

Parameters:

  • description (String) (defaults to: 'Output directory')

    Option description

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

    Default value

Returns:

  • (self)

    for method chaining



68
69
70
71
72
73
74
75
76
77
# File 'lib/epub_tools/cli/option_builder.rb', line 68

def with_output_dir(description = 'Output directory', default = nil)
  if default
    desc = "#{description} (default: #{default})"
    @options[:output_dir] = default unless @options.key?(:output_dir)
  else
    desc = "#{description} (required)"
  end
  @parser.on('-o DIR', '--output-dir DIR', desc) { |v| @options[:output_dir] = v }
  self
end

#with_output_file(description = 'Output file', required = true) ⇒ self

Add output file option to the parser

Parameters:

  • description (String) (defaults to: 'Output file')

    Option description

  • required (Boolean) (defaults to: true)

    Whether this option is required

Returns:

  • (self)

    for method chaining



83
84
85
86
87
# File 'lib/epub_tools/cli/option_builder.rb', line 83

def with_output_file(description = 'Output file', required = true)
  desc = required ? "#{description} (required)" : description
  @parser.on('-o FILE', '--output-file FILE', desc) { |v| @options[:output_file] = v }
  self
end

#with_title_optionself

Add title option to the parser

Returns:

  • (self)

    for method chaining



91
92
93
94
# File 'lib/epub_tools/cli/option_builder.rb', line 91

def with_title_option
  @parser.on('-t TITLE', '--title TITLE', 'Book title (required)') { |v| @options[:title] = v }
  self
end

#with_verbose_optionself

Add verbose option to the parser

Returns:

  • (self)

    for method chaining



38
39
40
41
42
# File 'lib/epub_tools/cli/option_builder.rb', line 38

def with_verbose_option
  @options[:verbose] = true unless @options.key?(:verbose)
  @parser.on('-q', '--quiet', 'Run quietly (default: verbose)') { |v| @options[:verbose] = !v }
  self
end