class Polites::Cli

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

@example

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

Public Class Methods

new(stdin:, stdout:, options: {}) click to toggle source

@param [IO] stdin to read input from @param [IO] stdout to write output to @param [Hash] options default options

# File lib/polites/cli.rb, line 17
def initialize(stdin:, stdout:, options: {})
  @stdin = stdin
  @stdout = stdout
  @options = options
end

Public Instance Methods

call(args) click to toggle source

Invoke the program with some options.

@param [Array<String>] args arguments given to the program invocation. @return [nil]

# 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

Private Instance Methods

option_parser() click to toggle source
# File lib/polites/cli.rb, line 43
def option_parser
  @option_parser ||=
    OptionParser.new do |o|
      o.banner = 'Usage: polites [options] FILE'
      o.on_tail '-v', '--version', 'Show current version'
      o.on_tail '-h', '--help', 'Show this message'
    end
end