Class: Opener::KafNafParser::CLI
- Inherits:
-
Object
- Object
- Opener::KafNafParser::CLI
- Defined in:
- lib/opener/kaf_naf_parser/cli.rb
Overview
CLI wrapper around LanguageIdentifier using OptionParser.
Instance Attribute Summary collapse
- #option_parser ⇒ OptionParser readonly
- #options ⇒ Hash readonly
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ CLI
constructor
A new instance of CLI.
- #run(input) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CLI
Returns a new instance of CLI.
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 |
# File 'lib/opener/kaf_naf_parser/cli.rb', line 17 def initialize( = {}) = DEFAULT_OPTIONS.merge() @option_parser = OptionParser.new do |opts| opts.program_name = 'kaf-naf-parser' opts.summary_indent = ' ' opts.on('-h', '--help', 'Shows this help message') do show_help end opts.on('-v', '--version', 'Shows the current version') do show_version end opts.on('-k', '--tokaf', 'Parses input to KAF') do [:conversion] = "to-kaf" end opts.on('-n', '--tonaf', 'Parses input to NAF') do [:conversion] = "to-naf" end opts.on('-l', '--log', 'Enable logging to STDERR') do [:logging] = true end opts.separator "\nExamples:\n\ncat example.kaf | \#{opts.program_name} --tonaf # Basic usage\ncat example.kaf | \#{opts.program_name} --tonaf -l # Logs information to STDERR\n" end end |
Instance Attribute Details
#option_parser ⇒ OptionParser (readonly)
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 |
# File 'lib/opener/kaf_naf_parser/cli.rb', line 11 class CLI attr_reader :options, :option_parser ## # @param [Hash] options # def initialize( = {}) = DEFAULT_OPTIONS.merge() @option_parser = OptionParser.new do |opts| opts.program_name = 'kaf-naf-parser' opts.summary_indent = ' ' opts.on('-h', '--help', 'Shows this help message') do show_help end opts.on('-v', '--version', 'Shows the current version') do show_version end opts.on('-k', '--tokaf', 'Parses input to KAF') do [:conversion] = "to-kaf" end opts.on('-n', '--tonaf', 'Parses input to NAF') do [:conversion] = "to-naf" end opts.on('-l', '--log', 'Enable logging to STDERR') do [:logging] = true end opts.separator "\nExamples:\n\ncat example.kaf | \#{opts.program_name} --tonaf # Basic usage\ncat example.kaf | \#{opts.program_name} --tonaf -l # Logs information to STDERR\n" end end ## # @param [String] input # def run(input) option_parser.parse!([:args]) tagger = KafNafParser.new() stdout, stderr, process = tagger.run(input) if process.success? puts stdout if [:logging] and !stderr.empty? STDERR.puts(stderr) end else abort stderr end end private ## # Shows the help message and exits the program. # def show_help abort option_parser.to_s end ## # Shows the version and exits the program. # def show_version abort "#{option_parser.program_name} v#{VERSION} on #{RUBY_DESCRIPTION}" end end |
#options ⇒ Hash (readonly)
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 |
# File 'lib/opener/kaf_naf_parser/cli.rb', line 11 class CLI attr_reader :options, :option_parser ## # @param [Hash] options # def initialize( = {}) = DEFAULT_OPTIONS.merge() @option_parser = OptionParser.new do |opts| opts.program_name = 'kaf-naf-parser' opts.summary_indent = ' ' opts.on('-h', '--help', 'Shows this help message') do show_help end opts.on('-v', '--version', 'Shows the current version') do show_version end opts.on('-k', '--tokaf', 'Parses input to KAF') do [:conversion] = "to-kaf" end opts.on('-n', '--tonaf', 'Parses input to NAF') do [:conversion] = "to-naf" end opts.on('-l', '--log', 'Enable logging to STDERR') do [:logging] = true end opts.separator "\nExamples:\n\ncat example.kaf | \#{opts.program_name} --tonaf # Basic usage\ncat example.kaf | \#{opts.program_name} --tonaf -l # Logs information to STDERR\n" end end ## # @param [String] input # def run(input) option_parser.parse!([:args]) tagger = KafNafParser.new() stdout, stderr, process = tagger.run(input) if process.success? puts stdout if [:logging] and !stderr.empty? STDERR.puts(stderr) end else abort stderr end end private ## # Shows the help message and exits the program. # def show_help abort option_parser.to_s end ## # Shows the version and exits the program. # def show_version abort "#{option_parser.program_name} v#{VERSION} on #{RUBY_DESCRIPTION}" end end |
Instance Method Details
#run(input) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/opener/kaf_naf_parser/cli.rb', line 57 def run(input) option_parser.parse!([:args]) tagger = KafNafParser.new() stdout, stderr, process = tagger.run(input) if process.success? puts stdout if [:logging] and !stderr.empty? STDERR.puts(stderr) end else abort stderr end end |