File size: 1,052 Bytes
e4b9a7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | # -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Command line helpers."""
import io
import sys
from argparse import ArgumentError, ArgumentParser
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8")
# a command should start with a verb when possible
COMMANDS = sorted(["data", "soundex", "tag", "tokenize", "benchmark"])
CLI_NAME = "thainlp"
def make_usage(command: str) -> dict:
prog = f"{CLI_NAME} {command}"
return {"prog": prog, "usage": f"{prog} [options]"}
def exit_if_empty(command: str, parser: ArgumentParser) -> None:
"""Print help and exit if command is empty.
:param command: command from command line
:type command: str
:param parser: parser object of the app
:type parser: ArgumentParser
"""
if not command:
if parser:
parser.print_help()
raise ArgumentError(None, "No command provided.")
|