cli_match_example/
cli_match_example.rs1use cli_command::{cli_args, cli_match};
7
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9 println!("🔧 CLI Match Macro Demo");
10 println!();
11
12 cli_match! {
13 "server" => {
14 println!("🚀 Starting server with cli_match! macro...");
15 start_server()
16 },
17 "client" => {
18 println!("📡 Starting client with cli_match! macro...");
19 start_client()
20 },
21 "config" => {
22 println!("⚙️ Managing configuration with cli_match! macro...");
23 manage_config()
24 },
25 "help" | "--help" | "-h" => {
26 print_help();
27 Ok(())
28 },
29 _ => {
30 eprintln!("❌ Unknown command");
31 print_help();
32 Ok(())
33 }
34 }
35}
36
37fn start_server() -> Result<(), Box<dyn std::error::Error>> {
39 let (port, host, workers, ssl, verbose) = cli_args!(
40 port: u16 = 8080,
41 host: String = "localhost".to_string(),
42 workers: usize = 4,
43 ssl: bool = false,
44 verbose: bool = false
45 );
46
47 let cmd = cli_command::parse_command_line()?;
48 let config_file = cmd.get_argument("config-file");
49
50 println!("🌐 Server Configuration:");
51 println!(" Host: {}", host);
52 println!(" Port: {}", port);
53 println!(" Workers: {}", workers);
54 println!(" SSL: {}", ssl);
55 println!(" Verbose: {}", verbose);
56
57 if let Some(config) = config_file {
58 println!(" Config file: {}", config);
59 }
60
61 println!("✅ Server started successfully on {}:{}", host, port);
62
63 Ok(())
64}
65
66fn start_client() -> Result<(), Box<dyn std::error::Error>> {
68 let (timeout, retries, server_url) = cli_args!(
69 timeout: u64 = 30,
70 retries: u32 = 3,
71 server_url: String = "http://localhost:8080".to_string()
72 );
73
74 let cmd = cli_command::parse_command_line()?;
75 let username = cmd.get_argument("username");
76 let password = cmd.get_argument("password");
77
78 println!("📡 Client Configuration:");
79 println!(" Server URL: {}", server_url);
80 println!(" Timeout: {}s", timeout);
81 println!(" Retries: {}", retries);
82
83 if let Some(user) = username {
84 println!(" Username: {}", user);
85 }
86
87 if let Some(pass) = password {
88 println!(" Password: {}", "*".repeat(pass.len()));
89 }
90
91 println!("✅ Client connected to server successfully");
92
93 Ok(())
94}
95
96fn manage_config() -> Result<(), Box<dyn std::error::Error>> {
98 let (action, config_path, backup, force) = cli_args!(
99 action: String = "show".to_string(),
100 config_path: String = "./config.toml".to_string(),
101 backup: bool = true,
102 force: bool = false
103 );
104
105 println!("⚙️ Configuration Management:");
106 println!(" Action: {}", action);
107 println!(" Config path: {}", config_path);
108 println!(" Backup: {}", backup);
109 println!(" Force: {}", force);
110
111 match action.as_str() {
112 "show" => {
113 println!("📋 Current configuration:");
114 println!(" port = 8080");
115 println!(" host = \"localhost\"");
116 println!(" workers = 4");
117 }
118 "backup" => {
119 println!("💾 Creating backup of configuration...");
120 println!("✅ Backup created successfully");
121 }
122 "restore" => {
123 println!("🔄 Restoring configuration from backup...");
124 println!("✅ Configuration restored successfully");
125 }
126 _ => {
127 println!("❌ Unknown action: {}", action);
128 println!("Available actions: show, backup, restore");
129 }
130 }
131
132 Ok(())
133}
134
135fn print_help() {
136 println!("🔧 CLI Match Macro Example");
137 println!();
138 println!("This example demonstrates the cli_match! macro for command routing");
139 println!("and the cli_args! macro for argument parsing from cli-command crate.");
140 println!();
141 println!("Usage:");
142 println!(" cli_match_example <COMMAND> [OPTIONS]");
143 println!();
144 println!("Commands:");
145 println!(" server Start a server with configurable options");
146 println!(" client Start a client with connection options");
147 println!(" config Manage configuration files");
148 println!(" help Show this help message");
149 println!();
150 println!("Server options:");
151 println!(" --port <PORT> Server port (default: 8080)");
152 println!(" --host <HOST> Server host (default: localhost)");
153 println!(" --workers <COUNT> Number of worker threads (default: 4)");
154 println!(" --ssl Enable SSL/TLS (default: false)");
155 println!(" --verbose Enable verbose logging (default: false)");
156 println!(" --config-file <FILE> Configuration file path (optional)");
157 println!();
158 println!("Client options:");
159 println!(" --server-url <URL> Server URL (default: http://localhost:8080)");
160 println!(" --timeout <SECONDS> Connection timeout (default: 30)");
161 println!(" --retries <COUNT> Number of retry attempts (default: 3)");
162 println!(" --username <USER> Username for authentication (optional)");
163 println!(" --password <PASS> Password for authentication (optional)");
164 println!();
165 println!("Config options:");
166 println!(" --action <ACTION> Action to perform: show, backup, restore (default: show)");
167 println!(" --config-path <PATH> Path to configuration file (default: ./config.toml)");
168 println!(" --backup Create backup before changes (default: true)");
169 println!(" --force Force operation without confirmation (default: false)");
170 println!();
171 println!("Examples:");
172 println!(" # Start server with custom port and SSL");
173 println!(" cargo run --example cli_match_example -- server --port 3000 --ssl --verbose");
174 println!();
175 println!(" # Start client with authentication");
176 println!(" cargo run --example cli_match_example -- client --server-url https://api.example.com --username admin --password secret");
177 println!();
178 println!(" # Show current configuration");
179 println!(" cargo run --example cli_match_example -- config --action show");
180 println!();
181 println!(" # Backup configuration");
182 println!(" cargo run --example cli_match_example -- config --action backup --config-path /etc/myapp.conf");
183 println!();
184 println!("🔍 Notice how cli_match! eliminates the need to manually parse command line");
185 println!(" and cli_args! eliminates boilerplate argument extraction code!");
186}