Class: CP::App

Inherits:
Object show all
Includes:
Has::Commands, Has::Options, Singleton
Defined in:
lib/cp/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Has::Options

#option, #options

Methods included from Has::Commands

#command, #commands

Constructor Details

#initializeApp

Returns a new instance of App.



12
13
14
15
16
17
# File 'lib/cp/app.rb', line 12

def initialize
  self.name = File.basename( $0 )
  self.runner = CP::Runners::CmdParse

  define_builtin_options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/cp/app.rb', line 10

def name
  @name
end

#runnerObject

Returns the value of attribute runner.



10
11
12
# File 'lib/cp/app.rb', line 10

def runner
  @runner
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/cp/app.rb', line 10

def version
  @version
end

Class Method Details

.exported_methodsObject



45
46
47
# File 'lib/cp/app.rb', line 45

def self.exported_methods
  [:app, :command, :option]
end

.run(*args) ⇒ Object



54
55
56
# File 'lib/cp/app.rb', line 54

def self.run( *args )
  CP::App.instance.run( *args )
end

Instance Method Details

#app(attr, value) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/cp/app.rb', line 19

def app( attr, value )
  begin
    self.send( "#{attr}=", value )
  rescue NoMethodError
    raise ArgumentError.new( "no such property: #{attr}" )
  end
end

#define_builtin_optionsObject



27
28
29
30
31
32
33
34
# File 'lib/cp/app.rb', line 27

def define_builtin_options
  option( "-f", "--format FORMAT", "Output format" ) do |o|
    o.allowed = [:json, :table, :yaml]
    o.action = lambda { |value|
      CP::HighLine.instance.format = value
    }
  end
end

#error(msg) ⇒ Object



41
42
43
# File 'lib/cp/app.rb', line 41

def error( msg )
  $stderr.puts "#{self.name}: #{msg}.  See `#{self.name} --help`."
end

#fatal(msg) ⇒ Object



36
37
38
39
# File 'lib/cp/app.rb', line 36

def fatal( msg )
  self.error( msg )
  exit 1
end

#run(*args) ⇒ Object



49
50
51
52
# File 'lib/cp/app.rb', line 49

def run( *args )
  args = ARGV if args.empty?
  CP::App.instance.runner.new( CP::App.instance ).run( *args )
end