Module: Canopus::CLI

Defined in:
lib/canopus/cli.rb,
sig/canopus.rbs

Class Method Summary collapse

Class Method Details

.main(argv = ARGV, output: $stdout, error: $stderr) ⇒ Integer

Parameters:

  • arguments (Array[String])
  • output: (IO, StringIO) (defaults to: $stdout)
  • error: (IO, StringIO) (defaults to: $stderr)

Returns:

  • (Integer)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/canopus/cli.rb', line 8

def self.main(argv = ARGV, output: $stdout, error: $stderr)
  options = {width: 1100, height: 760, root: Dir.pwd, platform: nil, gpu: nil, plugins: [], grants: []}
  parser = OptionParser.new do |flags|
    flags.banner = "Usage: canopus [options] [files...]"
    flags.on("--project DIRECTORY", "Project root") { |value| options[:root] = value }
    flags.on("--headless PNG", "Render to a PNG without opening a window") { |value| options[:png] = value; options[:platform] = :headless }
    flags.on("--tui", "Run in the current terminal") { options[:platform] = :tui }
    flags.on("--backend BACKEND", %w[metal opengl], "GPU backend") { |value| options[:gpu] = value.to_sym }
    flags.on("--size WIDTHxHEIGHT", /\A\d+x\d+\z/, "Window dimensions") { |value| options[:width], options[:height] = value.split("x").map(&:to_i) }
    flags.on("--settings JSONC", "Settings file") { |value| options[:settings] = value }
    flags.on("--session JSON", "Restore and save a workspace session") { |value| options[:session] = value }
    flags.on("--replay JSON", "Replay text, key, and action input records") { |value| options[:replay] = value }
    flags.on("--glyph-cache DIRECTORY", "Reuse a checksummed glyph atlas in this directory") { |value| options[:glyph_cache] = value }
    flags.on("--profile JSON_PATH", "Save frame timings and sampled Ruby stacks on exit") { |value| options[:profile] = value }
    flags.on("--trace-allocations", "Trace live allocation sites (slow; requires --profile)") { options[:trace_allocations] = true }
    flags.on("--crash-report JSON_PATH", "Save exceptions locally; review before sharing") { |value| options[:crash_report] = value }
    flags.on("--wait", "Wait until the opened files are closed") { options[:wait] = true }
    flags.on("--vim", "Enable Vim keybindings") { options[:vim] = true }
    flags.on("--plugin RUBY", "Load a Ruby plugin (requires --trust-plugins)") { |value| options[:plugins] << value }
    flags.on("--trust-plugins", "Allow the selected plugins to execute trusted Ruby code") { options[:trust_plugins] = true }
    flags.on("--grant PERMISSION", Plugins::PERMISSIONS, "Grant a plugin API permission (repeatable)") { |value| options[:grants] << value }
    flags.on("--plugins-in-process", "Run trusted plugins in the editor process") { options[:plugins_in_process] = true }
    flags.on("--version") { output.puts(VERSION); return 0 }
    flags.on("--help") { output.puts(flags); return 0 }
  end
  files = parser.parse(argv.dup)
  validate_cli_options(options, files)
  run_cli(options, files, output: output, error: error)
rescue OptionParser::ParseError, StandardError => exception
  error.puts("canopus: #{exception.message}")
  1
end