Skip to content

Instantly share code, notes, and snippets.

@aks
Created February 23, 2018 22:03
Show Gist options
  • Save aks/b5a3ba0657fa5b8696e66a92fb1ff222 to your computer and use it in GitHub Desktop.
Save aks/b5a3ba0657fa5b8696e66a92fb1ff222 to your computer and use it in GitHub Desktop.
A template for a new ruby thor-based command-line utility
#!/usr/bin/env ruby
#
# THOR template
$PROG = File.basename($PROGRAM_NAME)
$DIR = File.dirname($PROGRAM_NAME)
# Possibly modify the LOAD_PATH
# $LOAD_PATH << File.join(__dir__, '..', 'lib/migrations')
require 'thor'
# require 'pry-byebug' # uncomment for debugging
class MyNewCLI < Thor
class_option :verbose, aliases: '-v', type: :boolean, default: false, desc: "Be verbose"
class_option :norun, aliases: '-n', type: :boolean, default: false, desc: "No run -- don't make any changes"
class_option :debug, aliases: '-d', type: :boolean, default: false, desc: "Debug mode"
desc "action1 [ARGUMENTS ...]", "Perform action1"
def action1(*arguments)
# ...
end
desc "action2", "Perform action2"
def action2(*arguments)
# ...
end
desc "action3 ARG...", "Perform action3 on ARG .."
## some options specific to this action
# option :primary_key, aliases: '-p', type: :boolean, default: false, desc: "Only show the primary keys"
# option :id_columns, aliases: '-i', type: :boolean, default: false, desc: "Only show non-PK id columns"
# option :associations, aliases: '-a', type: :boolean, default: false, desc: "Only show the association keys"
# option :foreign_keys, aliases: '-f', type: :boolean, default: false, desc: "Only show foreign_keys"
# option :indexes, aliases: '-x', type: :boolean, default: false, desc: "Only show the indexes"
def action3(*arguments)
# ...
end
no_commands do
def root_dir
@root_dir ||= get_root_config_dir
end
def lib_dir
File.join(root_dir, 'lib')
end
def get_root_config_dir
loop do
this_dir = Dir.pwd
return this_dir if File.exist?('config/environment.rb')
case this_dir
when 'config', %r{/config$}
raise "Cannot find 'environment.rb' within 'config'!"
when '/', ''
raise "Cannot find 'config/environment.rb'!"
else
begin
Dir.chdir('..')
rescue
raise "chdir '..' failed! Cannot find 'config/environment.rb'!"
end
end
end
end
def add_load_path(dir)
$LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
end
def initialize_rails
unless defined?(RAILS_ENV)
add_load_path root_dir
require 'config/environment'
end
end
def print_in_columns_sorted_vertically(array)
return if array.empty?
data = array.sort
col_width = (data.map { |el| el.to_s.size }.max || 1) + COLUMN_GUTTER_WIDTH
num_cols = [terminal_width / col_width, 1].max
num_rows = [data.size / num_cols, 1].max
0.upto(num_rows - 1) do |row_index|
str = ''
0.upto(num_cols - 1) do |col_index|
index = row_index + col_index * num_rows
break unless index < data.size
str << sprintf("%-#{col_width}s", data[index] || '')
end
puts str.rstrip
end
end
end
end
MyNewCLI.start(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment