lib/please/cli.rb in openai-please-0.1.2 vs lib/please/cli.rb in openai-please-0.1.3

- old
+ new

@@ -1,28 +1,46 @@ # frozen_string_literal: true require 'English' require 'tty-prompt' +require 'yaml' require 'optparse' require 'please' require 'tempfile' +USAGE = 'Usage: please [options] <instruction>' +CONFIG_FILE_PATH = File.expand_path('~/.config/please/config.yml') + begin tty_prompt = TTY::Prompt.new options = { show_prompt: false, send_pwd: true, send_uname: true, send_ls: true, + access_token: ENV.fetch('OPENAI_ACCESS_TOKEN', nil), + examples: [], + skip_default_examples: false, } - USAGE = 'Usage: please [options] <instruction>' + if File.exist?(CONFIG_FILE_PATH) + begin + options.merge! YAML.load_file(CONFIG_FILE_PATH).transform_keys(&:to_sym) + options[:examples].each { |example| example.transform_keys!(&:to_sym) } + rescue StandardError + tty_prompt.warn 'Could not parse config file. Ignoring.' + end + end OptionParser.new do |opts| opts.banner = USAGE + opts.on('--show-config-path', 'Output the location of the config file, and then exit') do |v| + options[:show_config_path] = v + end + opts.on('--show-prompt', 'Output the prompt that would ordinarily be sent to OpenAI Codex, and then exit') do |v| options[:show_prompt] = v end opts.on('--[no-]send-pwd', 'Send the result of `pwd` as part of the prompt') do |v| @@ -36,15 +54,20 @@ opts.on('--[no-]send-ls', 'Send the result of `ls -a` as part of the prompt') do |v| options[:send_ls] = v end end.parse! - access_token = ENV.fetch('OPENAI_ACCESS_TOKEN') do - tty_prompt.error 'Ensure the OPENAI_ACCESS_TOKEN environment variable is set' + if options[:show_config_path] + tty_prompt.say CONFIG_FILE_PATH + exit + end + + if options[:access_token].nil? + tty_prompt.error "Access token not found. Set it in #{CONFIG_FILE_PATH} or $OPENAI_ACCESS_TOKEN." exit 1 end - codex_service = Please::OpenAI::CodexService.new(access_token: access_token) + codex_service = Please::OpenAI::CodexService.new(access_token: options[:access_token]) instruction = ARGV.join(' ') if instruction.empty? tty_prompt.error USAGE