Class: Samovar::Command
- Inherits:
-
Object
- Object
- Samovar::Command
- Defined in:
- lib/samovar/command.rb,
lib/samovar/command/system.rb,
lib/samovar/command/track_time.rb
Class Attribute Summary collapse
-
.description ⇒ Object
Returns the value of attribute description.
Class Method Summary collapse
- .[](*input) ⇒ Object
- .append(row) ⇒ Object
- .command_line(name) ⇒ Object
- .many(*args, **options) ⇒ Object
- .nested(*args, **options) ⇒ Object
- .one(*args, **options) ⇒ Object
- .options(*args, **options, &block) ⇒ Object
- .parse(input) ⇒ Object
- .split(*args, **options) ⇒ Object
- .table ⇒ Object
- .usage(rows, name) ⇒ Object
Instance Method Summary collapse
- #[](*input) ⇒ Object
-
#initialize(input = nil) ⇒ Command
constructor
A new instance of Command.
- #parse(input) ⇒ Object
- #print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter) ⇒ Object
- #system(*args, **options) ⇒ Object
- #system!(*args, **options) ⇒ Object
- #track_time ⇒ Object
Constructor Details
#initialize(input = nil) ⇒ Command
Returns a new instance of Command.
58 59 60 |
# File 'lib/samovar/command.rb', line 58 def initialize(input = nil) parse(input) if input end |
Class Attribute Details
.description ⇒ Object
Returns the value of attribute description.
63 64 65 |
# File 'lib/samovar/command.rb', line 63 def description @description end |
Class Method Details
.[](*input) ⇒ Object
46 47 48 |
# File 'lib/samovar/command.rb', line 46 def self.[](*input) self.parse(input) end |
.append(row) ⇒ Object
70 71 72 73 74 |
# File 'lib/samovar/command.rb', line 70 def self.append(row) attr_accessor(row.key) if row.respond_to?(:key) self.table << row end |
.command_line(name) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/samovar/command.rb', line 110 def self.command_line(name) if @table "#{name} #{@table.usage}" else name end end |
.many(*args, **options) ⇒ Object
88 89 90 |
# File 'lib/samovar/command.rb', line 88 def self.many(*args, **) append Many.new(*args, **) end |
.nested(*args, **options) ⇒ Object
80 81 82 |
# File 'lib/samovar/command.rb', line 80 def self.nested(*args, **) append Nested.new(*args, **) end |
.one(*args, **options) ⇒ Object
84 85 86 |
# File 'lib/samovar/command.rb', line 84 def self.one(*args, **) append One.new(*args, **) end |
.options(*args, **options, &block) ⇒ Object
76 77 78 |
# File 'lib/samovar/command.rb', line 76 def self.(*args, **, &block) append Options.parse(*args, **, &block) end |
.parse(input) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/samovar/command.rb', line 38 def self.parse(input) command = self.new(input) raise IncompleteParse.new("Could not parse #{input}") unless input.empty? return command end |
.split(*args, **options) ⇒ Object
92 93 94 |
# File 'lib/samovar/command.rb', line 92 def self.split(*args, **) append Split.new(*args, **) end |
.table ⇒ Object
66 67 68 |
# File 'lib/samovar/command.rb', line 66 def self.table @table ||= Table.new end |
.usage(rows, name) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/samovar/command.rb', line 96 def self.usage(rows, name) rows.nested(name, self) do |rows| return unless @table @table.rows.each do |row| if row.respond_to?(:usage) row.usage(rows) else rows << row end end end end |
Instance Method Details
#[](*input) ⇒ Object
50 51 52 |
# File 'lib/samovar/command.rb', line 50 def [](*input) self.dup.tap{|command| command.parse(input)} end |
#parse(input) ⇒ Object
54 55 56 |
# File 'lib/samovar/command.rb', line 54 def parse(input) self.class.table.parse(input, self) end |
#print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/samovar/command.rb', line 118 def print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter) rows = Output::Rows.new self.class.usage(rows, *args) formatter.print(rows, output) end |
#system(*args, **options) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/samovar/command/system.rb', line 31 def system(*args, **) log_system(args, ) Kernel::system(*args, **) rescue Errno::ENOENT return false end |
#system!(*args, **options) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/samovar/command/system.rb', line 39 def system!(*args, **) if system(*args, **) return true else raise SystemError.new("Command #{args.first.inspect} failed: #{$?.to_s}") end end |
#track_time ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/samovar/command/track_time.rb', line 25 def track_time start_time = Time.now yield ensure end_time = Time.now elapsed_time = end_time - start_time $stdout.flush $stderr.puts Rainbow("Elapsed Time: %0.3fs" % elapsed_time).magenta end |