Class: Classifier::Keywords::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/classifier/keywords/cli.rb

Defined Under Namespace

Classes: UsageError

Instance Method Summary collapse

Constructor Details

#initialize(args, stdin: nil) ⇒ CLI

Returns a new instance of CLI.

RBS:

  • @args: Array[String]

  • @stdin: String?

  • @options: Hash[Symbol, untyped]

  • @output: Array[String]

  • @error: Array[String]

  • @exit_code: Integer

  • @parser: OptionParser



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

def initialize(args, stdin: nil)
  @args = args.dup
  @stdin = stdin
  @options = {
    model: File.expand_path('./keywords.json'),
    top: nil,
    quiet: false,
    min_df: 1,
    max_df: 1.0,
    ngram_range: [1, 1]
  }
  @output = []
  @error = []
  @exit_code = 0
end

Instance Method Details

#runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/classifier/keywords/cli.rb', line 37

def run
  parse_options
  execute_command
  { output: @output.join("\n"), error: @error.join("\n"), exit_code: @exit_code }
rescue OptionParser::InvalidOption, OptionParser::MissingArgument,
       OptionParser::InvalidArgument, UsageError => e
  @error << "Error: #{e.message}"
  @exit_code = 2
  { output: @output.join("\n"), error: @error.join("\n"), exit_code: @exit_code }
rescue StandardError => e
  @error << "Error: #{e.message}"
  @exit_code = 1
  { output: @output.join("\n"), error: @error.join("\n"), exit_code: @exit_code }
end