class GoScript::CommandGroup
Groups a set of commands by common function.
Attributes
commands[RW]
description[R]
lineno[R]
path[R]
Public Class Methods
add_command(command, group_symbol, description, path, lineno)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/go_script/command_group.rb, line 67 def command_defined?(command) groups.values.any? { |g| g.include_command? command } end
groups()
click to toggle source
# File lib/go_script/command_group.rb, line 43 def groups @groups ||= {} end
location_path(target_path)
click to toggle source
# 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
new(description, path, lineno)
click to toggle source
# File lib/go_script/command_group.rb, line 23 def initialize(description, path, lineno) @description = description @path = path @lineno = lineno @commands = {} end
usage(exitstatus: 0)
click to toggle source
# 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
Public Instance Methods
include_command?(command_symbol)
click to toggle source
# File lib/go_script/command_group.rb, line 38 def include_command?(command_symbol) commands.keys.include? command_symbol end
to_s()
click to toggle source
# 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