Class: Campa::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/campa/cli.rb

Overview

Implements the features available to the command ‘campa` shipped with this gem.

Instance Method Summary collapse

Constructor Details

#initialize(repl: nil, evaler: nil, context: nil, reader: Campa::Reader) ⇒ Cli

Returns a new instance of Cli.

Parameters:

  • repl (#run) (defaults to: nil)

    the repl to be run if no option is given to ‘campa`

  • evaler (#eval) (defaults to: nil)

    lisp/campa evaler to be used by the repl or file evaluation

  • context (#push, #[], #[]=) (defaults to: nil)

    the context provider for the current run. This is where the __out__ binding pointing to $stdout will be created.

  • reader (#new) (defaults to: Campa::Reader)

    lisp/campa reader for when an existent file is passed to ‘campa`.



13
14
15
16
17
18
19
# File 'lib/campa/cli.rb', line 13

def initialize(repl: nil, evaler: nil, context: nil, reader: Campa::Reader)
  @evaler = evaler || default_evaler
  @context = context || default_context
  @reader = reader

  @repl = repl || default_repl
end

Instance Method Details

#execute(argv = nil, input: $stdin, out: $stdout) ⇒ Object

Execute some campa stuff depending on the options given in the command line.

If no argument is given it will start a Repl session.

When first argument is an existent file then it will evaluated using Reader and Evaler.

If the CLI argument is anything else, this method tries to match it with OPTIONS and find the method to be executed. Current options for the CLI are:

campa test FILE1, FILE2 Uses Campa::Core::Test and Campa::Core::TestReport to evaluate the files given as options as Campa test code.



38
39
40
41
42
43
# File 'lib/campa/cli.rb', line 38

def execute(argv = nil, input: $stdin, out: $stdout)
  return repl.run(input, out) if argv.nil? || argv.empty?
  return evaluate(argv[0], input, out) if File.file?(argv[0])

  execute_option(argv[0].to_sym, argv[1..], input, out)
end