Class: Cl

Inherits:
Object
  • Object
show all
Extended by:
Merge, Regex, Wrap
Defined in:
lib/cl.rb,
lib/cl/ui.rb,
lib/cl/arg.rb,
lib/cl/cmd.rb,
lib/cl/ctx.rb,
lib/cl/opt.rb,
lib/cl/args.rb,
lib/cl/cast.rb,
lib/cl/help.rb,
lib/cl/opts.rb,
lib/cl/config.rb,
lib/cl/errors.rb,
lib/cl/helper.rb,
lib/cl/parser.rb,
lib/cl/runner.rb,
lib/cl/version.rb,
lib/cl/help/cmd.rb,
lib/cl/help/cmds.rb,
lib/cl/config/env.rb,
lib/cl/help/table.rb,
lib/cl/help/usage.rb,
lib/cl/config/files.rb,
lib/cl/runner/multi.rb,
lib/cl/runner/default.rb

Defined Under Namespace

Modules: Cast, Merge, Regex, Runner, Ui, Wrap Classes: Arg, Args, Cmd, Config, Ctx, Error, Help, InvalidFormat, Opt, Opts, OutOfRange, Parser, RequiredOpts, RequiredsOpts, RequiresOpts, UnknownValues

Constant Summary collapse

ArgumentError =
Class.new(Error)
OptionError =
Class.new(Error)
VERSION =
'0.1.12'

Constants included from Merge

Merge::MERGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Merge

merge

Methods included from Regex

format_regex

Methods included from Wrap

wrap

Constructor Details

#initialize(*args) ⇒ Cl


9
10
11
12
13
14
# File 'lib/cl.rb', line 9

def initialize(*args)
  ctx   = args.shift if args.first.is_a?(Ctx)
  @opts = args.last.is_a?(Hash) ? args.pop : {}
  @name = args.shift || $0
  @ctx  = ctx || Ctx.new(name, opts)
end

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx


7
8
9
# File 'lib/cl.rb', line 7

def ctx
  @ctx
end

#nameObject (readonly)

Returns the value of attribute name


7
8
9
# File 'lib/cl.rb', line 7

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts


7
8
9
# File 'lib/cl.rb', line 7

def opts
  @opts
end

Instance Method Details

#help(*args) ⇒ Object


28
29
30
# File 'lib/cl.rb', line 28

def help(*args)
  runner(['help', *args]).cmd.help
end

#run(args) ⇒ Object


16
17
18
19
20
# File 'lib/cl.rb', line 16

def run(args)
  runner(args.map(&:dup)).run
rescue Error => e
  abort [e.message, help(args.first)].join("\n\n")
end

#runner(args) ⇒ Object


22
23
24
25
26
# File 'lib/cl.rb', line 22

def runner(args)
  runner = :default if args.first.to_s == 'help'
  runner ||= opts[:runner] || :default
  Runner[runner].new(ctx, args)
end