Namespacing Thor Commands in a Standalone Ruby Executable

Namespacing thor commands in a standalone ruby executable

Another way of doing this is to use register:

class CLI < Thor
register(SubTask, 'sub', 'sub <command>', 'Description.')
end

class SubTask < Thor
desc "bar", "..."
def bar()
# ...
end
end

CLI.start

Now - assuming your executable is called foo - you can call:

$ foo sub bar

In the current thor version (0.15.0.rc2) there is a bug though, which causes the help texts to skip the namespace of sub commands:

$ foo sub
Tasks:
foo help [COMMAND] # Describe subcommands or one specific subcommand
foo bar #

You can fix that by overriding self.banner and explicitly setting the namespace.

class SubTask < Thor
namespace :sub

def bar ...

def self.banner(task, namespace = true, subcommand = false)
"#{basename} #{task.formatted_usage(self, true, subcommand)}"
end
end

The second parameter of formatted_usage is the only difference to the original implemtation of banner. You can also do this once and have other sub command thor classes inherit from SubTask. Now you get:

$ foo sub
Tasks:
foo sub help [COMMAND] # Describe subcommands or one specific subcommand
foo sub bar #

Hope that helps.

Is it possible to invoke Thor commands from my Web App?

You could try this:

MyClass.start(args, config)

args is an array of strings that represent the options you would pass on the command line, config is a hash.

MyClass.start(["-f", "blah"], type: :yo)

You should be able to access the config options within your Thor class like this:

config[:type]  # => :yo

Possible to call executable Thor-powered script without calling thor?

Make the shebang line

#!/usr/bin/env ruby

and then at the end of your script add

App.start


Related Topics



Leave a reply



Submit