class Schematrix::CLI
The schematrix command line interface. Parses ARGV through tty-option and runs Schematrix.generate with the parsed settings. The user-facing manual for every option lives in docs/cli.md.
Public Instance Methods
Source
# File lib/schematrix/cli.rb, line 123 def run Schematrix.logger = TTY::Logger.new do |config| verbosity = [params[:verbose].count(&:itself), 2].min config.level = %i[warn info debug][verbosity] end if params[:help] print help elsif params.errors.any? Schematrix.logger&.fatal params.errors.summary exit 1 else Schematrix.logger&.debug "CLI arguments: \n", params.to_h.pretty_inspect input_files = Array(params[:input]) module_name = params[:module] generator_names = Array(params[:generators]) unknown = generator_names - GENERATORS.keys unless unknown.empty? names = unknown.join(", ") Schematrix.logger&.fatal "Unknown generators: #{names}" exit 1 end enums = params[:enums]&.to_sym unless enums.nil? || %i[classes unions].include?(enums) Schematrix.logger&.fatal "Unknown enums mode: #{params[:enums]}" exit 1 end output = Array(params[:output]) unless [0, 1, generator_names.size].include?(output.size) message = "The specified amount of output directories " \ "doesn't match the amount of generators" Schematrix.logger&.fatal message exit 1 end output << "generated" if output.empty? output_dirs = ( if output.size == 1 Array.new(generator_names.size, output.first) else output end ) generators = output_dirs .zip(generator_names) .uniq .map do |(output_dir, name)| generator_class = GENERATORS.fetch(name) generator_class.new( output_dir, module_name, format: true, command: "schematrix", enums:, immutable: params[:immutable] && generator_class.immutable_supported?, ) end Schematrix.generate( input_files:, generators:, closed_shapes: params[:closed_shapes], schema_dirs: resolved_schema_dirs, ) end rescue StandardError => e Schematrix.logger&.fatal "Error: ", e exit 1 end
Parses nothing itself; call parse first, then run. Prints help when asked, refuses invalid arguments and unknown generator names, pairs output directories with generators (zero -o flags mean generated, one -o is shared by all generators, one per -g pairs in flag order), and hands everything to Schematrix.generate. Exits 1 on any failure, with the reason on the fatal log level.