appcore-args 1.0.0

Dependency-free bounded argument parsing, generated help and shell completion
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// =============================================================================
//        #######
//     ###       ###     F: spec.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/08/19 12:52:57 by dnettoRaw
//    ##   ## ##   ##    U: 2026/08/19 13:34:54 by dnettoRaw
//      ###########      S: 1.0.1-rc.8
// =============================================================================

use std::fmt;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CliSpec {
    name: String,
    about: String,
    version: Option<String>,
    commands: Vec<CommandSpec>,
    options: Vec<OptionSpec>,
    arguments: Vec<ArgumentSpec>,
    command_required: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CommandSpec {
    name: String,
    aliases: Vec<String>,
    about: String,
    commands: Vec<CommandSpec>,
    options: Vec<OptionSpec>,
    arguments: Vec<ArgumentSpec>,
    command_required: bool,
    hidden: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OptionSpec {
    long: String,
    short: Option<char>,
    value: ValueMode,
    value_name: String,
    value_type: ValueType,
    possible_values: Vec<String>,
    about: String,
    required: bool,
    repeatable: bool,
    detached_optional_value: bool,
    terminal: bool,
    hidden: bool,
    conflicts_with: Vec<String>,
    requires: Vec<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArgumentSpec {
    name: String,
    about: String,
    value_type: ValueType,
    possible_values: Vec<String>,
    required: bool,
    multiple: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ValueMode {
    Forbidden,
    Required,
    Optional,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ValueType {
    String,
    Bool,
    I64,
    U64,
}

impl fmt::Display for ValueType {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::String => "text",
            Self::Bool => "true or false",
            Self::I64 => "a signed integer",
            Self::U64 => "an unsigned integer",
        })
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SpecError {
    message: String,
}

impl CliSpec {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            about: String::new(),
            version: None,
            commands: Vec::new(),
            options: Vec::new(),
            arguments: Vec::new(),
            command_required: false,
        }
    }
    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.about = about.into();
        self
    }
    pub fn version(mut self, version: impl Into<String>) -> Self {
        self.version = Some(version.into());
        self
    }
    pub fn command(mut self, command: CommandSpec) -> Self {
        self.commands.push(command);
        self
    }
    pub fn option(mut self, option: OptionSpec) -> Self {
        self.options.push(option);
        self
    }
    pub fn argument(mut self, argument: ArgumentSpec) -> Self {
        self.arguments.push(argument);
        self
    }
    pub fn command_required(mut self, required: bool) -> Self {
        self.command_required = required;
        self
    }
    pub fn validate(&self) -> Result<(), SpecError> {
        crate::spec_validation::validate_spec(self)
    }
    pub fn name(&self) -> &str {
        &self.name
    }
    pub fn about_text(&self) -> &str {
        &self.about
    }
    pub fn version_text(&self) -> Option<&str> {
        self.version.as_deref()
    }
    pub fn commands(&self) -> &[CommandSpec] {
        &self.commands
    }
    pub fn options(&self) -> &[OptionSpec] {
        &self.options
    }
    pub fn arguments(&self) -> &[ArgumentSpec] {
        &self.arguments
    }
    pub fn is_command_required(&self) -> bool {
        self.command_required
    }
}

impl CommandSpec {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            aliases: Vec::new(),
            about: String::new(),
            commands: Vec::new(),
            options: Vec::new(),
            arguments: Vec::new(),
            command_required: false,
            hidden: false,
        }
    }
    pub fn alias(mut self, alias: impl Into<String>) -> Self {
        self.aliases.push(alias.into());
        self
    }
    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.about = about.into();
        self
    }
    pub fn command(mut self, command: CommandSpec) -> Self {
        self.commands.push(command);
        self
    }
    pub fn option(mut self, option: OptionSpec) -> Self {
        self.options.push(option);
        self
    }
    pub fn argument(mut self, argument: ArgumentSpec) -> Self {
        self.arguments.push(argument);
        self
    }
    pub fn command_required(mut self, required: bool) -> Self {
        self.command_required = required;
        self
    }
    pub fn hidden(mut self, hidden: bool) -> Self {
        self.hidden = hidden;
        self
    }
    pub fn name(&self) -> &str {
        &self.name
    }
    pub fn aliases(&self) -> &[String] {
        &self.aliases
    }
    pub fn matches(&self, value: &str) -> bool {
        self.name == value || self.aliases.iter().any(|alias| alias == value)
    }
    pub fn about_text(&self) -> &str {
        &self.about
    }
    pub fn commands(&self) -> &[CommandSpec] {
        &self.commands
    }
    pub fn options(&self) -> &[OptionSpec] {
        &self.options
    }
    pub fn arguments(&self) -> &[ArgumentSpec] {
        &self.arguments
    }
    pub fn is_command_required(&self) -> bool {
        self.command_required
    }
    pub fn is_hidden(&self) -> bool {
        self.hidden
    }
}

