class Samovar::Command
def self.append(row)
def self.append(row) attr_accessor(row.key) if row.respond_to?(:key) self.table << row end
def self.command_line(name)
def self.command_line(name) if @table "#{name} #{@table.usage}" else name end end
def self.many(*args, **options)
def self.many(*args, **options) append Many.new(*args, **options) end
def self.nested(*args, **options)
def self.nested(*args, **options) append Nested.new(*args, **options) end
def self.one(*args, **options)
def self.one(*args, **options) append One.new(*args, **options) end
def self.options(*args, **options, &block)
def self.options(*args, **options, &block) append Options.parse(*args, **options, &block) end
def self.parse(input)
def self.parse(input) command = self.new(input) raise IncompleteParse.new("Could not parse #{input}") unless input.empty? return command end
def self.split(*args, **options)
def self.split(*args, **options) append Split.new(*args, **options) end
def self.table
def self.table @table ||= Table.new end
def self.usage(rows, name)
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
def [] key
def [] key @attributes[key] end
def initialize(input = nil)
def initialize(input = nil) self.class.table.parse(input) do |key, value| self.send("#{key}=", value) end if input end
def print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter)
def print_usage(*args, output: $stderr, formatter: Output::DetailedFormatter) rows = Output::Rows.new self.class.usage(rows, *args) formatter.print(rows, output) end
def system(*args, **options)
def system(*args, **options) command_line = args.join(' ') pid = Process.spawn(*args, **options) puts Rainbow(command_line).color(:blue) status = Process.waitpid2(pid).last return status.success? rescue Errno::ENOENT return false end
def system!(*args, **options)
def system!(*args, **options) command_line = args.join(' ') pid = Process.spawn(*args, **options) puts Rainbow(command_line).color(:blue) status = Process.waitpid2(pid).last if status.success? return true else raise SystemError.new("Command #{command_line.dump} failed: #{status.to_s}") end rescue Errno::ENOENT raise SystemError.new("Command #{command_line.dump} failed: #{$!}") end
def track_time
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