Class: Samovar::Command
- Inherits:
-
Object
- Object
- Samovar::Command
- Defined in:
- lib/samovar/command.rb
Class Attribute Summary collapse
-
.description ⇒ Object
Returns the value of attribute description.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
- .[](*input, **options) ⇒ Object
- .append(row) ⇒ Object
- .call(input = ARGV) ⇒ Object
- .command_line(name) ⇒ Object
- .many(*arguments, **options) ⇒ Object
- .nested(*arguments, **options) ⇒ Object
- .one(*arguments, **options) ⇒ Object
- .options(*arguments, **options, &block) ⇒ Object
-
.parse(input) ⇒ Object
The top level entry point for parsing ARGV.
- .split(*arguments, **options) ⇒ Object
- .table ⇒ Object
- .usage(rows, name) ⇒ Object
Instance Method Summary collapse
- #[](*input) ⇒ Object
-
#initialize(input = nil, name: File.basename($0), parent: nil) ⇒ Command
constructor
A new instance of Command.
- #parse(input) ⇒ Object
- #print_usage(output: $stderr, formatter: Output::UsageFormatter, &block) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(input = nil, name: File.basename($0), parent: nil) ⇒ Command
Returns a new instance of Command.
111 112 113 114 115 116 |
# File 'lib/samovar/command.rb', line 111 def initialize(input = nil, name: File.basename($0), parent: nil) @name = name @parent = parent parse(input) if input end |
Class Attribute Details
.description ⇒ Object
Returns the value of attribute description.
56 57 58 |
# File 'lib/samovar/command.rb', line 56 def description @description end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
122 123 124 |
# File 'lib/samovar/command.rb', line 122 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
123 124 125 |
# File 'lib/samovar/command.rb', line 123 def parent @parent end |
Class Method Details
.[](*input, **options) ⇒ Object
51 52 53 |
# File 'lib/samovar/command.rb', line 51 def self.[](*input, **) self.new(input, **) end |
.append(row) ⇒ Object
63 64 65 66 67 |
# File 'lib/samovar/command.rb', line 63 def self.append(row) attr_accessor(row.key) if row.respond_to?(:key) self.table << row end |
.call(input = ARGV) ⇒ Object
34 35 36 37 38 |
# File 'lib/samovar/command.rb', line 34 def self.call(input = ARGV) if command = self.parse(input) command.call end end |
.command_line(name) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/samovar/command.rb', line 103 def self.command_line(name) if table = self.table.merged "#{name} #{table.merged.usage}" else name end end |
.many(*arguments, **options) ⇒ Object
81 82 83 |
# File 'lib/samovar/command.rb', line 81 def self.many(*arguments, **) append Many.new(*arguments, **) end |
.nested(*arguments, **options) ⇒ Object
73 74 75 |
# File 'lib/samovar/command.rb', line 73 def self.nested(*arguments, **) append Nested.new(*arguments, **) end |
.one(*arguments, **options) ⇒ Object
77 78 79 |
# File 'lib/samovar/command.rb', line 77 def self.one(*arguments, **) append One.new(*arguments, **) end |
.options(*arguments, **options, &block) ⇒ Object
69 70 71 |
# File 'lib/samovar/command.rb', line 69 def self.(*arguments, **, &block) append Options.parse(*arguments, **, &block) end |
.parse(input) ⇒ Object
The top level entry point for parsing ARGV.
41 42 43 44 45 46 47 48 49 |
# File 'lib/samovar/command.rb', line 41 def self.parse(input) self.new(input) rescue Error => error error.command.print_usage(output: $stderr) do |formatter| formatter.map(error) end return nil end |
.split(*arguments, **options) ⇒ Object
85 86 87 |
# File 'lib/samovar/command.rb', line 85 def self.split(*arguments, **) append Split.new(*arguments, **) end |
.table ⇒ Object
59 60 61 |
# File 'lib/samovar/command.rb', line 59 def self.table @table ||= Table.nested(self) end |
.usage(rows, name) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/samovar/command.rb', line 89 def self.usage(rows, name) rows.nested(name, self) do |rows| return unless table = self.table.merged table.each do |row| if row.respond_to?(:usage) row.usage(rows) else rows << row end end end end |
Instance Method Details
#[](*input) ⇒ Object
125 126 127 |
# File 'lib/samovar/command.rb', line 125 def [](*input) self.dup.tap{|command| command.parse(input)} end |
#parse(input) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/samovar/command.rb', line 129 def parse(input) self.class.table.merged.parse(input, self) if input.empty? return self else raise InvalidInputError.new(self, input) end end |
#print_usage(output: $stderr, formatter: Output::UsageFormatter, &block) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/samovar/command.rb', line 139 def print_usage(output: $stderr, formatter: Output::UsageFormatter, &block) rows = Output::Rows.new self.class.usage(rows, @name) formatter.print(rows, output, &block) end |
#to_s ⇒ Object
118 119 120 |
# File 'lib/samovar/command.rb', line 118 def to_s self.class.name end |