module Huebot::CLI

Helpers for running huebot in cli-mode.

Constants

Options

Struct for storing cli options and program files.

@attr inputs [Array<String>]

Public Class Methods

check!(programs, io, quiet: false) click to toggle source

Prints any program errors or warnings, and returns a boolean for each.

@param programs [Array<Huebot::Program>] @param io [IO] Usually $stdout or $stderr @param quiet [Boolean] if true, don't print anything

# File lib/huebot/cli.rb, line 59
def self.check!(programs, io, quiet: false)
  if (invalid_progs = programs.select { |prog| prog.errors.any? }).any?
    print_messages! io, "Errors", invalid_progs, :errors unless quiet
  end

  if (imperfect_progs = programs.select { |prog| prog.warnings.any? }).any?
    puts "" if invalid_progs.any?
    print_messages! io, "Warnings", imperfect_progs, :warnings unless quiet
  end

  return invalid_progs.any?, imperfect_progs.any?
end
get_cmd() click to toggle source

Returns the command given to huebot.

@return [Symbol]

# File lib/huebot/cli.rb, line 22
def self.get_cmd
  ARGV[0].to_s.to_sym
end
get_input!() click to toggle source

Parses and returns input from the CLI. Serious errors might result in the program exiting.

@return [Huebot::CLI::Options] All given CLI options @return [Array<Huebot::ProgramSrc>] Array of given program sources

# File lib/huebot/cli.rb, line 32
def self.get_input!
  options, parser = option_parser
  parser.parse!

  files = ARGV[1..-1]
  if files.empty? and !options.read_stdin
    puts parser.help
    exit 1
  elsif (bad_paths = files.select { |p| !File.exists? p }).any?
    $stderr.puts "Cannot find #{bad_paths.join ', '}"
    exit 1
  else
    sources = files.map { |path|
      ProgramSrc.new(YAML.load_file(path), path)
    }
    sources << ProgramSrc.new(YAML.load($stdin.read), "STDIN") if options.read_stdin
    return options, sources
  end
end
help!() click to toggle source

Print help and exit

# File lib/huebot/cli.rb, line 73
def self.help!
  _, parser = option_parser
  puts parser.help
  exit 1
end

Private Class Methods

option_parser() click to toggle source
# File lib/huebot/cli.rb, line 97
    def self.option_parser
      options = Options.new([], false)
      parser = OptionParser.new { |opts|
        opts.banner = %(
List all lights and groups:
    huebot ls

Run program(s):
    huebot run file1.yml [file2.yml [file3.yml ...]] [options]

Validate programs and inputs:
    huebot check file1.yml [file2.yml [file3.yml ...]] [options]

Options:
        ).strip
        opts.on("-lLIGHT", "--light=LIGHT", "Light ID or name") { |l| options.inputs << LightInput.new(l) }
        opts.on("-gGROUP", "--group=GROUP", "Group ID or name") { |g| options.inputs << GroupInput.new(g) }
        opts.on("--all", "All lights and groups TODO") { $stderr.puts "Not Implemented"; exit 1 }
        opts.on("-i", "Read program from STDIN") { options.read_stdin = true }
        opts.on("-h", "--help", "Prints this help") { puts opts; exit }
      }
      return options, parser
    end
print_messages!(io, label, progs, msg_type) click to toggle source

Print each message (of the given type) for each program.

@param io [IO] Usually $stdout or $stderr @param label [String] Top-level for this group of messages @param progs [Array<Huebot::CLI::Program>] @param msg_type [Symbol] name of method that holds the messages (i.e. :errors or :warnings)