Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyphenated subcommand in Thor

Tags:

ruby

thor

I'm writing a CLI gem with Thor. Right now I have two subcommands with names that I'd like to be hyphenated. But I can't figure out how to make that work.

Here's the main class

module CLI
  class Base < Thor

    desc "api-token COMMAND", "Configure the API token"
    subcommand "api-token", ApiToken

Here's the subcommand class

module CLI
  class ApiToken < Thor
    include Shared

    namespace "api-token"

The subcommand shows up in the main help output, and if I enter

$ bundle exec bin/cli help api-token

it shows me the right output for the subcommands actions. So something is connecting at least.

But when I try to use the command, this is what I see

$ bundle exec bin/cli api-token set
> Could not find command "api-token".

The command works fine if I make it one word or use an underscore, but I really prefer a hyphen.

like image 503
Phillip Longman Avatar asked Mar 06 '26 10:03

Phillip Longman


2 Answers

There's no map required, just

class Test < Thor
  desc 'howto-dash', "dash in command name"
  def howto_dash
    puts "dashing through the snow"
  end
end

Output:

> thor list
test
----
thor test:howto-dash  # dash in command name
like image 143
Leonhardt Wille Avatar answered Mar 08 '26 00:03

Leonhardt Wille


In case anyone is wondering about this, you can use Thor.map to map a string to a method/command. Ex.

desc "Command help", "Longer command description"
def package_all
  puts "Packing..."
end
map "package-all" => "package_all"

Link to the documentation: Thor#map-class_method

like image 37
Patrick Teng Avatar answered Mar 07 '26 22:03

Patrick Teng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!