Class: Wookie::CLI

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

Overview

Command line interface for this gem.

Usage

wookie [options] string
wookie [options] -

Common Options

-w or --from-wookie

Translate from wookiespeak to english.

-e or --to-wookie

Translate from english to wookiespeak.

-dDIALECT or –dialect DIALECT

Use a specific dialect (default and currently only available option: simple).

-v or --version

Prints the version and exits.

Read from STDIN

When the option - is given, any non-option argument is ignored and reads from STDIN.

Examples

$ wookie -e 'Hello World!'
acwoananoo ohoorcanwa!
$ wookie -w 'acwoananoo ohoorcanwa!' --dialect simple
hello world!
$ echo 'rchurhro ahc raohwocooscwo :-)' | wookie -w -
ruby is awesome :-)

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Initializes the command line interface with the command line parameters (or any given argument array). See above.

Parameters:

  • argv (Array)

    The command line arguments (‘ARGV` is substituted, if `nil`).



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wookie/cli.rb', line 42

def initialize(argv)
  @argv = argv || ARGV
  @options = {
    :use_stdin => false,
    :dialect => :simple
  }
  OptionParser.new do |opts|
    opts.banner = "Usage: wookie [options] string\n       wookie [options] -"
    opts.separator ""
    opts.separator "Common Options"
    opts.on('-w', '--from-wookie', 'Translate from wookiespeak to english') do
      @options[:translation] = :from_wookie
    end
    opts.on('-e', '--to-wookie', 'Translate from english to wookiespeak') do
      @options[:translation] = :to_wookie
    end
    opts.on('-d', '--dialect DIALECT', [:simple], 'Use a specific dialect (defaults to simple)', 'Currently available: simple') do |dialect|
      @options[:dialect] = dialect
    end
    opts.on('-v', '--version', 'Prints the version and exits') do
      puts "Wookie Translator v#{Wookie::VERSION}.\nThis software is public domain."
      exit 0
    end
    opts.separator ""
    opts.separator "Read from STDIN"
    opts.separator '    -                                When given, ignore any non-option arguments'
    opts.separator '                                     and read from STDIN.'
  end.parse!(@argv)
end

Instance Method Details

#runvoid

This method returns an undefined value.

Actually executes the arguments and prints a translation.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wookie/cli.rb', line 74

def run
  dialect = case @options[:dialect]
  when :simple
    @options[:dialect] = Wookie::Dialect::Simple
  else
    raise ArgumentError, "Unknown dialact."
  end
  string = if @argv.include?('-')
    ARGF.read
  else
    @argv.join(' ')
  end
  t = Wookie::Translator.new(:dialect => dialect)
  puts t.send(@options[:translation], string)
end