figue 4.0.3

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
use crate::assert_help_snapshot;
use facet::Facet;
use figue as args;
use figue::FigueBuiltins;

/// A sample CLI application for testing help generation.
///
/// This is a longer description that spans multiple lines
/// to test how doc comments are handled.
#[derive(Facet, Debug)]
struct SimpleArgs {
    /// Enable verbose output
    #[facet(args::named, args::short = 'v')]
    verbose: bool,

    /// Number of parallel jobs
    #[facet(args::named, args::short = 'j', args::label = "count")]
    jobs: Option<usize>,

    /// Input file to process
    #[facet(args::positional)]
    input: String,

    /// Output file (optional)
    #[facet(default, args::positional)]
    output: Option<String>,

    /// Standard CLI options
    #[facet(flatten)]
    builtins: FigueBuiltins,
}

#[test]
fn test_help_simple_struct() {
    let config = figue::HelpConfig {
        program_name: Some("myapp".to_string()),
        version: Some("1.0.0".to_string()),
        ..Default::default()
    };
    let help = figue::generate_help::<SimpleArgs>(&config);
    assert_help_snapshot!(help);
}

/// Git-like CLI with subcommands
#[derive(Facet, Debug)]
struct GitArgs {
    /// Git command to run
    #[facet(args::subcommand)]
    command: GitCommand,

    /// Standard CLI options
    #[facet(flatten)]
    builtins: FigueBuiltins,
}

/// Available git commands
#[derive(Facet, Debug)]
#[repr(u8)]
#[allow(dead_code)]
enum GitCommand {
    /// Clone a repository
    Clone {
        /// URL of the repository to clone
        #[facet(args::positional)]
        url: String,
        /// Directory to clone into
        #[facet(default, args::positional)]
        directory: Option<String>,
    },
    /// Show commit history
    Log {
        /// Number of commits to show
        #[facet(args::named, args::short = 'n')]
        count: Option<usize>,
        /// Show one line per commit
        #[facet(args::named)]
        oneline: bool,
    },
    /// Manage remotes
    Remote {
        /// Remote subcommand
        #[facet(args::subcommand)]
        action: RemoteAction,
    },
}

/// Remote management commands
#[derive(Facet, Debug)]
#[repr(u8)]
#[allow(dead_code)]
enum RemoteAction {
    /// Add a new remote
    Add {
        /// Name of the remote
        #[facet(args::positional)]
        name: String,
        /// URL of the remote
        #[facet(args::positional)]
        url: String,
    },
    /// Remove a remote
    #[facet(rename = "rm")]
    Remove {
        /// Name of the remote to remove
        #[facet(args::positional)]
        name: String,
    },
    /// List all remotes
    #[facet(rename = "ls")]
    List {
        /// Show verbose output
        #[facet(args::named, args::short = 'v')]
        verbose: bool,
    },
}

#[test]
fn test_help_with_subcommands() {
    let config = figue::HelpConfig {
        program_name: Some("git".to_string()),
        version: Some("2.40.0".to_string()),
        ..Default::default()
    };
    let help = figue::generate_help::<GitArgs>(&config);
    assert_help_snapshot!(help);
}

#[test]
fn test_help_enum_only() {
    let config = figue::HelpConfig {
        program_name: Some("git".to_string()),
        ..Default::default()
    };
    let help = figue::generate_help::<GitCommand>(&config);
    assert_help_snapshot!(help);
}

