Module: Huebot::CLI::Helpers
- Defined in:
- lib/huebot/cli/helpers.rb
Class Method Summary collapse
-
.check!(programs, device_mapper, io, quiet: false) ⇒ Object
Prints any program errors or warnings, and returns a boolean for each.
- .get_args(min: nil, max: nil, num: nil) ⇒ Object
-
.get_cmd ⇒ Symbol
Returns the command given to huebot.
-
.get_input! ⇒ Huebot::CLI::Options, Array<Huebot::Program::Src>
Parses and returns input from the CLI.
-
.help! ⇒ Object
Print help and exit.
Class Method Details
.check!(programs, device_mapper, io, quiet: false) ⇒ Object
Prints any program errors or warnings, and returns a boolean for each.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/huebot/cli/helpers.rb', line 81 def self.check!(programs, device_mapper, io, quiet: false) if (invalid_progs = programs.select { |prog| prog.errors.any? }).any? errors = invalid_progs.reduce([]) { |acc, prog| acc + prog.errors.map { |e| " #{prog.name}: #{e}" } } io, "Errors", errors unless quiet end if (imperfect_progs = programs.select { |prog| prog.warnings.any? }).any? warnings = imperfect_progs.reduce([]) { |acc, prog| acc + prog.warnings.map { |e| " #{prog.name}: #{e}" } } io, "Warnings", warnings unless quiet end all_lights = programs.reduce([]) { |acc, p| acc + p.light_names } if (missing_lights = device_mapper.missing_lights all_lights).any? io, "Unknown lights", missing_lights end all_groups = programs.reduce([]) { |acc, p| acc + p.group_names } if (missing_groups = device_mapper.missing_groups all_groups).any? io, "Unknown groups", missing_groups end all_vars = programs.reduce([]) { |acc, p| acc + p.device_refs } if (missing_vars = device_mapper.missing_vars all_vars).any? io, "Unknown device inputs", missing_vars.map { |d| "$#{d}" } end invalid_devices = missing_lights.size + missing_groups.size + missing_vars.size return invalid_progs.any?, imperfect_progs.any?, invalid_devices > 0 end |
.get_args(min: nil, max: nil, num: nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/huebot/cli/helpers.rb', line 16 def self.get_args(min: nil, max: nil, num: nil) args = ARGV[1..] if num if num != args.size $stderr.puts "Expected #{num} args, found #{args.size}" exit 1 end elsif min and max if args.size < min or args.size > max $stderr.puts "Expected #{min}-#{max} args, found #{args.size}" end elsif min if args.size < min $stderr.puts "Expected at least #{num} args, found #{args.size}" exit 1 end elsif max if args.size > max $stderr.puts "Expected no more than #{num} args, found #{args.size}" exit 1 end end args end |
.get_cmd ⇒ Symbol
Returns the command given to huebot.
12 13 14 |
# File 'lib/huebot/cli/helpers.rb', line 12 def self.get_cmd ARGV[0].to_s.to_sym end |
.get_input! ⇒ Huebot::CLI::Options, Array<Huebot::Program::Src>
Parses and returns input from the CLI. Serious errors might result in the program exiting.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/huebot/cli/helpers.rb', line 47 def self.get_input! , parser = option_parser parser.parse! files = ARGV[1..-1] if (bad_paths = files.select { |p| !File.exist? p }).any? $stderr.puts "Cannot find #{bad_paths.join ', '}" exit 1 end sources = files.map { |path| src = YAML.load_file(path) version = (src.delete("version") || 1.0).to_f Program::Src.new(src, path, version) } if !$stdin.isatty or .read_stdin puts "Please enter your YAML Huebot program below, followed by Ctrl+d:" if .read_stdin src = YAML.load($stdin.read) puts "Executing..." if .read_stdin version = (src.delete("version") || 1.0).to_f sources << Program::Src.new(src, "STDIN", version) end return , sources end |
.help! ⇒ Object
Print help and exit
116 117 118 119 120 |
# File 'lib/huebot/cli/helpers.rb', line 116 def self.help! _, parser = option_parser puts parser.help exit 1 end |