Module: Konjac::CLI

Defined in:
lib/konjac/cli.rb

Overview

The class containing the command line interface

Defined Under Namespace

Classes: Color

Constant Summary collapse

SUB_COMMANDS =

A list of valid subcommands

["add", "export", "exportxml", "import", "importxml",
"install", "language", "list", "suggest", "translate", "update", "use"]
SC_MIN_OFFSET =

The minimum amount by which to offset subcommands in standard display. Should be equal to the length of the longest top-level flag (e.g. the version in opt :version) plus 6

13
<<-eos
#{Color.bold { Color.underscore { "KONJAC" } }}

#{Color.bold "%s"}

#{Color.bold I18n.t :usage}
       konjac %s [#{Color.underscore I18n.t :options}] %s
#{I18n.t(:where_options) % Color.underscore(I18n.t(:options))}%s
eos

Class Method Summary collapse

Class Method Details

.describe_subcommandsObject

Describe the subcommands available to the command line interface



186
187
188
189
190
191
192
193
# File 'lib/konjac/cli.rb', line 186

def describe_subcommands
  text = []
  leftcol_width = [SUB_COMMANDS.map(&:length).max, SC_MIN_OFFSET].max
  SUB_COMMANDS.each do |sc|
    text << "  %#{leftcol_width}s:   %s" % [sc, I18n.t(sc, :scope => :subcommands)]
  end
  text.join "\n"
end

.startObject

Starts the command line, parsing arguments passed via ARGV and running the respective commands



36
37
38
39
40
41
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/konjac/cli.rb', line 36

def start
  ARGV << "-h" if ARGV.empty?
  global_opts = Trollop::options do
    version "konjac #{Konjac::VERSION} (c) 2012 " + I18n.t(:me)
    banner BANNER % [
      I18n.t(:banner),
      "[#{Color.underscore I18n.t :subcommand}]",
      I18n.t(:filenames_or_word_arg),
      "\n\n" + (I18n.t(:where_subcommand) % Color.underscore(I18n.t(:subcommand))) + ("\n%s\n" %
        Konjac::CLI.describe_subcommands)
    ]
    opt :dry_run, I18n.t(:dry_run, :scope => :opts), :short => "n"
    opt :quiet, I18n.t(:quiet, :scope => :opts)
    opt :version, I18n.t(:version, :scope => :opts)
    opt :help, I18n.t(:help, :scope => :opts)
    stop_on SUB_COMMANDS
  end

  # Get subcommand
  cmd = ARGV.shift
  ARGV << "-h" if ARGV.empty? && cmd != "list"
  sc_banner = BANNER % [I18n.t(cmd, :scope => :subcommands), cmd, "%s", "\n"]
  cmd_opts = case cmd
    when "add"
      opts = Trollop::options do
        banner sc_banner % I18n.t(:filenames_arg)
        opt :original, I18n.t(:original, :scope => :opts), :type => :string
        opt :translation, I18n.t(:translation, :scope => :opts), :type => :string, :short => :r
        opt :from, I18n.t(:from, :scope => :opts), :type => :string
        opt :to, I18n.t(:to, :scope => :opts), :type => :string
        opt :using, I18n.t(:using, :scope => :opts), :type => :string,
          :default => Config.dictionary, :multi => true
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Dictionary.add_word opts
    when "export"
      opts = Trollop::options do
        banner sc_banner % I18n.t(:filenames_arg)
        opt :from, I18n.t(:from, :scope => :opts), :type => :string
        opt :to, I18n.t(:to, :scope => :opts), :type => :string
        opt :output, I18n.t(:output, :scope => :opts), :type => :string
        opt :force, I18n.t(:force, :scope => :opts), :default => false,
          :short => :y
        opt :using, I18n.t(:using, :scope => :opts), :type => :string,
          :default => Config.dictionary, :multi => true
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Office.new(ARGV[0]).export
    when "exportxml"
      opts = Trollop::options do
        banner sc_banner % I18n.t(:filenames_arg)
        opt :from, I18n.t(:from, :scope => :opts), :type => :string
        opt :to, I18n.t(:to, :scope => :opts), :type => :string
        opt :output, I18n.t(:output, :scope => :opts), :type => :string
        opt :force, I18n.t(:force, :scope => :opts), :default => false,
          :short => :y
        opt :using, I18n.t(:using, :scope => :opts), :type => :string,
          :default => Config.dictionary, :multi => true
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Office.export_xml ARGV, opts
    when "import"
      opts = Trollop::options do
        banner sc_banner % I18n.t(:filenames_arg)
        opt :output, I18n.t(:output, :scope => :opts), :type => :string
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Office.new(ARGV[0]).import
    when "importxml"
      opts = Trollop::options do
        banner sc_banner % I18n.t(:filenames_arg)
        opt :output, I18n.t(:output, :scope => :opts), :type => :string
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Office.import_xml ARGV, opts
    when "install"
      Trollop::options do
        banner sc_banner % I18n.t(:script_arg)
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Installer.run :install, ARGV[0].dup
    when "language"
      Trollop::options do
        banner sc_banner % I18n.t(:word_arg)
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Config.language = ARGV[0]
      Config.save
    when "list"
      Trollop::options do
        banner sc_banner % I18n.t(:word_arg)
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Config.list.each do |line|
        puts line
      end
    when "suggest"
      opts = Trollop::options do
        banner sc_banner % I18n.t(:word_arg)
        opt :from, I18n.t(:from, :scope => :opts), :type => :string
        opt :to, I18n.t(:to, :scope => :opts), :type => :string
        opt :using, I18n.t(:using, :scope => :opts), :type => :string,
          :default => Config.dictionary, :multi => true
        opt :use_cache, I18n.t(:use_cache, :scope => :opts),
          :default => false, :short => :c
        opt :help, I18n.t(:help, :scope => :opts)
      end
      results = suggest(ARGV[0].dup, opts)
      results.each_with_index do |result, index|
        puts "%i: %s (%i%%)" % [index + 1, result[2], result[0] * 100]
      end
    when "translate"
      opts = Trollop::options do
        banner sc_banner % I18n.t(:filenames_or_word_arg)
        opt :from, I18n.t(:from, :scope => :opts), :type => :string
        opt :to, I18n.t(:to, :scope => :opts), :type => :string
        opt :output, I18n.t(:output, :scope => :opts), :type => :string
        opt :force, I18n.t(:force, :scope => :opts), :default => false,
          :short => :y
        opt :using, I18n.t(:using, :scope => :opts), :type => :string,
          :default => Config.dictionary, :multi => true
        opt :use_cache, I18n.t(:use_cache, :scope => :opts),
          :default => false, :short => :c
        opt :word, I18n.t(:word, :scope => :opts), :default => false
        opt :help, I18n.t(:help, :scope => :opts)
      end
      result = translate(ARGV, opts)
      puts result if opts[:word]
    when "update"
      Trollop::options do
        banner sc_banner % I18n.t(:script_arg)
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Installer.run :update, ARGV[0].dup
    when "use"
      Trollop::options do
        banner sc_banner % I18n.t(:word_arg)
        opt :help, I18n.t(:help, :scope => :opts)
      end
      Config.use ARGV[0]
    else
      if global_opts[:quiet]
        raise SystemExit
      else
        Trollop::die I18n.t(:unknown_subcommand) % cmd.inspect
      end
  end
end