class WeatherSage::CLI::Commands::HelpCommand

Implementation of help command-line command.

Constants

HELP

Help for this command.

Used by the help command.

Public Instance Methods

run(args) click to toggle source

Entry point for help command-line command.

# File lib/weather-sage/cli/commands/help.rb, line 24
def run(args)
  if args.size > 0
    # show details of given commands
    show_details(args)
  else
    # print full list of commands
    list_commands
  end
end

Private Instance Methods

list_commands() click to toggle source

Print full list of commands.

# File lib/weather-sage/cli/commands/help.rb, line 39
def list_commands
  puts template(:all, {
    app: File.basename(@app),

    cmds: ::WeatherSage::CLI::Help::COMMANDS.values.map { |cmd|
      template(:cmd, cmd)
    }.join("\n"),

    envs: ::WeatherSage::CLI::Env::VARS.map { |row|
      template(:env, row)
    }.join("\n\n"),
  })
end
show_details(args) click to toggle source

Print details of given commands.

# File lib/weather-sage/cli/commands/help.rb, line 56
def show_details(args)
  # get a list of unknown commands
  unknown_cmds = args.select do |arg|
    !WeatherSage::CLI::Help::COMMANDS.key?(arg)
  end

  if unknown_cmds.size > 0
    # print list of unknown commands and exit
    puts 'Unknown commands: %s' % [unknown_cmds.join(', ')]
    exit -1
  end

  # print detailed help for each argument
  puts args.map { |arg| template(:one, commands[arg]) }
end
template(key, args = {}) click to toggle source

Expand template.

# File lib/weather-sage/cli/commands/help.rb, line 75
def template(key, args = {})
  unless WeatherSage::CLI::Help::TEMPLATES.key?(key)
    raise "unknown template: #{key}"
  end

  WeatherSage::CLI::Help::TEMPLATES[key] % args
end