ping_viewer_next/cli/
manager.rs1use clap;
2use clap::Parser;
3use lazy_static::lazy_static;
4use std::sync::Arc;
5
6#[derive(Parser, Debug)]
7#[command(version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = env!("CARGO_PKG_DESCRIPTION"))]
8struct Args {
9 #[arg(long, default_value = "false")]
11 enable_auto_create: bool,
12
13 #[arg(long)]
15 reset: bool,
16
17 #[arg(long, value_name = "IP>:<PORT", default_value = "0.0.0.0:8080")]
19 rest_server: String,
20
21 #[arg(short, long)]
23 verbose: bool,
24
25 #[arg(long, default_value = "./logs")]
27 log_path: Option<String>,
28
29 #[arg(long)]
31 enable_tracing_level_log_file: bool,
32
33 #[arg(long, default_value = "false")]
35 log_include_all_dependencies: bool,
36
37 #[arg(long)]
39 enable_tracy: bool,
40}
41
42#[derive(Debug)]
43struct Manager {
44 clap_matches: Args,
45}
46
47lazy_static! {
48 static ref MANAGER: Arc<Manager> = Arc::new(Manager::new());
49}
50
51impl Manager {
52 fn new() -> Self {
53 Self {
54 clap_matches: Args::parse(),
55 }
56 }
57}
58
59pub fn init() {
61 MANAGER.as_ref();
62}
63
64pub fn is_verbose() -> bool {
66 MANAGER.clap_matches.verbose
67}
68
69pub fn is_tracing() -> bool {
70 MANAGER.clap_matches.enable_tracing_level_log_file
71}
72
73pub fn is_tracy() -> bool {
74 MANAGER.clap_matches.enable_tracy
75}
76
77pub fn is_log_all_dependencies() -> bool {
78 MANAGER.clap_matches.log_include_all_dependencies
79}
80
81pub fn is_enable_auto_create() -> bool {
82 MANAGER.clap_matches.enable_auto_create
83}
84
85pub fn log_path() -> String {
86 let log_path =
87 MANAGER.clap_matches.log_path.clone().expect(
88 "Clap arg \"log-path\" should always be \"Some(_)\" because of the default value.",
89 );
90
91 shellexpand::full(&log_path)
92 .expect("Failed to expand path")
93 .to_string()
94}
95
96pub fn server_address() -> String {
98 MANAGER.clap_matches.rest_server.clone()
99}
100
101pub fn command_line_string() -> String {
103 std::env::args().collect::<Vec<String>>().join(" ")
104}
105
106pub fn command_line() -> String {
108 format!("{:#?}", MANAGER.clap_matches)
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn default_arguments() {
117 assert!(!is_verbose());
118 }
119}