Class: Clive

Inherits:
Object
  • Object
show all
Extended by:
Type::Lookup
Defined in:
lib/clive.rb,
lib/clive/base.rb,
lib/clive/type.rb,
lib/clive/error.rb,
lib/clive/option.rb,
lib/clive/output.rb,
lib/clive/parser.rb,
lib/clive/command.rb,
lib/clive/version.rb,
lib/clive/argument.rb,
lib/clive/arguments.rb,
lib/clive/formatter.rb,
lib/clive/struct_hash.rb,
lib/clive/type/lookup.rb,
lib/clive/option/runner.rb,
lib/clive/formatter/plain.rb,
lib/clive/arguments/parser.rb,
lib/clive/formatter/colour.rb,
lib/clive/type/definitions.rb

Overview

Clive is a DSL for creating command line interfaces. Generally to use it you will inherit from it with your own class.

class CLI < Clive
  opt :working, 'Test if it is working' do
    puts "YEP!".green
  end
end

CLI.run ARGV

# app.rb --working
#=> "YEP!"

But it is possible to create a new instance of Clive instead in almost the same way.

 cli = Clive.new do
   opt :working, 'Test if it is working' do
     puts "YEP!".green
   end
 end

cli.run ARGV

For very small tasks where you just need to collect options passed and query about them you can use the Kernel#Clive method.

r = Clive(:quiet, :verbose).run(ARGV)

$log = Logger.new(STDOUT)
$log.level = Logger::FATAL if r.quiet
$log.level = Logger::DEBUG if r.verbose

# do some stuff

Defined Under Namespace

Modules: Output, StateActions Classes: Argument, Arguments, Base, Command, Error, Formatter, Option, Parser, StructHash, Type

Constant Summary collapse

VERSION =
'1.2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



63
64
65
# File 'lib/clive.rb', line 63

def instance
  @instance
end

Class Method Details

.inherited(klass) ⇒ Object

Sets up proxy methods for each relevent method in Base to an instance of Base.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/clive.rb', line 66

def inherited(klass)
  klass.instance = Base.new

  str = (Base.instance_methods(false) | Command.instance_methods(false)).map do |sym|
    <<-EOS
      def self.#{sym}(*args, &block)
        instance.send(:#{sym}, *args, &block)
      end
    EOS
  end.join("\n")
  klass.instance_eval str
end

.method_missing(sym, *args, &block) ⇒ Object



79
80
81
# File 'lib/clive.rb', line 79

def method_missing(sym, *args, &block)
  instance.send(sym, *args, &block)
end

.new(opts = {}, &block) ⇒ Object

This allows you to use Clive without defining a class, but while keeping all of the control.

There is one caveat though when using this style: types can not be referenced with just the type name. Instead the full class path/name must be given. So instead of using opt :num, as: Integer you need to use opt :num, as: Clive::Type::Integer, and similarly for all types.

Examples:


c = Clive.new { opt :v, :verbose }
r = c.run ARGV

Parameters:



109
110
111
# File 'lib/clive.rb', line 109

def self.new(opts={}, &block)
  Base.new(opts, &block)
end

.respond_to_missing?(sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/clive.rb', line 83

def respond_to_missing?(sym, include_private=false)
  instance.respond_to?(sym, include_private)
end

Instance Method Details

#respond_to?(sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/clive.rb', line 88

def respond_to?(sym, include_private=false)
  respond_to_missing?(sym, include_private)
end