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
//! # Command Line Interface Utilities
//!
//! This module provides common utilities for command-line applications, including
//! error handling, program termination, and formatted output functions.
//!
//! ## Key Features
//!
//! - **Error Handling**: Standardized error reporting with optional usage information
//! - **JSON Output**: Formatted JSON serialization for CLI responses
//! - **Program Termination**: Clean exit functions with appropriate error codes
//!
//! ## Usage Examples
//!
//! ```rust
//! use stigmergy::cli_utils;
//!
//! // Exit with a simple error message
//! // cli_utils::exit_with_error("Configuration file not found");
//!
//! // Print formatted JSON output
//! let data = vec!["item1", "item2"];
//! cli_utils::print_json(&data).unwrap();
//! ```
use fmt;
use process;
use FromStr;
/// Output format for CLI command results.
///
/// Determines how command output should be serialized and displayed.
/// Terminates the program with an error message and exit code 1.
///
/// This function prints the error message to stderr and exits the program
/// immediately. It should be used for unrecoverable errors where the
/// program cannot continue execution.
///
/// # Arguments
/// * `message` - The error message to display
///
/// # Examples
/// ```no_run
/// use stigmergy::cli_utils::exit_with_error;
/// exit_with_error("Database connection failed");
/// ```
!
/// Terminates the program with an error message, usage information, and exit code 1.
///
/// This function is designed for command-line argument parsing errors where
/// the user needs both the error context and usage instructions to correct
/// their input.
///
/// # Arguments
/// * `message` - The error message describing what went wrong
/// * `usage` - Usage instructions or help text to guide the user
///
/// # Examples
/// ```no_run
/// use stigmergy::cli_utils::exit_with_usage_error;
/// exit_with_usage_error(
/// "Missing required argument",
/// "Usage: program <entity_id> [options]"
/// );
/// ```
!
/// Prints a success message to stdout.
///
/// Simple utility function for consistent success message formatting
/// across CLI applications.
///
/// # Arguments
/// * `message` - The success message to display
///
/// # Examples
/// ```
/// use stigmergy::cli_utils::print_success;
/// print_success("Entity created successfully");
/// ```
/// Serializes a value to the specified format and outputs to stdout.
///
/// This function provides consistent formatting across CLI tools,
/// using proper indentation for human readability.
///
/// # Arguments
/// * `value` - Any serializable value to output
/// * `format` - Output format (JSON or YAML)
///
/// # Returns
/// * `Ok(())` - Output was successfully printed
/// * `Err(String)` - Serialization failed
///
/// # Examples
/// ```
/// use stigmergy::cli_utils::{print_formatted, OutputFormat};
/// use serde_json::json;
///
/// let data = json!({"status": "success", "count": 42});
/// print_formatted(&data, OutputFormat::Json).unwrap();
/// ```
/// Serializes a value to pretty-printed JSON and outputs to stdout.
///
/// This function provides consistent JSON formatting across CLI tools,
/// using proper indentation for human readability.
///
/// # Arguments
/// * `value` - Any serializable value to output as JSON
///
/// # Returns
/// * `Ok(())` - JSON was successfully printed
/// * `Err(serde_json::Error)` - Serialization failed
///
/// # Examples
/// ```
/// use stigmergy::cli_utils::print_json;
/// use serde_json::json;
///
/// let data = json!({"status": "success", "count": 42});
/// print_json(&data).unwrap();
/// ```
/// Serializes a value to the specified format and outputs to stdout, exiting on error.
///
/// This is a convenience wrapper around `print_formatted` that handles errors
/// by terminating the program with a descriptive error message. Use this
/// when serialization failure is unrecoverable.
///
/// # Arguments
/// * `value` - Any serializable value to output
/// * `format` - Output format (JSON or YAML)
/// * `context` - Context description for error messages (e.g., "response", "entity")
///
/// # Examples
/// ```no_run
/// use stigmergy::cli_utils::{print_formatted_or_exit, OutputFormat};
/// use serde_json::json;
///
/// let entity_data = json!({"id": "entity:ABC123"});
/// print_formatted_or_exit(&entity_data, OutputFormat::Json, "entity");
/// ```
/// Serializes a value to JSON and outputs to stdout, exiting on error.
///
/// This is a convenience wrapper around `print_json` that handles errors
/// by terminating the program with a descriptive error message. Use this
/// when JSON serialization failure is unrecoverable.
///
/// # Arguments
/// * `value` - Any serializable value to output as JSON
/// * `context` - Context description for error messages (e.g., "response", "entity")
///
/// # Examples
/// ```no_run
/// use stigmergy::cli_utils::print_json_or_exit;
/// use serde_json::json;
///
/// let entity_data = json!({"id": "entity:ABC123"});
/// print_json_or_exit(&entity_data, "entity");
/// ```