Skip to main content

languagetool_rust/cli/
mod.rs

1//! Command line tools.
2//!
3//! This module is specifically designed to be used by LTRS's binary target.
4//! It contains all the content needed to create LTRS's command line interface.
5
6pub mod check;
7#[cfg(feature = "cli-complete")]
8mod completions;
9#[cfg(feature = "docker")]
10mod docker;
11mod languages;
12mod ping;
13mod words;
14
15use std::io;
16
17use clap::{CommandFactory, Parser, Subcommand};
18use enum_dispatch::enum_dispatch;
19use is_terminal::IsTerminal;
20#[cfg(feature = "annotate")]
21use termcolor::{ColorChoice, StandardStream};
22
23#[cfg(feature = "docker")]
24pub use docker::Docker;
25
26use crate::{
27    api::server::{ServerCli, ServerClient},
28    error::Result,
29};
30
31/// Main command line structure. Contains every subcommand.
32#[derive(Parser, Debug)]
33#[command(
34    author,
35    version,
36    about = "LanguageTool API bindings in Rust.",
37    propagate_version(true),
38    subcommand_required(true),
39    verbatim_doc_comment
40)]
41pub struct Cli {
42    /// Specify WHEN to colorize output.
43    #[arg(short, long, value_name = "WHEN", default_value = "auto", default_missing_value = "always", num_args(0..=1), require_equals(true))]
44    pub color: clap::ColorChoice,
45    /// [`ServerCli`] arguments.
46    #[command(flatten, next_help_heading = "Server options")]
47    pub server_cli: ServerCli,
48    /// Subcommand.
49    #[command(subcommand)]
50    #[allow(missing_docs)]
51    pub command: Command,
52    #[command(flatten)]
53    #[allow(missing_docs)]
54    pub verbose: clap_verbosity_flag::Verbosity<clap_verbosity_flag::WarnLevel>,
55}
56
57/// All possible subcommands.
58#[derive(Subcommand, Debug)]
59#[enum_dispatch]
60#[allow(missing_docs)]
61pub enum Command {
62    /// Check text using LanguageTool server.
63    Check(check::Command),
64    /// Commands to easily run a LanguageTool server with Docker.
65    #[cfg(feature = "docker")]
66    Docker(docker::Command),
67    /// Return list of supported languages.
68    #[clap(visible_alias = "lang")]
69    Languages(languages::Command),
70    /// Ping the LanguageTool server and return time elapsed in ms if success.
71    Ping(ping::Command),
72    /// Retrieve some user's words list, or add / delete word from it.
73    Words(words::Command),
74    /// Generate tab-completion scripts for supported shells
75    #[cfg(feature = "cli-complete")]
76    Completions(completions::Command),
77}
78
79/// Provides a common interface for executing the subcommands.
80#[enum_dispatch(Command)]
81trait ExecuteSubcommand {
82    /// Executes the subcommand.
83    async fn execute(self, stdout: StandardStream, server_client: ServerClient) -> Result<()>;
84}
85
86impl Cli {
87    /// Return a standard output stream that optionally supports color.
88    #[must_use]
89    fn stdout(&self) -> StandardStream {
90        let mut choice: ColorChoice = match self.color {
91            clap::ColorChoice::Auto => ColorChoice::Auto,
92            clap::ColorChoice::Always => ColorChoice::Always,
93            clap::ColorChoice::Never => ColorChoice::Never,
94        };
95
96        if choice == ColorChoice::Auto && !io::stdout().is_terminal() {
97            choice = ColorChoice::Never;
98        }
99
100        StandardStream::stdout(choice)
101    }
102
103    /// Execute command, possibly returning an error.
104    pub async fn execute(self) -> Result<()> {
105        let stdout = self.stdout();
106        let server_client: ServerClient = self.server_cli.into();
107
108        self.command.execute(stdout, server_client).await
109    }
110}
111
112/// Build a command from the top-level command line structure.
113#[must_use]
114pub fn build_cli() -> clap::Command {
115    Cli::command()
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121    #[test]
122    fn test_cli() {
123        Cli::command().debug_assert();
124    }
125
126    #[test]
127    fn test_cli_from_build_cli() {
128        build_cli().debug_assert();
129    }
130}