Class: Polites::Cli

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

Overview

Provides the implentation of the command-line interface, exposing functions to be called from a shell or other programs.

Examples:

Cli.new(stdin: $stdin, $stdout: stdout).call(ARGV)

Instance Method Summary collapse

Constructor Details

#initialize(stdin:, stdout:, options: {}) ⇒ Cli

Returns a new instance of Cli.

Parameters:

  • stdin (IO)

    to read input from

  • stdout (IO)

    to write output to

  • options (Hash) (defaults to: {})

    default options



17
18
19
20
21
# File 'lib/polites/cli.rb', line 17

def initialize(stdin:, stdout:, options: {})
  @stdin = stdin
  @stdout = stdout
  @options = options
end

Instance Method Details

#call(args) ⇒ nil

Invoke the program with some options.

Parameters:

  • args (Array<String>)

    arguments given to the program invocation.

Returns:

  • (nil)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/polites/cli.rb', line 27

def call(args)
  filenames = option_parser.parse(args, into: @options)
  if @options[:help]
    @stdout.puts option_parser
  elsif @options[:version]
    @stdout.puts Polites::VERSION
  elsif filenames.any?
    filenames.each do |filename|
      @stdout.puts Convert.new.call(filename)
    end
  end
  nil
end