Class: BuildTool::Commands::SubCommands

Inherits:
Base
  • Object
show all
Defined in:
lib/build-tool/commands.rb

Overview

Commands that has subcommands

Instance Attribute Summary

Attributes inherited from Base

#cmd, #options, #parent

Instance Method Summary collapse

Methods inherited from Base

#<=>, #applicable?, #cleanup_after_vcs_access, #complete_readline, #configuration, #debug, #debug2, #do_execute, #each_option, #error, #fullname, #info, #log?, #setup_command, #setup_options, #skip_command, #summarize, #teardown_command, #trace, #usage, #verbose, #warn

Methods included from HelpText

#cmdalias, #description, included, #long_description, #name

Constructor Details

#initialize(*args) ⇒ SubCommands

Returns a new instance of SubCommands.



726
727
728
729
# File 'lib/build-tool/commands.rb', line 726

def initialize( *args )
    super( *args )
    @commands = []
end

Instance Method Details

#add_command(cmd) ⇒ Object



744
745
746
747
# File 'lib/build-tool/commands.rb', line 744

def add_command( cmd )
    trace( "#{name}: Adding command #{cmd.name}" )
    @commands << cmd
end

#complete_arguments(args, string) ⇒ Object



824
825
826
827
828
829
830
831
# File 'lib/build-tool/commands.rb', line 824

def complete_arguments( args, string )
    # args[0] is supposed to contain the command name
    if cmd = get_command( args[0] )
        return cmd.complete_arguments( args, string )
    else
        return []
    end
end

#complete_command(cmdname) ⇒ Object



818
819
820
821
822
# File 'lib/build-tool/commands.rb', line 818

def complete_command( cmdname )
    cmds = @commands.collect { |com|
        com.name if com.name.start_with? cmdname
    }.compact
end

#do_complete(args) ⇒ Object



804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/build-tool/commands.rb', line 804

def do_complete( args )
    cur = args[0]
    # Check if we have a match for one command
    if subcmd = get_command( cur )
        # Check if the commands is complete. If not complete it.
        return [ subcmd.name() ] if cur != subcmd.name()
        # It is complete. forward.
        return subcmd.do_complete( args[1..-1] )
    else
        # No match. Give back possible matches.
        return complete_command( cur )
    end
end

#execute(args) ⇒ Object



761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'lib/build-tool/commands.rb', line 761

def execute( args )
    debug2( '%s: %s' % [ name, args ] )

    # Reinitialize the option parser.
    setup_options

    # Parse our options. Store the rest for later.
    begin
        args = @options.order( args )
    rescue OptionParser::ParseError => e
        error( "#{name}: #{e}" )
        return -1
    end

    setup_command

    begin

        # There has to be a subcommand
        if args.length == 0
            return default_command()
        end

        # Always accept help as a command
        return help_commands() if args[0] == 'help'

        # Now follow up with real ones.
        cmd = get_command( args[0] )
        if cmd.nil?
            if self.name
                raise UsageError, "Unknown command #{args[0]} for #{self.name}"
            else
                raise UsageError, "Unknown command #{args[0]}"
            end
        end

        cmd.execute( args[1..-1] )
    ensure

        teardown_command
    end
end

#get_command(name) ⇒ Object



749
750
751
752
753
754
755
756
757
758
759
# File 'lib/build-tool/commands.rb', line 749

def get_command( name )
    partial = Array.new
    @commands.each do |cmd|
        return cmd if cmd.name == name
        partial << cmd if cmd.name.start_with? name
    end
    if partial.length == 1
        return partial[0]
    end
    nil
end

#help_commands(args = []) ⇒ Object Also known as: default_command



881
882
883
884
885
886
# File 'lib/build-tool/commands.rb', line 881

def help_commands( args = [] )
    @commands.sort.each do |cmd|
        info( "%-20s: %s" % [ cmd.name, cmd.description ] )
    end
    return 0
end

#initialize_optionsObject



731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/build-tool/commands.rb', line 731

def initialize_options()
    options.banner = "Usage: #{name} [OPTIONS]... COMMAND [COMMAND OPTIONS]..."

    options.on( "-h", "--help", "Show this help text." ) do
        show_help
    end

    options.separator( '' )
    options.separator( "Use '#{name} help' to see a list of commands" )

    super()
end

#load_commands(path, parentmod, submod = nil) ⇒ Object



833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
# File 'lib/build-tool/commands.rb', line 833

def load_commands( path, parentmod, submod = nil )
    debug( "load_commands( #{path} #{parentmod.inspect} #{submod.inspect}" )
    # Load all available ruby files in path
    Dir[  "#{path}/*.rb" ].each do |mod|
        load "#{mod}"
    end

    if !submod.nil?
        parentmod = parentmod.const_get( submod )
    end
    # Now look at all constants for candidates
    parentmod.constants.each do |objname|
        # Get the obj
        obj = parentmod.const_get( objname )
        # :TODO: Better solution
        next if [ "Standard", "ModuleBasedCommand", "Base", "SubCommands", "Shell" ].include? objname.to_s
        next if ! obj.kind_of? Class

        tmp = obj
        while tmp.superclass
            if tmp == BuildTool::Commands::SubCommands
                cmd = obj.new( self )
                break if !cmd.applicable?
                if cmd.cmdalias
                    parent.add_command( Alias.new( cmd.cmdalias, cmd ) )
                end
                add_command( cmd )
                cmd.load_commands( "#{path}/#{cmd.name}", parentmod, cmd.name.capitalize )
                break
            elsif tmp == BuildTool::Commands::Base
                cmd = obj.new( self )
                break if !cmd.applicable?
                if cmd.cmdalias
                    parent.add_command( Alias.new( cmd.cmdalias, cmd ) )
                end
                add_command( cmd )
                break
            end
            tmp = tmp.superclass
        end
    end

end

#show_helpObject



877
878
879
# File 'lib/build-tool/commands.rb', line 877

def show_help()
    super()
end