Search code examples
rubythor

Hyphenated subcommand in 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.


Solution

  • 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