Class: Lilac::CLI::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/lilac/cli/command.rb

Overview

Entry point for exe/lilac. Routes argv to the matching Subcommand::* handler; each subcommand owns its own OptionParser and run method. The outer rescue here keeps the user-facing error surface uniform (Builder::Error / ConfigLoader::LoadError / etc. → "lilac: " + exit 1).

Constant Summary collapse

SUBCOMMAND_HANDLERS =
{
  "build"         => Subcommand::Build,
  "dev"           => Subcommand::Dev,
  "preview"       => Subcommand::Preview,
  "new"           => Subcommand::New,
  "doctor"        => Subcommand::Doctor,
  "package-build" => Subcommand::PackageBuild,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(argv, out: $stdout, err: $stderr) ⇒ Command

Returns a new instance of Command.



27
28
29
30
31
# File 'lib/lilac/cli/command.rb', line 27

def initialize(argv, out: $stdout, err: $stderr)
  @argv = argv.dup
  @out = out
  @err = err
end

Instance Method Details

#runObject

Returns an exit status (0 for success).



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lilac/cli/command.rb', line 34

def run
  name = @argv.shift || "help"

  return print_version if name == "--version"
  return run_help if %w[help -h --help].include?(name)

  if (handler = SUBCOMMAND_HANDLERS[name])
    handler.new(@argv, out: @out, err: @err).run
  else
    @err.puts "lilac: unknown command #{name.inspect}"
    @err.puts
    print_help(io: @err)
    1
  end
rescue Builder::Error, SFC::ParseError, Scaffold::Error,
       ConfigLoader::LoadError, PreviewServer::Error,
       PackageBuild::Error => e
  @err.puts "lilac: #{e.message}"
  1
end

#run_helpObject

lilac help shows the top-level help; lilac help <subcmd> shows the per-subcommand option list (sourced from the same OptionParser that the subcommand uses, so it stays in sync).



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lilac/cli/command.rb', line 58

def run_help
  topic = @argv.shift
  case topic
  when nil, "help", "-h", "--help"
    print_help
    0
  else
    if (handler = SUBCOMMAND_HANDLERS[topic])
      @out.puts handler.help_text
      0
    else
      @err.puts "lilac help: unknown command #{topic.inspect}"
      @err.puts
      print_help(io: @err)
      1
    end
  end
end