standout 14.0.0

Styled CLI template rendering with automatic terminal detection
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
use clap::Subcommand;
use serde_json::json;
use standout::cli::{App, CommandContext, Dispatch, GroupBuilder, HandlerResult, Output};
use standout::ColorPolicy;
use standout::{EmbeddedSource, Representation, TemplateResource};
use standout_test::TestHarness;

mod handlers {
    use super::*;
    use clap::ArgMatches;

    pub fn list(_matches: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<()> {
        Ok(Output::Silent)
    }

    pub fn add(_matches: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<()> {
        Ok(Output::Silent)
    }

    pub fn show_all(_matches: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<()> {
        Ok(Output::Silent)
    }

    pub fn export(
        _matches: &ArgMatches,
        _ctx: &CommandContext,
    ) -> HandlerResult<serde_json::Value> {
        Ok(Output::Render(json!({"name": "Ada"})))
    }

    pub fn download(_matches: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<()> {
        Ok(Output::Binary {
            data: vec![1, 2, 3],
            filename: "data.bin".to_string(),
        })
    }

    macro_rules! silent_handlers {
        ($($name:ident),* $(,)?) => {
            $(
                pub fn $name(_matches: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<()> {
                    Ok(Output::Silent)
                }
            )*
        };
    }

    silent_handlers!(
        x2fa,
        a1b2,
        sha256_sum,
        utf8_check,
        http_server,
        list_units,
        r#move
    );
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum BasicCommands {
    List,
    Add,
}

#[test]
fn test_basic_dispatch_compiles() {
    let config: fn(GroupBuilder) -> GroupBuilder =
        |builder| BasicCommands::dispatch_config()(builder);
    let _ = config;
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum MultiWordCommands {
    ShowAll,
}

#[test]
fn multi_word_variant_registers_the_kebab_case_name() {
    let builder = MultiWordCommands::dispatch_config()(GroupBuilder::new());
    assert!(builder.contains("show-all"));
    assert!(!builder.contains("show_all"));
}

/// Digit/acronym runs and raw identifiers are where a hand-rolled splitter and `heck` part ways.
#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum ParityCommands {
    X2FA,
    A1B2,
    Sha256Sum,
    Utf8Check,
    HTTPServer,
    ListUnits,
    r#Move,
}

#[test]
fn derived_names_match_the_ones_clap_registers() {
    let clap_names: Vec<String> = ParityCommands::augment_subcommands(clap::Command::new("app"))
        .get_subcommands()
        .map(|sub| sub.get_name().to_string())
        .collect();
    assert_eq!(
        clap_names,
        [
            "x2fa",
            "a1b2",
            "sha256-sum",
            "utf8-check",
            "http-server",
            "list-units",
            "move"
        ]
    );

    let builder = ParityCommands::dispatch_config()(GroupBuilder::new());
    for name in &clap_names {
        assert!(builder.contains(name), "dispatch did not register `{name}`");
    }
}

/// Every `#[dispatch(...)]` on a variant applies, not only the first.
#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum SplitAttrCommands {
    #[dispatch(name = "listing")]
    #[dispatch(default)]
    ListUnits,
}

#[test]
fn attributes_spread_over_several_dispatch_attrs_all_apply() {
    let builder = SplitAttrCommands::dispatch_config()(GroupBuilder::new());
    assert!(builder.contains("listing"));
    assert!(!builder.contains("list-units"));
    assert_eq!(builder.get_default_command(), Some("listing"));
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum RenamedCommands {
    #[dispatch(name = "ls", handler = handlers::list)]
    List,
    #[dispatch(name = "show", default)]
    ShowAll,
}

#[test]
fn variant_rename_replaces_the_derived_name() {
    let builder = RenamedCommands::dispatch_config()(GroupBuilder::new());
    assert!(builder.contains("ls"));
    assert!(builder.contains("show"));
    assert!(!builder.contains("list"));
    assert!(!builder.contains("show-all"));
    assert_eq!(builder.get_default_command(), Some("show"));
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum OverrideCommands {
    #[dispatch(handler = handlers::list)]
    Custom,
}

#[test]
fn test_handler_override_compiles() {
    let _ = OverrideCommands::dispatch_config();
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum TemplateCommands {
    #[dispatch(template_name = "custom.j2")]
    List,
}

#[test]
fn test_template_override_compiles() {
    let _ = TemplateCommands::dispatch_config();
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum TemplateAbsenceCommands {
    #[dispatch(template_name = "list")]
    Export,
    #[dispatch(silent)]
    Add,
    #[dispatch(binary, handler = handlers::download)]
    Download,
    #[dispatch(structured_only, handler = handlers::export)]
    ShowAll,
}

#[test]
fn test_template_absence_attributes_build_mixed_apps() {
    let app = App::builder()
        .templates(EmbeddedSource::<TemplateResource>::new(
            &[("list.jinja", "Hello {{ name }}")],
            "/path/that/does/not/exist",
        ))
        .commands(TemplateAbsenceCommands::dispatch_config())
        .unwrap()
        .build()
        .unwrap();
    let command = || {
        clap::Command::new("app")
            .subcommand(clap::Command::new("export"))
            .subcommand(clap::Command::new("add"))
            .subcommand(clap::Command::new("download"))
            .subcommand(clap::Command::new("show-all"))
    };

    let rendered =
        TestHarness::new()
            .color(ColorPolicy::Never)
            .run(&app, command(), ["app", "export"]);
    rendered.assert_success();
    assert_eq!(rendered.stdout(), "Hello Ada");

    let silent = TestHarness::new().run(&app, command(), ["app", "add"]);
    silent.assert_success();
    assert_eq!(silent.stdout(), "");

    let binary = TestHarness::new().run(&app, command(), ["app", "download"]);
    binary.assert_success();
    assert_eq!(binary.binary(), Some((&[1, 2, 3][..], "data.bin")));

    let structured = TestHarness::new().output_mode(Representation::Json).run(
        &app,
        command(),
        ["app", "show-all"],
    );
    structured.assert_success();
    assert_eq!(structured.stdout(), "{\n  \"name\": \"Ada\"\n}");
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum SkipCommands {
    List,
    #[dispatch(skip)]
    Hidden,
}

#[test]
fn test_skip_attribute_compiles() {
    let _ = SkipCommands::dispatch_config();
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = handlers)]
enum DefaultCommands {
    #[dispatch(default)]
    List,
    Add,
}

#[test]
fn test_default_command_compiles() {
    let _ = DefaultCommands::dispatch_config();
}

#[test]
fn test_default_command_sets_default() {
    let builder = DefaultCommands::dispatch_config()(GroupBuilder::new());
    assert_eq!(builder.get_default_command(), Some("list"));
}

#[test]
fn test_default_command_registers_commands() {
    let builder = DefaultCommands::dispatch_config()(GroupBuilder::new());
    assert!(builder.contains("list"));
    assert!(builder.contains("add"));
}

mod input_handlers {
    use super::*;
    use clap::ArgMatches;
    use standout::cli::{CommandConfig, CommandContextInput};
    use standout::input::{ArgSource, InputChain, StdinSource};

    pub fn note_inputs<H>(config: CommandConfig<H>) -> CommandConfig<H> {
        config.input(
            "note",
            InputChain::<String>::new()
                .try_source(ArgSource::new("note"))
                .try_source(StdinSource::new())
                .validate(
                    |note: &String| !note.trim().is_empty(),
                    "note cannot be empty",
                ),
        )
    }

    pub fn write(_matches: &ArgMatches, ctx: &CommandContext) -> HandlerResult<serde_json::Value> {
        let note: &String = ctx.input("note").expect("note is resolved before dispatch");
        Ok(Output::Render(json!({ "note": note })))
    }
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = input_handlers)]
enum InputCommands {
    #[dispatch(inputs = input_handlers::note_inputs, structured_only)]
    Write { note: Option<String> },
}

fn input_command() -> clap::Command {
    clap::Command::new("app")
        .subcommand(clap::Command::new("write").arg(clap::Arg::new("note").long("note")))
}

#[test]
fn inputs_attribute_resolves_a_chain_for_a_derive_registered_command() {
    let app = App::builder()
        .commands(InputCommands::dispatch_config())
        .unwrap()
        .build()
        .unwrap();

    let from_arg = TestHarness::new().output_mode(Representation::Json).run(
        &app,
        input_command(),
        ["app", "write", "--note", "from the argument"],
    );
    from_arg.assert_success();
    assert!(from_arg.stdout().contains("from the argument"));

    let from_stdin = TestHarness::new()
        .output_mode(Representation::Json)
        .piped_stdin("from stdin\n")
        .run(&app, input_command(), ["app", "write"]);
    from_stdin.assert_success();
    assert!(from_stdin.stdout().contains("from stdin"));

    let rejected = TestHarness::new().output_mode(Representation::Json).run(
        &app,
        input_command(),
        ["app", "write", "--note", "   "],
    );
    rejected.assert_error_contains("note cannot be empty");
}

mod hook_handlers {
    use super::*;
    use clap::ArgMatches;
    use standout::cli::hooks::HookError;
    use std::cell::RefCell;

    thread_local! {
        static ORDER: RefCell<Vec<&'static str>> = const { RefCell::new(Vec::new()) };
    }

    fn record(step: &'static str) {
        ORDER.with(|order| order.borrow_mut().push(step));
    }

    pub fn reset() {
        ORDER.with(|order| order.borrow_mut().clear());
    }

    pub fn steps() -> Vec<&'static str> {
        ORDER.with(|order| order.borrow().clone())
    }

    pub fn first(_matches: &ArgMatches, _ctx: &mut CommandContext) -> Result<(), HookError> {
        record("first");
        Ok(())
    }

    pub fn second(_matches: &ArgMatches, _ctx: &mut CommandContext) -> Result<(), HookError> {
        record("second");
        Ok(())
    }

    pub fn chained(_matches: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<()> {
        record("handler");
        Ok(Output::Silent)
    }

    pub fn single(_matches: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<()> {
        record("handler");
        Ok(Output::Silent)
    }
}

#[derive(Subcommand, Dispatch)]
#[dispatch(handlers = hook_handlers)]
enum HookCommands {
    #[dispatch(pre_dispatch(hook_handlers::first, hook_handlers::second), silent)]
    Chained,
    #[dispatch(pre_dispatch = hook_handlers::first, silent)]
    Single,
}

fn hook_command() -> clap::Command {
    clap::Command::new("app")
        .subcommand(clap::Command::new("chained"))
        .subcommand(clap::Command::new("single"))
}

fn hook_app() -> App {
    App::builder()
        .commands(HookCommands::dispatch_config())
        .unwrap()
        .build()
        .unwrap()
}

#[test]
fn a_pre_dispatch_list_runs_every_hook_in_written_order() {
    let app = hook_app();
    hook_handlers::reset();
    TestHarness::new()
        .run(&app, hook_command(), ["app", "chained"])
        .assert_success();
    assert_eq!(hook_handlers::steps(), ["first", "second", "handler"]);
}

#[test]
fn the_single_path_pre_dispatch_spelling_registers_that_one_hook() {
    let app = hook_app();
    hook_handlers::reset();
    TestHarness::new()
        .run(&app, hook_command(), ["app", "single"])
        .assert_success();
    assert_eq!(hook_handlers::steps(), ["first", "handler"]);
}