Class: Textus::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/cli.rb,
lib/textus/cli/verb.rb,
lib/textus/cli/group.rb,
lib/textus/cli/verb/mv.rb,
lib/textus/cli/verb/get.rb,
lib/textus/cli/verb/put.rb,
lib/textus/cli/verb/uid.rb,
lib/textus/cli/group/key.rb,
lib/textus/cli/verb/deps.rb,
lib/textus/cli/verb/init.rb,
lib/textus/cli/verb/list.rb,
lib/textus/cli/group/hook.rb,
lib/textus/cli/group/rule.rb,
lib/textus/cli/verb/audit.rb,
lib/textus/cli/verb/blame.rb,
lib/textus/cli/verb/build.rb,
lib/textus/cli/verb/hooks.rb,
lib/textus/cli/verb/intro.rb,
lib/textus/cli/verb/rdeps.rb,
lib/textus/cli/verb/where.rb,
lib/textus/cli/verb/accept.rb,
lib/textus/cli/verb/delete.rb,
lib/textus/cli/verb/doctor.rb,
lib/textus/cli/verb/reject.rb,
lib/textus/cli/verb/schema.rb,
lib/textus/cli/group/schema.rb,
lib/textus/cli/verb/refresh.rb,
lib/textus/cli/group/refresh.rb,
lib/textus/cli/verb/hook_run.rb,
lib/textus/cli/verb/freshness.rb,
lib/textus/cli/verb/published.rb,
lib/textus/cli/verb/rule_list.rb,
lib/textus/cli/verb/schema_diff.rb,
lib/textus/cli/verb/schema_init.rb,
lib/textus/cli/verb/rule_explain.rb,
lib/textus/cli/verb/refresh_stale.rb,
lib/textus/cli/verb/schema_migrate.rb

Defined Under Namespace

Classes: Group, Verb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin:, stdout:, stderr:, cwd:) ⇒ CLI

Returns a new instance of CLI.



28
29
30
31
32
33
34
# File 'lib/textus/cli.rb', line 28

def initialize(stdin:, stdout:, stderr:, cwd:)
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @cwd = cwd
  @root_arg = nil
end

Class Method Details

.const_missing(name) ⇒ Object

Backward-compat constant; callers should prefer ‘CLI.verbs`.



18
19
20
21
22
# File 'lib/textus/cli.rb', line 18

def self.const_missing(name)
  return verbs.freeze if name == :VERBS

  super
end

.run(argv, stdin: $stdin, stdout: $stdout, stderr: $stderr, cwd: Dir.pwd) ⇒ Object



24
25
26
# File 'lib/textus/cli.rb', line 24

def self.run(argv, stdin: $stdin, stdout: $stdout, stderr: $stderr, cwd: Dir.pwd)
  new(stdin: stdin, stdout: stdout, stderr: stderr, cwd: cwd).run(argv)
end

.verbsObject

Auto-derived verb table. Every CLI::Verb (or Group) subclass that declares ‘command_name “X”` and has no `parent_group` is a top-level verb. Sorted alphabetically for stable help output. Adding a new verb requires only a new file declaring its `command_name`.



10
11
12
13
14
15
# File 'lib/textus/cli.rb', line 10

def self.verbs
  Verb.descendants
      .select { |k| k.command_name && k.parent_group.nil? }
      .sort_by(&:command_name)
      .to_h { |k| [k.command_name, k] }
end

Instance Method Details

#run(argv) ⇒ Object



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

def run(argv)
  OptionParser.new { |o| o.on("--root=PATH") { |v| @root_arg = v } }.order!(argv)
  verb = argv.shift
  raise UsageError.new("missing verb") if verb.nil?

  result =
    case verb
    when "--version", "-v" then @stdout.puts(VERSION)
                                0
    when "--help", "-h"    then print_help
                                0
    else
      klass = self.class.verbs[verb] or raise UsageError.new("unknown verb: #{verb}")
      dispatch(klass, argv)
    end

  coerce_exit_code(result)
rescue Textus::Error => e
  emit_error(e)
end