Skip to content

CLI Code

cli

This module defines the kotobase CLI implementation using the Click module.

db_info()

Print information about Database being used.

Source code in kotobase/src/kotobase/cli.py
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
@main.command()
def db_info():
    """
    Print information about Database being used.
    """
    try:
        info = kb.db_info()
        click.secho("--- Database Build Log ---",
                    fg="blue")
        click.secho(f"Build Date : {info['build_date']}")
        click.secho(f"Build Time : {info['build_time']} seconds")
        click.secho(f"File Size : {info['size_mb']} MB")

    except Exception as e:
        click.secho(f"Error Getting Database Info: {e}",
                    fg="red",
                    err=True
                    )
        sys.exit(1)

jlpt(word)

Show JLPT levels associated with a word / kanji string.

Source code in kotobase/src/kotobase/cli.py
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
@main.command()
@click.argument("word")
def jlpt(word: str):
    """
    Show JLPT levels associated with a word / kanji string.
    """
    try:
        vocab_level = kb.jlpt_level(word)
        kanji_levels = kb.lookup(word).jlpt_kanji_levels
        if vocab_level:
            click.echo(f"Vocabulary level: N{vocab_level}")
        else:
            click.echo("Vocabulary: (not in JLPT lists)")

        if kanji_levels:
            click.echo("Kanji levels:")
            for k, lvl in kanji_levels.items():
                click.echo(f"  {k} -> N{lvl}")
        else:
            click.echo("Kanji: (none in JLPT lists)")
    except Exception as e:
        click.secho(f"Error During Lookup: {e}",
                    fg="red",
                    err=True
                    )
        sys.exit(1)

kanji(literal)

Show KanjiDic details for a single character.

Source code in kotobase/src/kotobase/cli.py
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
@main.command()
@click.argument("literal")
def kanji(literal: str):
    """
    Show KanjiDic details for a single character.
    """
    try:
        info = kb.kanji(literal)
        if not info:
            click.echo("Kanji not found.")
            return
        handle_kanji(info)
    except Exception as e:
        click.secho(f"Error During Lookup: {e}",
                    fg="red",
                    err=True
                    )
        sys.exit(1)

lookup(word, names, wildcard, sentences, json_out)

Comprehensive dictionary lookup.

Source code in kotobase/src/kotobase/cli.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
@main.command()
@click.argument("word")
@click.option("-n",
              "--names",
              is_flag=True,
              help="Include JMnedict names"
              )
@click.option("-w",
              "--wildcard",
              is_flag=True,
              help="Treat '*'/'%' as wildcards"
              )
@click.option("-s",
              "--sentences",
              default=5,
              show_default=True,
              help="Number of example sentences to display (0 = none)"
              )
@click.option("--json-out",
              "-j",
              is_flag=True,
              help="Dump raw JSON result"
              )
def lookup(word: str,
           names: bool,
           wildcard: bool,
           sentences: int,
           json_out: bool
           ):
    """
    Comprehensive dictionary lookup.
    """
    try:
        result = kb.lookup(word,
                           wildcard=wildcard,
                           include_names=names,
                           sentence_limit=sentences)

        if json_out:
            click.echo(result.to_json())
            return

        # ---------- JMdict / JMnedict ----------
        click.secho("\n[Dictionary Entries]", fg="cyan", bold=True)
        if result.entries:
            for ent in result.entries:
                if isinstance(ent, JMDictEntryDTO):
                    handle_jmdict(ent)
                elif isinstance(ent, JMNeDictEntryDTO):
                    handle_jmnedict(ent)
        else:
            click.echo("No Entries")

        # ---------- Kanji ----------
        if result.kanji:
            click.secho("\n[Kanji Breakdown]", fg="cyan", bold=True)
            for kan in result.kanji:
                handle_kanji(kan)

        # ---------- JLPT vocab ----------
        if result.jlpt_vocab:
            click.secho("\n[Tanos JLPT Vocabulary]", fg="cyan", bold=True)
            handle_jlpt_vocab(result.jlpt_vocab)

        # ---------- JLPT grammar ----------
        if result.jlpt_grammar:
            click.secho("\n[Tanos JLPT Grammar]", fg="cyan", bold=True)
            for g in result.jlpt_grammar:
                handle_jlpt_grammar(g)
        # ---------- Sentences ----------
        if sentences:
            click.secho("\n[Example Sentences]", fg="cyan", bold=True)
            if result.examples:
                for sen in result.examples[:sentences]:
                    bullet(sen.text)
            else:
                click.echo("No Examples Found")
    except Exception as e:
        click.secho(f"Error During Lookup: {e}",
                    fg="red",
                    err=True
                    )
        sys.exit(1)