teamy-figue 2.0.2

Type-safe CLI arguments, config files, and environment variables powered by Facet reflection
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Tests for subcommand validation error messages
//!
//! These tests cover the scenarios where subcommands have missing required fields,
//! unexpected arguments, or other validation issues that should produce helpful
//! error messages.

use crate::assert_diag_snapshot;
use facet::Facet;
use figue as args;

// ============================================================================
// Test Case 1: Missing required positional argument in subcommand
// ============================================================================

#[test]
fn test_subcommand_missing_required_positional() {
    /// Main CLI with subcommands
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,

        #[facet(flatten)]
        builtins: args::FigueBuiltins,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        /// Initialize a new project
        Init(InitArgs),
        /// Build the project
        Build(BuildArgs),
    }

    #[derive(Facet, Debug)]
    struct InitArgs {
        /// Project name (creates directory with this name)
        #[facet(args::positional)]
        name: String,

        /// Template to use (skips interactive selection)
        #[facet(args::named, args::short = 't', default)]
        template: Option<String>,
    }

    #[derive(Facet, Debug)]
    struct BuildArgs {
        /// Build in release mode
        #[facet(args::named, args::short = 'r', default)]
        release: bool,
    }

    // User types: "ddc init" (without the required positional "name")
    // Should give a helpful error like "Missing required argument: <name>"
    // NOT "Error: missing field `name` in type `InitArgs`"
    let err = figue::from_slice::<Cli>(&["init"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 2: Subcommand with one positional that gets cancelled/interrupted
// ============================================================================

#[test]
fn test_subcommand_with_partial_input() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,

        #[facet(flatten)]
        builtins: args::FigueBuiltins,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        Init(InitArgs),
    }

    #[derive(Facet, Debug)]
    struct InitArgs {
        /// Project name
        #[facet(args::positional)]
        name: String,

        /// Project description
        #[facet(args::positional)]
        description: String,
    }

    // User types: "ddc init myproject" (missing second positional)
    // Should clearly indicate which argument is missing
    let err = figue::from_slice::<Cli>(&["init", "myproject"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 3: Subcommand with unexpected extra positional argument
// ============================================================================

#[test]
fn test_subcommand_unexpected_extra_positional() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,

        #[facet(flatten)]
        builtins: args::FigueBuiltins,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        Init(InitArgs),
    }

    #[derive(Facet, Debug)]
    struct InitArgs {
        /// Project name
        #[facet(args::positional)]
        name: String,
    }

    // User types: "ddc init myproject extra-arg"
    // Should indicate unexpected argument and show what was already parsed
    let err = figue::from_slice::<Cli>(&["init", "myproject", "extra-arg"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 4: Multiple missing positional arguments
// ============================================================================

#[test]
fn test_subcommand_multiple_missing_positionals() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        Create(CreateArgs),
    }

    #[derive(Facet, Debug)]
    struct CreateArgs {
        /// Resource type (e.g., user, project)
        #[facet(args::positional)]
        resource_type: String,

        /// Resource name
        #[facet(args::positional)]
        resource_name: String,

        /// Resource location
        #[facet(args::positional)]
        location: String,
    }

    // User types just the subcommand with no arguments
    let err = figue::from_slice::<Cli>(&["create"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 5: Missing required named argument in subcommand
// ============================================================================

#[test]
fn test_subcommand_missing_required_named() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        Connect(ConnectArgs),
    }

    #[derive(Facet, Debug)]
    struct ConnectArgs {
        /// Server URL (required)
        #[facet(args::named)]
        url: String,

        /// Optional timeout
        #[facet(args::named, default)]
        timeout: Option<u32>,
    }

    // User types: "cli connect" without --url
    let err = figue::from_slice::<Cli>(&["connect"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 6: Wrong subcommand name with suggestion
// ============================================================================

#[test]
fn test_unknown_subcommand_with_suggestion() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        /// Initialize a new project
        Init(InitArgs),
        /// Build the project
        Build(BuildArgs),
    }

    #[derive(Facet, Debug)]
    struct InitArgs {
        #[facet(args::positional)]
        name: String,
    }

    #[derive(Facet, Debug)]
    struct BuildArgs {
        #[facet(args::named, default)]
        release: bool,
    }

    // User types: "cli int" (typo for "init")
    let err = figue::from_slice::<Cli>(&["int"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 7: Nested subcommands with missing argument
// ============================================================================

#[test]
fn test_nested_subcommand_missing_required() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        /// Repository operations
        Repo(RepoArgs),
    }

    #[derive(Facet, Debug)]
    struct RepoArgs {
        #[facet(args::subcommand)]
        action: RepoAction,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum RepoAction {
        /// Clone a repository
        Clone(CloneArgs),
        /// Push changes
        Push,
    }

    #[derive(Facet, Debug)]
    struct CloneArgs {
        /// Repository URL
        #[facet(args::positional)]
        url: String,
    }

    // User types: "cli repo clone" without URL
    let err = figue::from_slice::<Cli>(&["repo", "clone"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 8: Subcommand with mixed missing arguments
// ============================================================================

#[test]
fn test_subcommand_mixed_missing_arguments() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        Deploy(DeployArgs),
    }

    #[derive(Facet, Debug)]
    struct DeployArgs {
        /// Environment to deploy to
        #[facet(args::positional)]
        environment: String,

        /// Deployment region (required)
        #[facet(args::named)]
        region: String,

        /// Optional version tag
        #[facet(args::named, default)]
        version: Option<String>,
    }

    // User types: "cli deploy production" (has positional but missing --region)
    let err = figue::from_slice::<Cli>(&["deploy", "production"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 9: Empty command line (no subcommand provided)
// ============================================================================

#[test]
fn test_no_subcommand_provided() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,

        #[facet(flatten)]
        builtins: args::FigueBuiltins,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        Init(InitArgs),
        Build(BuildArgs),
    }

    #[derive(Facet, Debug)]
    struct InitArgs {
        #[facet(args::positional)]
        name: String,
    }

    #[derive(Facet, Debug)]
    struct BuildArgs {
        #[facet(args::named, default)]
        release: bool,
    }

    // User types just "cli" with no subcommand
    let err = figue::from_slice::<Cli>(&[]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 10: Subcommand with value that looks like flag
// ============================================================================

#[test]
fn test_subcommand_positional_looks_like_flag() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        Create(CreateArgs),
    }

    #[derive(Facet, Debug)]
    struct CreateArgs {
        /// Resource name (can start with -)
        #[facet(args::positional)]
        name: String,

        /// Optional description
        #[facet(args::named, default)]
        description: Option<String>,
    }

    // User types: "cli create --help" but --help is unknown in this context
    // (FigueBuiltins not included, so --help is not a valid flag)
    let err = figue::from_slice::<Cli>(&["create", "--help"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 11: Nested subcommand not provided (lists available subcommands)
// ============================================================================

#[test]
fn test_nested_subcommand_not_provided() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        /// Query operations
        Query(QueryArgs),
        /// Serve the dashboard
        Serve,
    }

    #[derive(Facet, Debug)]
    struct QueryArgs {
        #[facet(args::subcommand)]
        action: QueryAction,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum QueryAction {
        /// Coverage overview
        Status,
        /// List rules without implementation references
        Uncovered,
        /// List rules without verification references
        Untested,
        /// Show unmapped code units
        Unmapped,
        /// Show details about a specific rule
        Rule(RuleArgs),
        /// Display current configuration
        Config,
        /// Validate the spec and implementation
        Validate,
    }

    #[derive(Facet, Debug)]
    struct RuleArgs {
        /// Rule ID to look up
        #[facet(args::positional)]
        rule_id: String,
    }

    // User types: "cli query" without specifying which query subcommand
    // Should list the available subcommands for the query level
    let err = figue::from_slice::<Cli>(&["query"]).unwrap_err();
    assert_diag_snapshot!(err);
}

// ============================================================================
// Test Case 12: Nested subcommand not provided (with FigueBuiltins)
// ============================================================================

#[test]
fn test_nested_subcommand_not_provided_with_builtins() {
    #[derive(Facet, Debug)]
    struct Cli {
        #[facet(args::subcommand)]
        command: Command,

        #[facet(flatten)]
        builtins: args::FigueBuiltins,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        /// Repository operations
        Repo(RepoArgs),
        /// Build the project
        Build,
    }

    #[derive(Facet, Debug)]
    struct RepoArgs {
        #[facet(args::subcommand)]
        action: RepoAction,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum RepoAction {
        /// Clone a repository
        Clone(CloneArgs),
        /// Push changes
        Push,
        /// Pull changes
        Pull,
    }

    #[derive(Facet, Debug)]
    struct CloneArgs {
        /// Repository URL
        #[facet(args::positional)]
        url: String,
    }

    // User types: "cli repo" without specifying which repo action
    // Should list clone, push, pull as available subcommands
    let err = figue::from_slice::<Cli>(&["repo"]).unwrap_err();
    assert_diag_snapshot!(err);
}