Class: EpubTools::CLI::OptionBuilder
- Inherits:
-
Object
- Object
- EpubTools::CLI::OptionBuilder
- Defined in:
- lib/epub_tools/cli/option_builder.rb
Overview
Builds and manages command line options
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#required_keys ⇒ Object
readonly
Returns the value of attribute required_keys.
Instance Method Summary collapse
-
#initialize(default_options = {}, required_keys = []) ⇒ OptionBuilder
constructor
Initialize a new OptionBuilder.
-
#parse(args = ARGV) ⇒ Hash
Parse the command line arguments.
-
#with_author_option ⇒ self
Add author option to the parser.
-
#with_banner(text) ⇒ self
Add banner to the option parser.
-
#with_cover_option ⇒ self
Add cover option to the parser.
-
#with_custom_options {|OptionParser, Hash| ... } ⇒ self
Add a custom block to configure options.
-
#with_help_option ⇒ self
Add help option to the parser.
-
#with_input_dir(description = 'Input directory', required = true) ⇒ self
Add input directory option to the parser.
-
#with_input_file(description = 'Input file', required = true) ⇒ self
Add input file option to the parser.
-
#with_option(short, long, description, option_key) ⇒ self
Add a custom option to the parser.
-
#with_output_dir(description = 'Output directory', default = nil) ⇒ self
Add output directory option to the parser.
-
#with_output_file(description = 'Output file', required = true) ⇒ self
Add output file option to the parser.
-
#with_title_option ⇒ self
Add title option to the parser.
-
#with_verbose_option ⇒ self
Add verbose option to the parser.
Constructor Details
#initialize(default_options = {}, required_keys = []) ⇒ OptionBuilder
Initialize a new OptionBuilder
12 13 14 15 16 |
# File 'lib/epub_tools/cli/option_builder.rb', line 12 def initialize( = {}, required_keys = []) = .dup @required_keys = required_keys @parser = OptionParser.new end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/epub_tools/cli/option_builder.rb', line 7 def end |
#parser ⇒ Object (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_keys ⇒ Object (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
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 end |
#with_author_option ⇒ self
Add author option to the parser
98 99 100 101 |
# File 'lib/epub_tools/cli/option_builder.rb', line 98 def @parser.on('-a AUTHOR', '--author AUTHOR', 'Author name (required)') { |v| [:author] = v } self end |
#with_banner(text) ⇒ self
Add banner to the option parser
21 22 23 24 |
# File 'lib/epub_tools/cli/option_builder.rb', line 21 def (text) @parser. = text self end |
#with_cover_option ⇒ self
Add cover option to the parser
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| [:cover_image] = v } self end |
#with_custom_options {|OptionParser, Hash| ... } ⇒ self
Add a custom block to configure options
127 128 129 130 |
# File 'lib/epub_tools/cli/option_builder.rb', line 127 def yield @parser, if block_given? self end |
#with_help_option ⇒ self
Add help option to the parser
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
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| [:input_dir] = v } self end |
#with_input_file(description = 'Input file', required = true) ⇒ self
Add input file option to the parser
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| [:input_file] = v } self end |
#with_option(short, long, description, option_key) ⇒ self
Add a custom option to the parser
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| [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
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})" [:output_dir] = default unless .key?(:output_dir) else desc = "#{description} (required)" end @parser.on('-o DIR', '--output-dir DIR', desc) { |v| [:output_dir] = v } self end |
#with_output_file(description = 'Output file', required = true) ⇒ self
Add output file option to the parser
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| [:output_file] = v } self end |
#with_title_option ⇒ self
Add title option to the parser
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| [:title] = v } self end |
#with_verbose_option ⇒ self
Add verbose option to the parser
38 39 40 41 42 |
# File 'lib/epub_tools/cli/option_builder.rb', line 38 def with_verbose_option [:verbose] = true unless .key?(:verbose) @parser.on('-q', '--quiet', 'Run quietly (default: verbose)') { |v| [:verbose] = !v } self end |