Namespacing thor commands in a standalone ruby executable

后端 未结 2 1462
遥遥无期
遥遥无期 2021-01-31 11:23

When calling thor commands on the command line, the methods are namespaced by their module/class structure, e.g.

class App < Thor
  desc \'hello\', \'prints h         


        
2条回答
  •  遥遥无期
    2021-01-31 11:56

    This is one way with App as the default namespace (quite hacky though):

    #!/usr/bin/env ruby
    require "rubygems"
    require "thor"
    
    class Say < Thor
      # ./app say:hello
      desc 'hello', 'prints hello'
      def hello
        puts 'hello'
      end
    end
    
    class App < Thor
      # ./app nothing
      desc 'nothing', 'does nothing'
      def nothing
        puts 'doing nothing'
      end
    end
    
    begin
      parts = ARGV[0].split(':')
      namespace = Kernel.const_get(parts[0].capitalize)
      parts.shift
      ARGV[0] = parts.join
      namespace.start
    rescue
      App.start
    end
    

    Or, also not ideal:

    define_method 'say:hello'
    

提交回复
热议问题