Class: SublimeDSL::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/sublime_dsl/cli.rb,
lib/sublime_dsl/cli/export.rb,
lib/sublime_dsl/cli/import.rb

Overview

A Command Line Interpreter.

Exit codes:

  • 0: command successful

  • 1: command did nothing

  • 2: something went wrong

Defined Under Namespace

Classes: Export, Import

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ CLI

Returns a new instance of CLI.



38
39
40
# File 'lib/sublime_dsl/cli.rb', line 38

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/sublime_dsl/cli.rb', line 36

def name
  @name
end

Class Method Details

.cmd_class_hashObject

Returns a Hash { command_name => command_class }.



18
19
20
# File 'lib/sublime_dsl/cli.rb', line 18

def cmd_class_hash
  @cmd_class_hash ||= load_commands
end

.load_commandsObject



22
23
24
25
26
27
28
29
30
31
32
# 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

Instance Method Details

#command(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# 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

#helpObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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

#runObject



42
43
44
45
46
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

#versionObject



119
120
121
# File 'lib/sublime_dsl/cli.rb', line 119

def version
  "SublimeDSL #{SublimeDSL::VERSION}"
end