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/dsl.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/help/format.rb,
lib/cl/config/files.rb,
lib/cl/runner/multi.rb,
lib/cl/opts/validate.rb,
lib/cl/parser/format.rb,
lib/cl/helper/suggest.rb,
lib/cl/runner/default.rb

Defined Under Namespace

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

Constant Summary collapse

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

Constants included from Merge

Merge::MERGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Wrap

wrap

Methods included from Regex

format_regex

Methods included from Merge

merge

Constructor Details

#initialize(ctx, name, opts) ⇒ Cl

Returns a new instance of Cl.

Parameters:

  • ctx (Cl::Ctx)

    the current execution context (optional)

  • name (String)

    the program (executable) name (optional, defaults to the last segment of $0)

  • opts (Hash)

    options (optional)

Options Hash (opts):

  • :runner (Cl::Runner)

    registry key for a runner (optional, defaults to :default)

  • :ui (Cl::Ui)

    the ui for handling user interaction



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

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

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



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

def ctx
  @ctx
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#help(*args) ⇒ Object

Returns help output for the given command



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

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

#run(args) ⇒ Object

Runs the command.

Instantiates a runner with the given arguments, and runs it.

If the command fails (raises a Cl::Error) then the exception is caught, and the process aborted with the error message and help output for the given command.

Parameters:

  • args (Array<String>)

    arguments (usually ARGV)



33
34
35
36
37
38
39
# File 'lib/cl.rb', line 33

def run(args)
  runner(untaint(args)).run
rescue UnknownCmd => e
  ctx.abort e
rescue Error => e
  ctx.abort e, help(args.first)
end

#runner(args) ⇒ Object

Returns a runner instance for the given arguments.



42
43
44
45
46
# File 'lib/cl.rb', line 42

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