impl OptionSpec {
    pub fn flag(long: impl Into<String>) -> Self {
        Self::new(long, ValueMode::Forbidden)
    }
    pub fn value(long: impl Into<String>) -> Self {
        Self::new(long, ValueMode::Required)
    }
    fn new(long: impl Into<String>, value: ValueMode) -> Self {
        Self {
            long: long.into(),
            short: None,
            value,
            value_name: "VALUE".into(),
            value_type: ValueType::String,
            possible_values: Vec::new(),
            about: String::new(),
            required: false,
            repeatable: false,
            detached_optional_value: false,
            terminal: false,
            hidden: false,
            conflicts_with: Vec::new(),
            requires: Vec::new(),
        }
    }
    pub fn short(mut self, short: char) -> Self {
        self.short = Some(short);
        self
    }
    pub fn optional_value(mut self) -> Self {
        self.value = ValueMode::Optional;
        self
    }
    pub fn value_name(mut self, name: impl Into<String>) -> Self {
        self.value_name = name.into();
        self
    }
    pub fn value_type(mut self, value_type: ValueType) -> Self {
        self.value_type = value_type;
        self
    }
    pub fn possible_value(mut self, value: impl Into<String>) -> Self {
        self.possible_values.push(value.into());
        self
    }
    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.about = about.into();
        self
    }
    pub fn required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }
    pub fn repeatable(mut self, repeatable: bool) -> Self {
        self.repeatable = repeatable;
        self
    }
    /// Allows an optional value to be supplied as the following argument.
    ///
    /// Optional values remain attached-only by default because a detached text
    /// value may otherwise consume a positional argument. Prefer a bounded
    /// [`ValueType`] such as [`ValueType::Bool`] when enabling this behavior.
    pub fn detached_optional_value(mut self, enabled: bool) -> Self {
        self.detached_optional_value = enabled;
        self
    }
    pub fn terminal(mut self, terminal: bool) -> Self {
        self.terminal = terminal;
        self
    }
    pub fn hidden(mut self, hidden: bool) -> Self {
        self.hidden = hidden;
        self
    }
    pub fn conflicts_with(mut self, long: impl Into<String>) -> Self {
        self.conflicts_with.push(long.into());
        self
    }
    pub fn requires(mut self, long: impl Into<String>) -> Self {
        self.requires.push(long.into());
        self
    }
    pub fn long(&self) -> &str {
        &self.long
    }
    pub fn short_name(&self) -> Option<char> {
        self.short
    }
    pub fn value_mode(&self) -> ValueMode {
        self.value
    }
    pub fn value_name_text(&self) -> &str {
        &self.value_name
    }
    pub fn value_type_kind(&self) -> ValueType {
        self.value_type
    }
    pub fn possible_values(&self) -> &[String] {
        &self.possible_values
    }
    pub fn about_text(&self) -> &str {
        &self.about
    }
    pub fn is_required(&self) -> bool {
        self.required
    }
    pub fn is_repeatable(&self) -> bool {
        self.repeatable
    }
    pub fn accepts_detached_optional_value(&self) -> bool {
        self.detached_optional_value
    }
    pub fn is_terminal(&self) -> bool {
        self.terminal
    }
    pub fn is_hidden(&self) -> bool {
        self.hidden
    }
    pub fn conflicts(&self) -> &[String] {
        &self.conflicts_with
    }
    pub fn requirements(&self) -> &[String] {
        &self.requires
    }
}

impl ArgumentSpec {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            about: String::new(),
            value_type: ValueType::String,
            possible_values: Vec::new(),
            required: false,
            multiple: false,
        }
    }
    pub fn about(mut self, about: impl Into<String>) -> Self {
        self.about = about.into();
        self
    }
    pub fn value_type(mut self, value_type: ValueType) -> Self {
        self.value_type = value_type;
        self
    }
    pub fn possible_value(mut self, value: impl Into<String>) -> Self {
        self.possible_values.push(value.into());
        self
    }
    pub fn required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }
    pub fn multiple(mut self, multiple: bool) -> Self {
        self.multiple = multiple;
        self
    }
    pub fn name(&self) -> &str {
        &self.name
    }
    pub fn about_text(&self) -> &str {
        &self.about
    }
    pub fn value_type_kind(&self) -> ValueType {
        self.value_type
    }
    pub fn possible_values(&self) -> &[String] {
        &self.possible_values
    }
    pub fn is_required(&self) -> bool {
        self.required
    }
    pub fn is_multiple(&self) -> bool {
        self.multiple
    }
}

impl SpecError {
    fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
        }
    }
    pub(crate) fn new_internal(message: impl Into<String>) -> Self {
        Self::new(message)
    }
}
impl fmt::Display for SpecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}
impl std::error::Error for SpecError {}