Class: BitGirder::Core::BitGirderCliApplication

Inherits:
BitGirderClass show all
Defined in:
lib/bitgirder/core.rb

Defined Under Namespace

Classes: UnrecognizedSubcommandError

Constant Summary

Constants included from BitGirderMethods

BitGirder::Core::BitGirderMethods::PARAM_TYPE_ARG, BitGirder::Core::BitGirderMethods::PARAM_TYPE_ENVVAR, BitGirder::Core::BitGirderMethods::PARAM_TYPE_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BitGirderMethods

argv_to_argh, check_fail_prefix, class_name_to_sym, code, compares_to, console, ext_to_class_name, ext_to_sym, has_env, has_key, has_keys, nonnegative, not_nil, positive, raisef, set_from_key, set_var, split_argv, sym_to_cli_switch, sym_to_ext_id, to_bool, unpack_argv_array, unpack_argv_hash, warn

Methods included from BitGirderStructure

#==, included

Constructor Details

#initialize(opts = {}) ⇒ BitGirderCliApplication

Returns a new instance of BitGirderCliApplication.



940
941
942
943
944
945
946
# File 'lib/bitgirder/core.rb', line 940

def initialize( opts = {} )

    super( opts )
    raise "No such method: #@main_method" unless respond_to?( @main_method )

    @verbose = BitGirderLogger.is_debug_env_set?
end

Class Method Details

.get_subcommand(commands) ⇒ Object



1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'lib/bitgirder/core.rb', line 1135

def self.get_subcommand( commands )
    
    idx = ARGV.find_index { |arg| ! arg.start_with?( "-" ) }
    
    return nil unless idx

    cmd_str = ARGV.delete_at( idx )
    cmd_sym = cmd_str.gsub( "-", "_" ).to_sym
 
    unless res = commands[ cmd_sym ]
        raise UnrecognizedSubcommandError, cmd_str
    end

    res
end

.run(cls) ⇒ Object



1127
1128
1129
1130
1131
# File 'lib/bitgirder/core.rb', line 1127

def self.run( cls )

    BitGirderMethods::not_nil( cls, :cls )
    self.new( :app_class => cls ).run( ARGV )
end

.run_subcommand_app(opts) ⇒ Object



1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/bitgirder/core.rb', line 1161

def self.run_subcommand_app( opts )

    cmds = has_key( opts, :commands )
    
    cmd_cls = begin
        self.get_subcommand( cmds )
    rescue UnrecognizedSubcommandError => e
        STDERR.puts( "Unrecognized command: #{e}" )
    end

    if cmd_cls
        self.run( cmd_cls )
    else
        self.show_subcommand_help( cmds )
    end
end

.show_subcommand_help(cmds) ⇒ Object



1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/bitgirder/core.rb', line 1151

def self.show_subcommand_help( cmds )
    
    print "\ncommands:\n\n"

    cmds.keys.sort.map { |s| s.to_s.gsub( "_", "-" ) }.
         each { |s| puts "#{ " " * 4 }#{s}" }

    puts
end

Instance Method Details

#run(argv = ARGV) ⇒ Object



1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
# File 'lib/bitgirder/core.rb', line 1103

def run( argv = ARGV )
 
    p, argh = configure_parser

    begin
        p.parse!( argv = argv.dup )
        app_obj = create_app_obj( argh )
    rescue SystemExit => e
        skip_main = true
    rescue Exception => e

        puts e
        puts p.to_s

        if @verbose || BitGirderLogger.is_debug_env_set?
            puts e.backtrace.join( "\n" ) 
        end

        exit EXIT_FAILURE
    end
 
    run_main( app_obj, argv ) unless skip_main
end