/// Test that --help and -h flags trigger help when FigueBuiltins is present
#[test]
fn test_help_flags() {
    // --help
    let result = figue::from_slice::<SimpleArgs>(&["--help"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.is_help());
    assert!(err.help_text().is_some());
    // -h
    let result = figue::from_slice::<SimpleArgs>(&["-h"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.is_help());
}

#[test]
fn test_html_help_flag_writes_html_file() {
    let result = figue::from_slice::<SimpleArgs>(&["--html-help"]).into_result();

    let Err(figue::DriverError::HtmlHelp { path }) = result else {
        panic!("expected HTML help request");
    };

    assert_eq!(
        path.file_name().and_then(|name| name.to_str()),
        Some("index.html")
    );

    let html = std::fs::read_to_string(&path).expect("HTML help file should be readable");
    assert!(html.contains("<!doctype html>"));
    assert!(html.contains("A sample CLI application for testing help generation."));
    assert!(html.contains("--html-help"));
    assert!(html.contains("&lt;INPUT&gt;"));
}

/// Test that help output for tuple variant subcommands shows flattened fields
/// instead of `--0 <STRUCTNAME>`.
#[test]
fn test_tuple_variant_subcommand_help_flattening() {
    #[derive(Facet, Debug)]
    struct BuildArgs {
        /// Build in release mode
        #[facet(args::named, args::short = 'r')]
        release: bool,

        /// Disable spawning processes
        #[facet(args::named)]
        no_spawn: bool,

        /// Disable TUI mode
        #[facet(args::named)]
        no_tui: bool,
    }

    #[derive(Facet, Debug)]
    #[repr(u8)]
    #[allow(dead_code)]
    enum Command {
        /// Build the project
        Build(BuildArgs),
        /// Run tests
        Test {
            /// Run in verbose mode
            #[facet(args::named, args::short = 'v')]
            verbose: bool,
        },
    }

    #[derive(Facet, Debug)]
    struct Args {
        #[facet(args::subcommand)]
        command: Command,

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

    // Test help for the main command
    let config = figue::HelpConfig {
        program_name: Some("myapp".to_string()),
        ..Default::default()
    };
    let help = figue::generate_help::<Args>(&config);
    assert_help_snapshot!("tuple_variant_main_help", help);
}

// ------------------------------------------------------------------------
// Subcommand-aware help generation
// ------------------------------------------------------------------------
// When a user runs `myapp subcommand --help`, the help output should be
// tailored to that specific subcommand, not the root help.

/// CLI with subcommands for testing subcommand-specific help
#[derive(Facet, Debug)]
struct PkgManager {
    /// Package manager command
    #[facet(args::subcommand)]
    command: PkgCommand,

    /// Standard CLI options
    #[facet(flatten)]
    builtins: FigueBuiltins,
}

#[derive(Facet, Debug)]
#[repr(u8)]
#[allow(dead_code)]
enum PkgCommand {
    /// Install a package
    Install {
        /// Package name to install
        #[facet(args::positional)]
        package: String,

        /// Install globally
        #[facet(args::named, args::short = 'g')]
        global: bool,

        /// Force reinstall even if already installed
        #[facet(args::named, args::short = 'f')]
        force: bool,
    },
    /// Remove a package
    #[facet(rename = "rm")]
    Remove {
        /// Package name to remove
        #[facet(args::positional)]
        package: String,

        /// Don't ask for confirmation
        #[facet(args::named, args::short = 'y')]
        yes: bool,
    },
    /// List installed packages
    #[facet(rename = "ls")]
    List {
        /// Show all versions
        #[facet(args::named, args::short = 'a')]
        all: bool,

        /// Output as JSON
        #[facet(args::named)]
        json: bool,
    },
}

#[test]

fn test_help_subcommand_install() {
    // `pkg install --help` should show help specific to the install subcommand
    let result = figue::from_slice::<PkgManager>(&["install", "--help"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.is_help(), "expected help error, got: {:?}", err);

    let help = err.help_text().expect("should have help text");
    assert_help_snapshot!("subcommand_install_help", help);
}

#[test]

fn test_help_subcommand_remove() {
    // `pkg rm --help` should show help specific to the remove subcommand
    let result = figue::from_slice::<PkgManager>(&["rm", "--help"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.is_help(), "expected help error, got: {:?}", err);

    let help = err.help_text().expect("should have help text");
    assert_help_snapshot!("subcommand_remove_help", help);
}

#[test]

fn test_help_subcommand_list() {
    // `pkg ls --help` should show help specific to the list subcommand
    let result = figue::from_slice::<PkgManager>(&["ls", "--help"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.is_help(), "expected help error, got: {:?}", err);

    let help = err.help_text().expect("should have help text");
    assert_help_snapshot!("subcommand_list_help", help);
}

#[test]
fn test_help_root_shows_all_subcommands() {
    // `pkg --help` should show the root help with all subcommands listed
    let result = figue::from_slice::<PkgManager>(&["--help"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.is_help(), "expected help error, got: {:?}", err);

    let help = err.help_text().expect("should have help text");
    assert_help_snapshot!("subcommand_root_help", help);
}

#[test]
fn test_html_help_after_subcommand_still_writes_root_document() {
    // `pkg install --html-help` should write the full app-wide HTML document,
    // not a document scoped only to the already-parsed `install` subcommand.
    let result = figue::from_slice::<PkgManager>(&["install", "--html-help"]).into_result();

    let Err(figue::DriverError::HtmlHelp { path }) = result else {
        panic!("expected HTML help request");
    };

    let html = std::fs::read_to_string(&path).expect("HTML help file should be readable");
    assert!(html.contains("<!doctype html>"));
    assert!(html.contains("CLI with subcommands for testing subcommand-specific help"));
    assert!(html.contains("Install a package"));
    assert!(html.contains("Remove a package"));
    assert!(html.contains("List installed packages"));
    assert!(html.contains("--html-help"));
    assert!(html.contains("id=\"command-install\""));
    assert!(html.contains("window.FIGUE_INITIAL_ANCHOR = \"command-install\""));
}

// Nested subcommands: help should be aware of the full path
#[derive(Facet, Debug)]
struct NestedCli {
    #[facet(args::subcommand)]
    command: TopLevel,

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

#[derive(Facet, Debug)]
#[repr(u8)]
#[allow(dead_code)]
enum TopLevel {
    /// Manage repositories
    Repo {
        #[facet(args::subcommand)]
        action: RepoCmd,
    },
    /// Show version
    Version,
}

#[derive(Facet, Debug)]
#[repr(u8)]
#[allow(dead_code)]
enum RepoCmd {
    /// Clone a repository
    Clone {
        /// Repository URL
        #[facet(args::positional)]
        url: String,

        /// Clone depth (shallow clone)
        #[facet(args::named)]
        depth: Option<u32>,

        /// Branch to clone
        #[facet(args::named, args::short = 'b')]
        branch: Option<String>,
    },
    /// Push changes
    Push {
        /// Remote name
        #[facet(args::positional, default)]
        remote: Option<String>,

        /// Force push
        #[facet(args::named, args::short = 'f')]
        force: bool,
    },
}

#[test]

fn test_help_nested_subcommand_clone() {
    // `myapp repo clone --help` should show clone-specific help
    let result = figue::from_slice::<NestedCli>(&["repo", "clone", "--help"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.is_help(), "expected help error, got: {:?}", err);

    let help = err.help_text().expect("should have help text");
    assert_help_snapshot!("nested_subcommand_clone_help", help);
}

#[test]

fn test_help_nested_subcommand_push() {
    // `myapp repo push --help` should show push-specific help
    let result = figue::from_slice::<NestedCli>(&["repo", "push", "--help"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.is_help(), "expected help error, got: {:?}", err);

    let help = err.help_text().expect("should have help text");
    assert_help_snapshot!("nested_subcommand_push_help", help);
}

#[test]

fn test_help_nested_intermediate_level() {
    // `myapp repo --help` should show repo-level help (listing clone, push)
    let result = figue::from_slice::<NestedCli>(&["repo", "--help"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.is_help(), "expected help error, got: {:?}", err);

    let help = err.help_text().expect("should have help text");
    assert_help_snapshot!("nested_intermediate_repo_help", help);
}

#[test]

fn test_help_short_flag_h_works_in_subcommand() {
    // `pkg install -h` should also work
    let result = figue::from_slice::<PkgManager>(&["install", "-h"]);
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(
        err.is_help(),
        "expected help error for -h flag, got: {:?}",
        err
    );
}