Skip to content

Instantly share code, notes, and snippets.

@laserlemon
Created May 23, 2014 23:21
Show Gist options
  • Save laserlemon/77827a46b3a3bcabd84c to your computer and use it in GitHub Desktop.
Save laserlemon/77827a46b3a3bcabd84c to your computer and use it in GitHub Desktop.
Thor is hard.
# my_gem/cli.rb
require "thor"
require "my_gem/cli/foo_bar"
require "my_gem/cli/foo_baz"
require "my_gem/cli/install"
module MyGem
class CLI < Thor
desc "foo:bar", "Do bar to foo"
define_method "foo:bar" do
# MyGem::CLI::FooBar is my own independently unit-test bit of code.
FooBar.run(options)
end
desc "baz", "Do baz to foo"
method_option "force",
aliases: ["-f"],
desc: "Force the issue"
define_method "foo:baz" do
# MyGem::CLI::FooBaz is my own independently unit-test bit of code.
FooBaz.run(options)
end
desc "install", "Install MyGem"
method_option "dry-run",
desc: "Fake it"
def install
# MyGem::CLI::Install is a Thor::Group including Thor::Actions.
# It's used very much like a Rails generator.
#
# The problem is that the "install" task shows up twice when
# calling the my_gem executable, once as "install" and once
# more as "my_gem:c_l_i:install".
#
# I also have to duplicate options between the method options
# here and the class options in the Install class.
#
# How can I have a command that acts as a generator without
# accidentally creating two commands?
Install.start
end
end
end
# my_gem/cli/install.rb
module MyGem
class CLI < Thor
class Install < Thor::Group
include Thor::Actions
desc "install", "Install MyGem"
class_option "dry-run",
desc: "Fake it"
source_root File.expand_path("../templates", __FILE__)
def do_first_thing
# TODO: Create some file.
end
def do_next_thing
# TODO: Update some other file.
end
end
end
end
#!/usr/bin/env ruby
# bin/my_gem
require "my_gem/cli"
MyGem::CLI.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment