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.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#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, output: nil) ⇒ Command
constructor
A new instance of Command.
- #parse(input) ⇒ Object
- #print_usage(output: self.output, formatter: Output::UsageFormatter, &block) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(input = nil, name: File.basename($0), parent: nil, output: nil) ⇒ Command
Returns a new instance of Command.
101 102 103 104 105 106 107 |
# File 'lib/samovar/command.rb', line 101 def initialize(input = nil, name: File.basename($0), parent: nil, output: nil) @name = name @parent = parent @output = output parse(input) if input end |
Class Attribute Details
.description ⇒ Object
Returns the value of attribute description.
41 42 43 |
# File 'lib/samovar/command.rb', line 41 def description @description end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
119 120 121 |
# File 'lib/samovar/command.rb', line 119 def name @name end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
109 110 111 |
# File 'lib/samovar/command.rb', line 109 def output @output end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
120 121 122 |
# File 'lib/samovar/command.rb', line 120 def parent @parent end |
Class Method Details
.[](*input, **options) ⇒ Object
36 37 38 |
# File 'lib/samovar/command.rb', line 36 def self.[](*input, **) self.new(input, **) end |
.append(row) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/samovar/command.rb', line 48 def self.append(row) if method_defined?(row.key, false) warning "Method for key #{row.key} is already defined!", caller # raise ArgumentError, "Method for key #{row.key} is already defined!" end attr_accessor(row.key) if row.respond_to?(:key) self.table << row end |
.call(input = ARGV) ⇒ Object
19 20 21 22 23 |
# File 'lib/samovar/command.rb', line 19 def self.call(input = ARGV) if command = self.parse(input) command.call end end |
.command_line(name) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/samovar/command.rb', line 93 def self.command_line(name) if table = self.table.merged "#{name} #{table.merged.usage}" else name end end |
.many(*arguments, **options) ⇒ Object
71 72 73 |
# File 'lib/samovar/command.rb', line 71 def self.many(*arguments, **) append Many.new(*arguments, **) end |
.nested(*arguments, **options) ⇒ Object
63 64 65 |
# File 'lib/samovar/command.rb', line 63 def self.nested(*arguments, **) append Nested.new(*arguments, **) end |
.one(*arguments, **options) ⇒ Object
67 68 69 |
# File 'lib/samovar/command.rb', line 67 def self.one(*arguments, **) append One.new(*arguments, **) end |
.options(*arguments, **options, &block) ⇒ Object
59 60 61 |
# File 'lib/samovar/command.rb', line 59 def self.(*arguments, **, &block) append Options.parse(*arguments, **, &block) end |
.parse(input) ⇒ Object
The top level entry point for parsing ARGV.
26 27 28 29 30 31 32 33 34 |
# File 'lib/samovar/command.rb', line 26 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
75 76 77 |
# File 'lib/samovar/command.rb', line 75 def self.split(*arguments, **) append Split.new(*arguments, **) end |
.table ⇒ Object
44 45 46 |
# File 'lib/samovar/command.rb', line 44 def self.table @table ||= Table.nested(self) end |
.usage(rows, name) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/samovar/command.rb', line 79 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
122 123 124 |
# File 'lib/samovar/command.rb', line 122 def [](*input) self.dup.tap{|command| command.parse(input)} end |
#parse(input) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/samovar/command.rb', line 126 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: self.output, formatter: Output::UsageFormatter, &block) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/samovar/command.rb', line 136 def print_usage(output: self.output, formatter: Output::UsageFormatter, &block) rows = Output::Rows.new self.class.usage(rows, @name) formatter.print(rows, output, &block) end |
#to_s ⇒ Object
115 116 117 |
# File 'lib/samovar/command.rb', line 115 def to_s self.class.name end |