module Aigu class CLI COMMANDS = { 'rails-import' => 'RailsImporter', 'rails-export' => 'RailsExporter', 'android-import' => 'AndroidImporter', 'android-export' => 'AndroidExporter', 'core-import' => 'CoreImporter', 'core-export' => 'CoreExporter', 'ios-import' => 'IOSImporter', 'ios-export' => 'IOSExporter', 'ember-import' => 'EmberImporter', 'ember-export' => 'EmberExporter', 'merge' => 'Merger', 'unmerge' => 'Unmerger' } def initialize(env, argv) @env = env @command = argv.first =~ /^[^-]/ ? argv.shift : nil @command = @command.gsub('_', '-') if @command @argv = argv @options = {} @options = parse_options_from_yaml(@options) @options = parse_options_from_arguments(@options) end def run if COMMANDS[@command] service_class = Aigu.const_get(COMMANDS[@command]) else puts "The #{@command} command doesn’t exist. Nice try." exit 0 end service_object = service_class.new(@options) service_object.process! end protected def parse_options_from_yaml(options) file = File.join(Dir.pwd, '.aigu.yml') if File.exist?(file) # Load YAML content content = YAML.load_file(file) # Symbolize keys and merge with existing options options = options.merge(content.symbolize_keys) end options end # rubocop:disable Metrics/MethodLength def parse_options_from_arguments(options) OptionParser.new do |opts| opts.banner = < [options]" Commands: #{COMMANDS.keys.map { |command| " #{command}" }.join("\n")} Options: DOC opts.on('--input-directory=', 'The directory in which the Rails YAML localization files are stored.') do |directory| options[:'input-directory'] = directory end opts.on('--output-directory=', 'The directory in which the Rails YAML localization files will be generated.') do |directory| options[:'output-directory'] = directory end opts.on('--input-file=', 'The JSON file generated by Accent.') do |file| options[:'input-file'] = file end opts.on('--output-file=', 'The JSON file that will be generated for Accent.') do |file| options[:'output-file'] = file end opts.on('--locale=', 'The locale to use. Files will be processed/generated using the "..yml" pattern.') do |locale| options[:locale] = locale end opts.on('--ignore=', 'Patterns to ignore, separated by commas') do |ignore| options[:ignore] = ignore.split(',') end opts.on('--accent-api-key=', 'Accent API key') do |key| options[:'accent-api-key'] = key end opts.on('--accent-url=', 'Accent URL (ex: http://accent.mirego.com)') do |url| options[:'accent-url'] = url end opts.on_tail('-h', '--help', 'Show this message') do puts opts exit end end.parse! @argv options end # rubocop:enable Metrics/MethodLength end end