Class: Clive::Base
- Includes:
- StateActions
- Defined in:
- lib/clive/base.rb
Constant Summary collapse
- DEFAULTS =
{ :name => File.basename($0), :formatter => Formatter::Colour.new, :help_command => true, :help => true }
- GLOBAL_OPTIONS =
These options should be copied into each Command that is created.
[:name, :formatter, :help]
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
Attributes inherited from Command
Attributes inherited from Option
DSL Methods collapse
-
#command(*names, description = current_desc, opts = {}, &block) ⇒ Object
Creates a new Command.
-
#config(opts = nil) ⇒ Object
Set configuration values for the base, as if you passed an options hash to #initialize.
Instance Method Summary collapse
-
#find(arg) ⇒ Object
Finds the option or command represented by
arg
, this can the name of a command or an option which should include the correct number of dashes. -
#find_command(arg) ⇒ Object
Finds the command with the name given, if the command cannot be found returns
nil
. -
#has_command?(arg) ⇒ Boolean
Attempts to find the command with the name given, returns true if the command exits.
-
#initialize(config = {}, &block) ⇒ Base
constructor
You don’t need to create an instance of this, create a class extending Clive or call Clive.new instead.
-
#run(args = ARGV, config = {}) ⇒ Object
Runs the Clive with the args passed which defaults to
ARGV
.
Methods included from StateActions
Methods inherited from Command
#action, #boolean, #desc, #description, #end_group, #find_option, #footer, #group, #has?, #has_option?, #header, #help, #name, #option, #run_block, #to_s
Methods inherited from Option
#<=>, #block?, #inspect, long, #name, short, #to_s
Constructor Details
#initialize(config = {}, &block) ⇒ Base
You don’t need to create an instance of this, create a class extending Clive or call Clive.new instead.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/clive/base.rb', line 21 def initialize(config={}, &block) super([], config, &block) @commands = [] @header = proc { "Usage: #{@config[:name]} [command] [options]" } @footer = "" @_group = nil @config = DEFAULTS.merge(get_subhash(config, DEFAULTS.keys)) # Need to keep a state before #run is called so #set works. @state = {} instance_exec &block if block end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
4 5 6 |
# File 'lib/clive/base.rb', line 4 def commands @commands end |
Instance Method Details
#command(*names, description = current_desc, opts = {}, &block) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/clive/base.rb', line 78 def command(*args, &block) ns, d, o = [], current_desc, {} args.each do |i| case i when ::Symbol then ns << i when ::String then d = i when ::Hash then o = i end end o = DEFAULTS.merge(Hash[global_opts]).merge(o) @commands << Command.new(ns, d, o.merge({:group => @_group}), &block) end |
#config(opts = nil) ⇒ Object
Set configuration values for the base, as if you passed an options hash to #initialize.
101 102 103 104 105 106 107 |
# File 'lib/clive/base.rb', line 101 def config(opts=nil) if opts @config = @config.merge(get_subhash(opts, DEFAULTS.keys)) else @config end end |
#find(arg) ⇒ Object
Finds the option or command represented by arg
, this can the name of a command or an option which should include the correct number of dashes. If the option or command cannot be found nil
is returned.
131 132 133 134 135 136 137 |
# File 'lib/clive/base.rb', line 131 def find(arg) if arg[0..0] == '-' super else find_command(arg.to_sym) end end |
#find_command(arg) ⇒ Object
Finds the command with the name given, if the command cannot be found returns nil
.
152 153 154 |
# File 'lib/clive/base.rb', line 152 def find_command(arg) @commands.find {|i| i.names.include?(arg) } end |
#has_command?(arg) ⇒ Boolean
Attempts to find the command with the name given, returns true if the command exits.
169 170 171 |
# File 'lib/clive/base.rb', line 169 def has_command?(arg) !!find_command(arg) end |
#run(args = ARGV, config = {}) ⇒ Object
Runs the Clive with the args passed which defaults to ARGV
.
51 52 53 54 55 56 57 58 |
# File 'lib/clive/base.rb', line 51 def run(args=ARGV, config={}) @config = @config.merge(get_subhash(config, DEFAULTS.keys)) add_help_option add_help_command Clive::Parser.new(self, config).parse(args, @state) end |