Class: Textus::CLI::Verb
- Inherits:
-
Object
- Object
- Textus::CLI::Verb
- Defined in:
- lib/textus/cli/verb.rb
Overview
Subclasses must implement #call(store) and return an integer exit code. Use #emit(obj) for normal JSON output (returns 0). Subclasses that don’t need a Textus store (e.g. Init) override ‘.needs_store?` to return false; dispatch will pass nil instead.
Direct Known Subclasses
Accept, Action, Build, Delete, Deps, DoctorVerb, Extensions, Get, Group, InitVerb, IntroVerb, List, Migrate, MigrateKeysVerb, Mv, Published, Put, Rdeps, RefreshVerb, SchemaDiff, SchemaInit, SchemaMigrate, SchemaVerb, Stale, Uid, Where
Instance Attribute Summary collapse
-
#positional ⇒ Object
readonly
Returns the value of attribute positional.
Class Method Summary collapse
- .inherited(subclass) ⇒ Object
- .needs_store? ⇒ Boolean
- .option(name, optspec) ⇒ Object
- .options ⇒ Object
Instance Method Summary collapse
-
#emit(obj, exit_code: 0) ⇒ Object
Hashes get “protocol” => PROTOCOL prepended unless they already carry one (Store envelopes do).
-
#initialize(stdin:, stdout:, stderr:, cwd: nil) ⇒ Verb
constructor
A new instance of Verb.
- #parse(argv) ⇒ Object
Constructor Details
#initialize(stdin:, stdout:, stderr:, cwd: nil) ⇒ Verb
Returns a new instance of Verb.
31 32 33 34 35 36 |
# File 'lib/textus/cli/verb.rb', line 31 def initialize(stdin:, stdout:, stderr:, cwd: nil) @stdin = stdin @stdout = stdout @stderr = stderr @cwd = cwd end |
Instance Attribute Details
#positional ⇒ Object (readonly)
Returns the value of attribute positional.
51 52 53 |
# File 'lib/textus/cli/verb.rb', line 51 def positional @positional end |
Class Method Details
.inherited(subclass) ⇒ Object
25 26 27 28 |
# File 'lib/textus/cli/verb.rb', line 25 def inherited(subclass) super subclass.instance_variable_set(:@options, []) end |
.needs_store? ⇒ Boolean
21 22 23 |
# File 'lib/textus/cli/verb.rb', line 21 def needs_store? true end |
.option(name, optspec) ⇒ Object
12 13 14 15 |
# File 'lib/textus/cli/verb.rb', line 12 def option(name, optspec) << [name, optspec] attr_accessor(name) end |
.options ⇒ Object
17 18 19 |
# File 'lib/textus/cli/verb.rb', line 17 def @options ||= [] end |
Instance Method Details
#emit(obj, exit_code: 0) ⇒ Object
Hashes get “protocol” => PROTOCOL prepended unless they already carry one (Store envelopes do). Caller’s value wins on collision.
55 56 57 58 59 |
# File 'lib/textus/cli/verb.rb', line 55 def emit(obj, exit_code: 0) payload = obj.is_a?(Hash) ? { "protocol" => PROTOCOL }.merge(obj) : obj @stdout.puts(JSON.generate(payload)) exit_code end |
#parse(argv) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/textus/cli/verb.rb', line 38 def parse(argv) fmt = "json" OptionParser.new do |o| self.class..each do |name, optspec| o.on(optspec) { |v| public_send(:"#{name}=", v) } end o.on("--format=FMT") { |v| fmt = v } end.permute!(argv) raise UsageError.new("only --format=json is supported in v1") unless fmt == "json" @positional = argv.dup end |