2017-08-16 3 views
1

La gemme thor semble toujours ordonner les commandes définies par ordre alphabétique lors de l'impression de sa sortie d'aide. Exemple:Thor CLI: Définition d'un ordre personnalisé de commandes dans la sortie d'aide

#!/usr/bin/env ruby 

require "thor" 

class MyCLI < Thor 
    desc "z", "this should go first" 
    def z; end 

    desc "a", "this should go second" 
    def a; end 
end 

MyCLI.start(ARGV) 

Enregistrer ce script comme thor-test et appelant sans arguments donne cette sortie:

Commands: 
    thor-test a    # this should go second 
    thor-test help [COMMAND] # Describe available commands or one specific command 
    thor-test z    # this should go first 

Question: Comment puis-je dire Thor de commander les entrées différemment?

Répondre

2

Thor semble que ne propose pas une option de configuration pour cela. Je vais donc me contenter de quelques patchs pour le moment. aristotll's answer m'a indiqué au bon endroit dans source code de Thor. Mais au lieu de pirater la méthode <=>, j'ai décidé de changer l'implémentation de la méthode help. Cela me reste plus propre semble et a l'avantage que je peux encore influencer le comportement de la sortie d'aide:

#!/usr/bin/env ruby 

require "thor" 

class MyCLI < Thor 
    class << self 
    def help(shell, subcommand = false) 
     list = printable_commands(true, subcommand) 
     Thor::Util.thor_classes_in(self).each do |klass| 
     list += klass.printable_commands(false) 
     end 

     # Remove this line to disable alphabetical sorting 
     # list.sort! { |a, b| a[0] <=> b[0] } 

     # Add this line to remove the help-command itself from the output 
     list.reject! {|l| l[0].split[1] == 'help'} 

     if defined?(@package_name) && @package_name 
     shell.say "#{@package_name} commands:" 
     else 
     shell.say "Commands:" 
     end 

     shell.print_table(list, :indent => 2, :truncate => true) 
     shell.say 
     class_options_help(shell) 

     # Add this line if you want to print custom text at the end of your help output. 
     # (similar to how Rails does it) 
     shell.say 'All commands can be run with -h (or --help) for more information.' 
    end 
    end 

    desc "z", "this should go first" 
    def z; end 

    desc "a", "this should go second" 
    def a; end 
end 

MyCLI.start(ARGV) 
2

De l'helpsource code

list.sort! { |a, b| a[0] <=> b[0] } 

Il est triée par ordre alphabétique comme prévu.


Bien sûr patch singe mal est toujours autorisé, ajoutez le code suivant avant MyCLI.

SCRIPT = File.basename $PROGRAM_NAME 
class String 
    alias old_compare <=> 
    # @param [String] other_string 
    # @return [Fixnum] 
    def <=>(other_string) 
    # currently the command name is like `script_name+space+usage` 
    # a monkey patch to make z goes first 
    if other_string.start_with?(SCRIPT) 
     index = SCRIPT.size + 1 
     if other_string[index] == 'z' 
     return 1 
     elsif self[index] =='z' 
     return -1 
     end 
    end 
    old_compare other_string 
    end 
end 

La sortie:

Commands: 
    thor-test z    # this should go first 
    thor-test a    # this should go second 
    thor-test help [COMMAND] # Describe available commands or one specific command 
+1

Votre patch singe est un peu trop mal pour moi;) j'ai posté ma propre solution et accepterai que pour à présent. Mais merci de me pointer au bon endroit dans le code source. – thutt