Class: GeekDict::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/geekdict/cli.rb

Instance Method Summary collapse

Instance Method Details

#t(word) ⇒ Object



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
# File 'lib/geekdict/cli.rb', line 23

def t(word)
  GeekDict.debugger options[:debug]
  LocalHistory.save word

  # 1. Load config file using the Config module
  config = GeekDict::Config.load_config

  # 2. Determine provider: CLI option > Config file > Default (use Config constants)
  provider = options[:provider]&.downcase || config[:provider] || GeekDict::Config::DEFAULT_PROVIDER

  # 3. Determine model: CLI option > Config file > Default (use Config constants)
  model = options[:model] || config[:model] || GeekDict::Config::DEFAULT_MODEL

  # 4. Validate provider (use Config constants)
  unless GeekDict::Config::ALLOWED_PROVIDERS.include?(provider)
    warn "Warning: Invalid provider '#{provider}' specified. Using default '#{GeekDict::Config::DEFAULT_PROVIDER}'."
    provider = GeekDict::Config::DEFAULT_PROVIDER
    # Reset model if provider changed to default, unless model was explicitly set via CLI
    model = options[:model] || GeekDict::Config::DEFAULT_MODEL # Use default model for the default provider
  end

  # Optional: Output effective settings for debugging/clarity
  puts "Using provider: #{provider}, model: #{model}" if options[:debug]

  # 5. Call the appropriate provider with the word and model
  result = case provider
           when 'openai'
             # TODO: Update OpenAI class to accept model
             GeekDict::OpenAI.translate(word, model: model)
           when 'openrouter'
             # TODO: Update OpenRouter class to accept model
             GeekDict::OpenRouter.translate(word, model: model)
           when 'youdao'
             # Youdao might not use a 'model' in the same way.
             # If it needs config, adjust here. For now, pass nothing extra.
             GeekDict::Youdao.translate(word)
           else
             # This case should technically not be reached due to validation
             warn "Internal Error: Unknown provider '#{provider}'. Aborting."
             exit 1
           end

  puts result
end

#vObject



69
70
71
# File 'lib/geekdict/cli.rb', line 69

def v()
  puts GeekDict::VERSION
end