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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! # Flag-rs - A Cobra-inspired CLI Framework for Rust
//!
//! Flag-rs is a powerful command-line interface framework for Rust that brings the best features
//! of Go's Cobra library to the Rust ecosystem. The key differentiator is **dynamic runtime
//! completions** - unlike other Rust CLI frameworks, Flag-rs can generate completions based on
//! current system state, making it perfect for tools like `kubectl` that need to complete
//! resource names from a live API.
//!
//! ## Key Features
//!
//! - **Dynamic Completions**: Generate completions at runtime based on application state
//! - **Zero Dependencies**: Pure Rust implementation with no external crates
//! - **Subcommand Support**: Organize complex CLIs with nested subcommands
//! - **Flag Inheritance**: Global flags automatically available to all subcommands
//! - **Shell Completion**: Generate completion scripts for bash, zsh, and fish
//! - **Colored Output**: Beautiful help messages with automatic TTY detection
//! - **Flexible Architecture**: Use builder pattern or direct construction
//! - **Advanced Flag Types**: Choice, Range, File, Directory validation
//! - **Flag Constraints**: `RequiredIf`, `ConflictsWith`, `Requires` relationships
//! - **Completion Caching**: Cache expensive completion operations
//! - **Timeout Protection**: Prevent slow completions from hanging
//! - **Memory Optimization**: String interning and efficient data structures
//!
//! ## Quick Start
//!
//! ```rust
//! use flag_rs::{CommandBuilder, Flag, FlagType, FlagValue};
//!
//! let app = CommandBuilder::new("myapp")
//! .short("A simple CLI application")
//! .long("This is my awesome command-line application that does great things")
//! .flag(
//! Flag::new("verbose")
//! .short('v')
//! .usage("Enable verbose output")
//! .value_type(FlagType::Bool)
//! .default(FlagValue::Bool(false))
//! )
//! .subcommand(
//! CommandBuilder::new("serve")
//! .short("Start the server")
//! .flag(
//! Flag::new("port")
//! .short('p')
//! .usage("Port to listen on")
//! .value_type(FlagType::Int)
//! .default(FlagValue::Int(8080))
//! )
//! .run(|ctx| {
//! let verbose = ctx.flag("verbose")
//! .and_then(|s| s.parse::<bool>().ok())
//! .unwrap_or(false);
//!
//! let port = ctx.flag("port")
//! .and_then(|s| s.parse::<i64>().ok())
//! .unwrap_or(8080);
//!
//! if verbose {
//! println!("Starting server on port {}", port);
//! }
//! Ok(())
//! })
//! .build()
//! )
//! .build();
//!
//! // In main():
//! // let args: Vec<String> = std::env::args().skip(1).collect();
//! // if let Err(e) = app.execute(args) {
//! // eprintln!("Error: {}", e);
//! // std::process::exit(1);
//! // }
//! ```
//!
//! ## Dynamic Completions
//!
//! The killer feature that sets Flag-rs apart from other Rust CLI libraries:
//!
//! ```rust
//! use flag_rs::{CommandBuilder, CompletionResult};
//!
//! let cmd = CommandBuilder::new("kubectl")
//! .subcommand(
//! CommandBuilder::new("get")
//! .subcommand(
//! CommandBuilder::new("pods")
//! .arg_completion(|ctx, prefix| {
//! // This runs when the user presses TAB!
//! // In a real app, you'd query the Kubernetes API here
//! let namespace = ctx.flag("namespace")
//! .map(|s| s.as_str())
//! .unwrap_or("default");
//!
//! let pods = vec!["nginx-abc123", "redis-def456", "postgres-ghi789"];
//! Ok(CompletionResult::new().extend(
//! pods.into_iter()
//! .filter(|p| p.starts_with(prefix))
//! .map(String::from)
//! ))
//! })
//! .build()
//! )
//! .build()
//! )
//! .build();
//! ```
//!
//! ## Shell Completion Setup
//!
//! Add a completion command to enable shell completions:
//!
//! ```rust
//! use flag_rs::{CommandBuilder, Shell};
//!
//! fn build_completion_command() -> flag_rs::Command {
//! CommandBuilder::new("completion")
//! .short("Generate shell completion script")
//! .run(|ctx| {
//! let shell_name = ctx.args().first()
//! .ok_or(flag_rs::Error::ArgumentParsing("shell name required".to_string()))?;
//!
//! // In a real app, get the root command here
//! // let script = match shell_name.as_str() {
//! // "bash" => root_cmd.generate_completion(Shell::Bash),
//! // "zsh" => root_cmd.generate_completion(Shell::Zsh),
//! // "fish" => root_cmd.generate_completion(Shell::Fish),
//! // _ => return Err(flag_rs::Error::ArgumentParsing("unsupported shell".to_string())),
//! // };
//! // println!("{}", script);
//! Ok(())
//! })
//! .build()
//! }
//! ```
//!
//! Users can then enable completions:
//!
//! ```bash
//! # Bash
//! source <(myapp completion bash)
//!
//! # Zsh
//! source <(myapp completion zsh)
//!
//! # Fish
//! myapp completion fish | source
//! ```
//!
//! ## Advanced Flag Types
//!
//! Flag-rs now supports advanced flag types with built-in validation:
//!
//! ```rust
//! use flag_rs::{CommandBuilder, Flag, FlagType, FlagValue};
//!
//! let cmd = CommandBuilder::new("config")
//! .flag(
//! Flag::new("environment")
//! .value_type(FlagType::Choice(vec![
//! "dev".to_string(),
//! "staging".to_string(),
//! "prod".to_string()
//! ]))
//! )
//! .flag(
//! Flag::new("workers")
//! .value_type(FlagType::Range(1, 16))
//! .default(FlagValue::Int(4))
//! )
//! .flag(
//! Flag::new("config-file")
//! .value_type(FlagType::File)
//! )
//! .build();
//! ```
//!
//! ## Flag Constraints
//!
//! Define relationships between flags:
//!
//! ```rust
//! use flag_rs::{Flag, FlagType, FlagConstraint};
//!
//! let ssl_cert_flag = Flag::new("ssl-cert")
//! .value_type(FlagType::File)
//! .constraint(FlagConstraint::RequiredIf("ssl".to_string()));
//!
//! let json_flag = Flag::new("json")
//! .value_type(FlagType::Bool)
//! .constraint(FlagConstraint::ConflictsWith(vec!["xml".to_string()]));
//! ```
//!
//! ## Performance Features
//!
//! ### Completion Caching
//!
//! Cache expensive completion operations:
//!
//! ```rust,ignore
//! use flag_rs::completion_cache::CompletionCache;
//! use std::sync::Arc;
//! use std::time::Duration;
//!
//! let cache = Arc::new(CompletionCache::new(Duration::from_secs(5)));
//! ```
//!
//! ### Timeout Protection
//!
//! Prevent slow completions from hanging:
//!
//! ```rust,ignore
//! use flag_rs::completion_timeout::make_timeout_completion;
//! use std::time::Duration;
//!
//! let safe_completion = make_timeout_completion(
//! Duration::from_millis(100),
//! expensive_completion_fn
//! );
//! ```
//!
//! ## Modular Command Structure
//!
//! For larger applications, Flag-rs supports a modular architecture:
//!
//! ```rust,ignore
//! // src/commands/mod.rs
//! pub fn register_commands(root: &mut flag_rs::Command) {
//! // Register each command module
//! serve::register(root);
//! config::register(root);
//! migrate::register(root);
//! }
//!
//! // src/commands/serve.rs
//! use flag_rs::{CommandBuilder, Flag, FlagType};
//!
//! pub fn register(parent: &mut flag_rs::Command) {
//! let cmd = CommandBuilder::new("serve")
//! .short("Start the application server")
//! .flag(
//! Flag::new("port")
//! .short('p')
//! .usage("Port to bind to")
//! .value_type(FlagType::Int)
//! )
//! .run(|ctx| {
//! // Server implementation
//! Ok(())
//! })
//! .build();
//!
//! parent.add_command(cmd);
//! }
//! ```
//!
//! ## Error Handling
//!
//! Flag-rs uses idiomatic Rust error handling:
//!
//! ```rust
//! use flag_rs::{CommandBuilder, Error};
//!
//! let cmd = CommandBuilder::new("deploy")
//! .run(|ctx| {
//! let env = ctx.args().first()
//! .ok_or(Error::ArgumentParsing("environment required".to_string()))?;
//!
//! if env != "production" && env != "staging" {
//! return Err(Error::Validation(
//! format!("unknown environment: {}", env)
//! ));
//! }
//!
//! Ok(())
//! })
//! .build();
//! ```
/// Color support for terminal output
/// Core command structures and execution
/// Dynamic completion support
/// Runtime context for command execution
/// Error types and result handling
/// Flag parsing and value types
/// Shell completion script generation
/// Completion format handling
/// Completion caching for performance
/// Completion timeout handling
/// Terminal utilities for enhanced CLI output
/// Argument validation for commands
/// Command and flag suggestion support
/// ActiveHelp system for contextual hints
/// Memory-efficient completion items
/// Memory-optimized completion results
/// String interning pool for reducing memory usage
// Re-export main types for convenience
pub use ;
pub use ;
pub use CompletionCache;
pub use Context;
pub use ;
pub use ;
pub use Shell;
pub use ArgValidator;