Class: CLI::App
- Inherits:
-
Object
- Object
- CLI::App
- Defined in:
- lib/cli/app.rb
Overview
An App
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#binary ⇒ Object
Returns the value of attribute binary.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#action(name, &block) ⇒ Object
Defines an action.
-
#actions ⇒ Object
Hash of action_name => block to be executed.
-
#initialize(binary, &block) ⇒ App
constructor
A new instance of App.
-
#run(action_name) ⇒ Object
Runs an action.
-
#run!(args) ⇒ Object
Parses the arguments (with OptionParser) and runs specified or default action.
Constructor Details
#initialize(binary, &block) ⇒ App
Returns a new instance of App.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/cli/app.rb', line 9 def initialize(binary, &block) @binary = binary @name = binary @actions = {} @use_actions = false @proxy = Proxy.new(self) @args = [] @opts = OptionParser.new @options = {} proxy.instance_eval &block end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
7 8 9 |
# File 'lib/cli/app.rb', line 7 def args @args end |
#binary ⇒ Object
Returns the value of attribute binary.
6 7 8 |
# File 'lib/cli/app.rb', line 6 def binary @binary end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/cli/app.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/cli/app.rb', line 7 def @options end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
7 8 9 |
# File 'lib/cli/app.rb', line 7 def opts @opts end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
7 8 9 |
# File 'lib/cli/app.rb', line 7 def proxy @proxy end |
#version ⇒ Object
Returns the value of attribute version.
6 7 8 |
# File 'lib/cli/app.rb', line 6 def version @version end |
Instance Method Details
#action(name, &block) ⇒ Object
Defines an action
27 28 29 30 31 |
# File 'lib/cli/app.rb', line 27 def action(name, &block) @use_actions = true if name != "default" @actions[name] = block end |
#actions ⇒ Object
Hash of action_name => block to be executed
22 23 24 |
# File 'lib/cli/app.rb', line 22 def actions @actions end |
#run(action_name) ⇒ Object
Runs an action
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cli/app.rb', line 34 def run(action_name) block = @actions[action_name] unless block if action_name == "default" raise RuntimeError, "Error: you have to define 'default' action" end raise ArgumentError, "action '#{action_name}' not found" end instance_eval &block end |
#run!(args) ⇒ Object
Parses the arguments (with OptionParser) and runs specified or default action.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cli/app.rb', line 50 def run!(args) @opts.parse!(args) @args = args begin if args == [] || @use_actions == false run "default" else run args[0] end rescue => e puts e..capitalize end end |