bin/code in code-ruby-0.15.15 vs bin/code in code-ruby-0.15.16
- old
+ new
@@ -1,108 +1,71 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
-require "optparse"
require_relative "../lib/code-ruby"
+require "dorian/arguments"
-options = {
- timeout: 0,
- profile: false,
- profiler: "text",
- input: "",
- parse: false
-}
+parsed =
+ Dorian::Arguments.parse(
+ input: {
+ type: :string,
+ alias: :i
+ },
+ parse: {
+ type: :boolean,
+ alias: :p
+ },
+ profile: :boolean,
+ profiler: :string,
+ timeout: {
+ type: :integer,
+ alias: :t
+ },
+ version: {
+ type: :boolean,
+ alias: :v
+ },
+ help: {
+ type: :boolean,
+ alias: :h
+ }
+ )
-argv =
- OptionParser
- .new do |opts|
- opts.banner = "Usage: code INPUT\n\n"
+abort Code::Version.to_s if parsed.options.version
+abort parsed.help if parsed.options.help
- opts.on("-v", "--version", "Version of Code") do |_input|
- puts Code::Version
- exit
- end
+input = parsed.options.input.to_s
+input = File.read(input) if File.exist?(input)
+input += parsed.arguments.join(" ")
+input += parsed.files.map { |file| File.read(file) }.join
- opts.on(
- "-i INPUT",
- "--input INPUT",
- "Input in the Code language (String or File)"
- ) do |input|
- input = File.read(input) if File.exist?(input)
+abort parsed.help if input.empty?
- options[:input] = input
- end
+profile = parsed.options.profile || parsed.options.profiler
+require "ruby-prof" if profile
- opts.on("-p", "--parse", "Parser tree for input") do |parse|
- options[:parse] = parse
- end
+RubyProf.start if profile
- opts.on(
- "-t TIMEOUT",
- "--timeout TIMEOUT",
- "Set timeout in seconds"
- ) { |timeout| options[:timeout] = timeout.to_f }
-
- opts.on(
- "-z TIME_ZONE",
- "--time-zone TIME_ZONE",
- "Set time zone"
- ) { |time_zone| Time.zone = time_zone }
-
- opts.on("--profile", "Profile Ruby code") do |_timeout|
- require "ruby-prof"
- options[:profile] = true
- end
-
- opts.on(
- "--profiler TYPE",
- "Profiler output type (text (default) or html)"
- ) do |profiler|
- require "ruby-prof"
- options[:profile] = true
- options[:profiler] = profiler
- end
- end
- .parse!
-
-if options[:input].empty?
- options[:input] = argv.join(" ")
- options[:input] = File.read(options[:input]) if File.exist?(options[:input])
-end
-
-abort <<~HELP if options[:input].empty?
- Usage: code INPUT
-
- -v, --version Version of Code
- -i, --input INPUT Input in the Code language (String or File)
- -p, --parse Parser tree for input
- -t, --timeout TIMEOUT Set timeout in seconds
- --profile Profile Ruby code
- --profiler TYPE Profiler output type (text (default) or html)
-HELP
-
-RubyProf.start if options[:profile]
-
-if options[:parse]
- pp Code::Parser.parse(options[:input]).to_raw
+if parsed.options.parse
+ pp Code::Parser.parse(input).to_raw
else
- print Code.evaluate(
- options[:input],
- output: $stdout,
- error: $stderr,
- timeout: options[:timeout]
- )
+ print(
+ Code.evaluate(
+ input,
+ output: $stdout,
+ error: $stderr,
+ timeout: parsed.options.timeout
+ )
+ )
end
-if options[:profile]
+if profile
result = RubyProf.stop
- if options[:profiler] == "text"
- printer = RubyProf::FlatPrinter.new(result)
- printer.print($stdout)
- elsif options[:profiler] == "html"
+ if parsed.options.profiler == "html"
printer = RubyProf::GraphHtmlPrinter.new(result)
printer.print($stdout)
else
- abort "#{options[:profiler]} not recognized"
+ printer = RubyProf::FlatPrinter.new(result)
+ printer.print($stdout)
end
end