class SublimeDSL::CLI

A Command Line Interpreter.

Exit codes:

Attributes

name[R]

Public Class Methods

cmd_class_hash() click to toggle source

Returns a Hash { command_name => command_class }.

# File lib/sublime_dsl/cli.rb, line 18
def cmd_class_hash
  @cmd_class_hash ||= load_commands
end
load_commands() click to toggle source
# File lib/sublime_dsl/cli.rb, line 22
def load_commands
  h = {}
  Dir.chdir File.dirname(__FILE__) do
    Dir["cli/*.rb"].each do |f|
      require_relative f.sub(/\.rb$/, '')
      c = File.basename(f, '.rb')
      h[c] = CLI.const_get(c.pascal_case)
    end
  end
  h
end
new(name) click to toggle source
# File lib/sublime_dsl/cli.rb, line 38
def initialize(name)
  @name = name
end

Public Instance Methods

command(name) click to toggle source
# File lib/sublime_dsl/cli.rb, line 88
def command(name)
  cmd_class = CLI.cmd_class_hash[name]
  unless cmd_class
    Console.error "invalid command #{name.inspect}\n" \
      "valid commands: #{CLI.cmd_class_hash.keys.join(', ')}"
    exit 2
  end

  cmd_class.new(self)
end
help() click to toggle source
# File lib/sublime_dsl/cli.rb, line 99
    def help
      <<-HELP.dedent
      Sublime DSL allows defining Sublime Text packages using a Ruby DSL.

        Usage:
          #{name} -h/--help
          #{name} -v/--version
          #{name} <command> [arguments...] [options...]

        Examples:
          #{name} import Ruby
          #{name} export Ruby --cleanup

        Available commands:
          import          import to DSL
          export          export from DSL
          help <command>  show help on <command>
      HELP
    end
run() click to toggle source
# File lib/sublime_dsl/cli.rb, line 42
def run

  # honor help & version
  while ARGV.first && ARGV.first.start_with?('-')
    option = ARGV.shift
    case option
    when '-h', '--help'
      puts help
      exit 1
    when '-v', '--version'
      puts version
      exit 1
    else
      Console.error "invalid option: #{option.inspect}"
      exit 2
    end
  end

  cmd_name = ARGV.shift

  # if no argument, print help & exit
  unless cmd_name
    puts help
    exit 2
  end

  if cmd_name == 'help'
    if ARGV.empty?
      puts help
    else
      arg = ARGV.shift
      if arg == 'commands'
        puts "Available commands:"
        CLI.cmd_class_hash.each do |name, klass|
          puts "  %-10s %s" % [name, klass.summary]
        end
      else
        puts command(arg).help
      end
    end
    exit 1
  end

  command(cmd_name).run
end
version() click to toggle source
# File lib/sublime_dsl/cli.rb, line 119
def version
  "SublimeDSL #{SublimeDSL::VERSION}"
end