Skip to content

Commit

Permalink
Refactor to use option parser in create_compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Jul 3, 2023
1 parent 666034d commit e3241cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
25 changes: 19 additions & 6 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class Crystal::Command

private def create_compiler(command, no_codegen = false, run = false,
hierarchy = false, cursor_command = false,
single_file = false)
single_file = false, dependencies = false)
compiler = new_compiler
compiler.progress_tracker = @progress_tracker
link_flags = [] of String
Expand Down Expand Up @@ -410,8 +410,14 @@ class Crystal::Command
end
end

opts.on("-f text|json", "--format text|json", "Output format text (default) or json") do |f|
output_format = f
if dependencies
opts.on("-f tree|flat", "--format tree|flat", "Output format tree (default) or flat") do |f|
output_format = f
end
else
opts.on("-f text|json", "--format text|json", "Output format text (default) or json") do |f|
output_format = f
end
end

opts.on("--error-trace", "Show full error trace") do
Expand Down Expand Up @@ -547,9 +553,16 @@ class Crystal::Command
end
end

output_format ||= "text"
unless output_format.in?("text", "json")
error "You have input an invalid format, only text and JSON are supported"
if dependencies
output_format ||= "tree"
unless output_format.in?("tree", "flat")
error "You have input an invalid format, only tree and flat are supported"
end
else
output_format ||= "text"
unless output_format.in?("text", "json")
error "You have input an invalid format, only text and JSON are supported"
end
end

error "maximum number of threads cannot be lower than 1" if compiler.n_threads < 1
Expand Down
18 changes: 2 additions & 16 deletions src/compiler/crystal/tools/dependencies.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,10 @@ require "../syntax/ast"

class Crystal::Command
private def dependencies
dependency_printer = nil
option_parser = parse_with_crystal_opts do |opts|
opts.on("-f FORMAT", "--format FORMAT", "Format the output. Available options: flat, tree (default)") do |format|
case format
when "flat"
dependency_printer = DependencyPrinter.new(STDOUT, flat: true)
when "tree"
dependency_printer = DependencyPrinter.new(STDOUT, flat: false)
else
error "Invalid format: #{format}"
end
end
end

config = create_compiler "tool dependencies", no_codegen: true
config = create_compiler "tool dependencies", no_codegen: true, dependencies: true
config.compiler.no_codegen = true

config.compiler.dependency_printer = dependency_printer || DependencyPrinter.new(STDOUT)
config.compiler.dependency_printer = DependencyPrinter.new(STDOUT, flat: config.output_format == "flat")
config.compile
end
end
Expand Down

0 comments on commit e3241cc

Please sign in to comment.