Class: GoScript::CommandGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/go_script/command_group.rb

Overview

Groups a set of commands by common function.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, path, lineno) ⇒ CommandGroup

Returns a new instance of CommandGroup.



23
24
25
26
27
28
# File 'lib/go_script/command_group.rb', line 23

def initialize(description, path, lineno)
  @description = description
  @path = path
  @lineno = lineno
  @commands = {}
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



20
21
22
# File 'lib/go_script/command_group.rb', line 20

def commands
  @commands
end

#descriptionObject (readonly)

Returns the value of attribute description.



19
20
21
# File 'lib/go_script/command_group.rb', line 19

def description
  @description
end

#linenoObject (readonly)

Returns the value of attribute lineno.



19
20
21
# File 'lib/go_script/command_group.rb', line 19

def lineno
  @lineno
end

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/go_script/command_group.rb', line 19

def path
  @path
end

Class Method Details

.add_command(command, group_symbol, description, path, lineno) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/go_script/command_group.rb', line 71

def add_command(command, group_symbol, description, path, lineno)
  groups.values.each do |group|
    check_not_defined group.commands, 'Command', command, path, lineno
  end
  groups[group_symbol].commands[command] = Command.new(
    description, path, lineno)
end

.add_group(group_symbol, description, path, lineno) ⇒ Object



62
63
64
65
# File 'lib/go_script/command_group.rb', line 62

def add_group(group_symbol, description, path, lineno)
  check_not_defined groups, 'Command group', group_symbol, path, lineno
  groups[group_symbol] = new description, path, lineno
end

.check_not_defined(collection, label, key, path, lineno) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/go_script/command_group.rb', line 53

def check_not_defined(collection, label, key, path, lineno)
  return unless (existing = collection[key])
  previous = location_path existing.path
  current = location_path path
  prefix = previous == current ? 'line ' : previous + ':'
  abort "#{current}:#{lineno}: #{label} \"#{key}\" " \
    "already defined at #{prefix}#{existing.lineno}"
end

.command(command_sym) ⇒ Object



79
80
81
82
83
# File 'lib/go_script/command_group.rb', line 79

def command(command_sym)
  return command_sym if command_defined? command_sym
  $stderr.puts "Unknown option or command: #{command_sym}"
  usage exitstatus: 1
end

.command_defined?(command) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/go_script/command_group.rb', line 67

def command_defined?(command)
  groups.values.any? { |g| g.include_command? command }
end

.groupsObject



43
44
45
# File 'lib/go_script/command_group.rb', line 43

def groups
  @groups ||= {}
end

.location_path(target_path) ⇒ Object



47
48
49
50
51
# File 'lib/go_script/command_group.rb', line 47

def location_path(target_path)
  @base_path ||= Pathname.new(
    File.dirname(File.expand_path $PROGRAM_NAME))
  Pathname.new(File.expand_path target_path).relative_path_from @base_path
end

.usage(exitstatus: 0) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/go_script/command_group.rb', line 85

def usage(exitstatus: 0)
  output_stream = exitstatus == 0 ? $stdout : $stderr
  output_stream.puts <<END_OF_USAGE
Usage: #{$PROGRAM_NAME} [option|command] [optional command arguments...]

options:
  -h,--help     Show this help
  -v,--version  Show the version of the go_script gem
END_OF_USAGE
  (groups.values || []).each { |group| output_stream.puts group }
  exit exitstatus
end

Instance Method Details

#include_command?(command_symbol) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/go_script/command_group.rb', line 38

def include_command?(command_symbol)
  commands.keys.include? command_symbol
end

#to_sObject



30
31
32
33
34
35
36
# File 'lib/go_script/command_group.rb', line 30

def to_s
  padding = (commands.keys.max_by(&:size) || '').size + 2
  command_descriptions = commands.map do |name, command|
    format "  %-#{padding}s#{command.description}", name
  end
  ["\n#{@description}"].concat(command_descriptions).join("\n")
end