Class: AiCli::CLI
- Inherits:
-
Thor
- Object
- Thor
- AiCli::CLI
- Defined in:
- lib/aicli/cli.rb
Constant Summary collapse
- KNOWN_COMMANDS =
%w[config chat update help version tree].freeze
Class Method Summary collapse
- .exit_on_failure? ⇒ Boolean
- .invoke_prompt(prompt_text, silent: false) ⇒ Object
- .start(given_args = ARGV, config = {}) ⇒ Object
Instance Method Summary collapse
Class Method Details
.exit_on_failure? ⇒ Boolean
10 11 12 |
# File 'lib/aicli/cli.rb', line 10 def self.exit_on_failure? true end |
.invoke_prompt(prompt_text, silent: false) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/aicli/cli.rb', line 54 def self.invoke_prompt(prompt_text, silent: false) pastel = Pastel.new begin config = Helpers::Config.get Helpers::I18n.set_language(config['LANGUAGE']) rescue StandardError Helpers::I18n.set_language('en') end Prompt.run(use_prompt: prompt_text, silent_mode: silent) rescue Helpers::KnownError, StandardError => e puts "\n#{pastel.red('✖')} #{e.}" Helpers::Error.handle_cli_error(e) exit 1 end |
.start(given_args = ARGV, config = {}) ⇒ Object
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 |
# File 'lib/aicli/cli.rb', line 14 def self.start(given_args = ARGV, config = {}) args = given_args.dup # Extract global flags that may appear before the prompt text silent = args.delete('--silent') || args.delete('-s') version_flag = args.delete('--version') || args.delete('-v') help_flag = args.delete('--help') || args.delete('-h') if version_flag puts AiCli::VERSION return end if help_flag && (args.empty? || !KNOWN_COMMANDS.include?(args.first)) new.help return end # Prompt flag: -p / --prompt prompt_from_flag = nil if (idx = args.index('-p') || args.index('--prompt')) prompt_from_flag = args[idx + 1] args.slice!(idx, 2) end first = args.first if first.nil? || !KNOWN_COMMANDS.include?(first) # Treat remaining args as the natural-language prompt prompt_text = prompt_from_flag || args.join(' ') invoke_prompt(prompt_text, silent: !silent.nil?) return end # Rebuild argv for Thor subcommands, preserving silent if relevant thor_args = args thor_args = ['--silent'] + thor_args if silent && first == 'chat' super(thor_args, config) end |
Instance Method Details
#chat ⇒ Object
78 79 80 81 |
# File 'lib/aicli/cli.rb', line 78 def chat init_i18n Commands::Chat.run end |
#config(mode = nil, *key_values) ⇒ Object
72 73 74 75 |
# File 'lib/aicli/cli.rb', line 72 def config(mode = nil, *key_values) init_i18n Commands::Config.run(mode, *key_values) end |