Class: CP::Runners::CmdParse

Inherits:
Object
  • Object
show all
Defined in:
lib/cp/runners/cmd_parse.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CmdParse

Returns a new instance of CmdParse.



7
8
9
# File 'lib/cp/runners/cmd_parse.rb', line 7

def initialize( app )
  @app = app
end

Instance Method Details

#run(*args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cp/runners/cmd_parse.rb', line 11

def run( *args )
  command_name = ''

  begin
    runner.parse( args ) { |l, c| command_name << "#{c} " if l > 0 }

  rescue ::CmdParse::InvalidCommandError => e
    command_name << e.message.gsub( /^.*: /, '' )
    CP::App.instance.fatal( "'#{command_name}' is not a #{CP::App.instance.name} command" )

  rescue ::CmdParse::InvalidOptionError,
         ::OptionParser::InvalidOption => e
    switch = e.message.gsub( /^.*: /, '' )
    CP::App.instance.fatal( "'#{switch}' is not a valid option" )
  end
end

#runnerObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cp/runners/cmd_parse.rb', line 28

def runner
  runner = ::CmdParse::CommandParser.new
  runner.program_name = @app.name
  runner.program_version = @app.version ? @app.version.split( "." ) : nil

  add_options( runner, @app.options )
  add_commands( runner, @app.commands )

  unless @app.commands[:help]
    has_default_command = @app.commands.values.find { |c| c.default }
    runner.add_command( ::CmdParse::HelpCommand.new, !has_default_command )
  end

  runner
end