telepingbot/src/cli_parser.rs
Awiteb 31b810f817
All checks were successful
Write changelog / write-changelog (push) Successful in 4s
Rust CI / Rust CI (push) Successful in 2m53s
feat: Support CLI
Signed-off-by: Awiteb <a@4rs.nl>
2024-05-29 22:50:38 +03:00

108 lines
3.6 KiB
Rust

// A simple API to ping telegram bots and returns if it's online or not.
// Copyright (C) 2023-2024 Awiteb <a@4rs.nl>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/agpl-3.0>.
use std::{env, path::PathBuf};
use crate::ServerError;
pub(crate) const HELP_MESSAGE: &str = r#"Copyright (C) 2023-2024 Awiteb <a@4rs.nl>
License GNU AGPL-3.0-or-later <https://gnu.org/licenses/agpl-3.0>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Git repository: https://git.4rs.nl/awiteb/telepingbot
A simple API to ping telegram bots and returns if it's online or not
Usage: telepingbot [OPTIONS]
Options:
-c, --config <CONFIG_FILE> The config toml file
-V, --version Print version
--help Print this message
Please report bugs to <https://git.4rs.nl/awiteb/telepingbot/issues>."#;
/// The server accepted args
#[derive(Debug)]
pub(crate) struct Args {
/// The config file location, default to `config.toml`
pub config_file: PathBuf,
/// The version flag, the user want the server version
pub version: bool,
/// The help flag, the user want to show the help message
pub help: bool,
}
impl Default for Args {
fn default() -> Self {
Self {
config_file: "config.toml".into(),
version: false,
help: false,
}
}
}
impl Args {
/// Parse the cli input
pub(crate) fn parse() -> crate::ServerResult<Self> {
let mut user_args = env::args_os().skip(1);
let mut args = Self::default();
while let Some(arg) = user_args.next() {
if let Some(str_arg) = arg.to_str() {
match str_arg {
"-V" | "--version" => args.version = true,
"--help" => args.help = true,
"-c" | "--config" => {
args.config_file = user_args
.next()
.ok_or_else(|| {
ServerError::CliParse(
"The `-c`/`--config` does't have the config path".to_owned(),
)
})?
.into()
}
unknowun => {
return Err(ServerError::CliParse(format!("Unknowun arg `{unknowun}`")));
}
}
} else {
return Err(ServerError::CliParse(
"Invalid argument string provided".to_owned(),
));
}
}
Ok(args)
}
/// Returns true if the server should stop
pub(crate) fn stop(&self) -> bool {
const VERSION: &str = env!("CARGO_PKG_VERSION");
if self.help {
println!("{HELP_MESSAGE}");
} else if self.version {
println!("telepingbot v{VERSION}");
} else {
return false;
}
true
}
}