perforce-cli 0.1.0-alpha.11

A type-safe builder library for spawning Perforce (p4) commands, with compile-time option state isolation and multi-version support.
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! Scratch verification of every documented `p4 client` syntax form.
//!
//! This is a throwaway example: for each documented command line it walks
//! EVERY legal selection order through the type-state builder, asserting
//! that they all converge on the same arguments. The reference is the
//! current command reference:
//!
//! ```text
//! p4 client [-f] [-t template] [-T type] [name]
//! p4 client -d [-f [-Fs|-Fd]] name
//! p4 client -o [-t template] [-T type] [name]
//! p4 client -S stream [[-c change] -o] [name]
//! p4 client -s [-f] -S stream [name]
//! p4 client -s [-f] -t template [name]
//! p4 client -i [-f]
//! p4 client -d -f --serverid=X [-Fs] name
//! ```
//!
//! Run with:
//!
//! ```text
//! cargo run --example test
//! ```

use std::io;
use std::path::PathBuf;
use std::process::{Child, Stdio};

use perforce_cli::P4Cli;
use perforce_cli::cmd::SubCommand;
use perforce_cli::cmd::client::{Client, ShelvedHandling, WorkSpaceType};
use perforce_cli::global::GlobalOpts;
use perforce_cli::spawn::ParameterizedSpawn;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Collects the arguments assembled on a [`std::process::Command`] as
/// strings for easy comparison.
fn args_of(command: &std::process::Command) -> Vec<String> {
    command
        .get_args()
        .map(|arg| arg.to_string_lossy().into_owned())
        .collect()
}

/// Asserts the arguments produced by `setup_command` (everything except
/// the trailing optional/mandatory `name` operand, which is added on
/// spawn).
fn expect_args(cmd: &impl SubCommand, expected: &[&str]) {
    assert_eq!(args_of(&cmd.setup_command("p4")), expected);
}

/// Asserts that one chained builder expression assembles `expected`.
///
/// ```ignore
/// expect_chain!(["client", "-d", "-f", "-Fs"],
///               p4.client().delete().force().keep_shelved());
/// ```
macro_rules! expect_chain {
    ([$($e:literal),* $(,)?], $($chain:tt)*) => {{
        let cmd = $($chain)*;
        expect_args(&cmd, &[$($e),*]);
    }};
}

/// A tiny batch helper that just prints its arguments. `p4 client` only
/// appends the subcommand name, flags, and the optional `name`; none of
/// those contain spaces, so `%*` captures the exact argv tail.
fn echo_bin() -> PathBuf {
    let path = std::env::temp_dir().join("p4cli_client_echo_args.bat");
    std::fs::write(&path, "@echo %*\r\n").expect("write echo helper");
    path
}

/// Normalizes the echo helper output into logical arguments.
fn parse_echo(stdout: &[u8]) -> Vec<String> {
    // cmd.exe quotes args containing `=` (e.g. `--serverid=X`) when
    // launching the batch wrapper; those quotes are command-line
    // escaping, not part of the logical argument, so strip them.
    String::from_utf8_lossy(stdout)
        .split_whitespace()
        .map(|s| s.trim_matches('"').to_string())
        .collect()
}

/// Actually spawns the echo helper with one operand (the optional or
/// mandatory `name`) and returns the argv tail the helper received.
fn spawned_args<A, C>(cmd: &mut C, input: A) -> Vec<String>
where
    C: ParameterizedSpawn<A, Output = Child, Error = io::Error>,
{
    let output = cmd
        .spawn_with(input)
        .expect("spawn echo helper")
        .wait_with_output()
        .expect("wait for echo helper");
    assert!(output.status.success(), "echo helper exited unsuccessfully");
    parse_echo(&output.stdout)
}

/// Actually spawns the echo helper with no operand (`name` defaults to
/// `P4CLIENT`) and returns the argv tail the helper received.
fn spawned_args_unit<C>(cmd: &mut C) -> Vec<String>
where
    C: ParameterizedSpawn<(), Output = Child, Error = io::Error>,
{
    let output = cmd
        .spawn_with(())
        .expect("spawn echo helper")
        .wait_with_output()
        .expect("wait for echo helper");
    assert!(output.status.success(), "echo helper exited unsuccessfully");
    parse_echo(&output.stdout)
}

/// Starts a builder whose executable is the local echo helper.
fn echo_client() -> Client {
    Client::new(echo_bin(), GlobalOpts::new())
}

// Form 1 -------------------------------------------------------------------
// `p4 client [-f] [-t template] [-T type] [name]`
//
// `-f`, `-t`, and `-T` are orthogonal: every subset is legal and every
// selection order converges on syntax order.

fn form_1_general() {
    let p4 = P4Cli::default();
    let g = WorkSpaceType::Graph;

    // Every subset of {`-f`, `-t`, `-T`}.
    expect_chain!(["client"], p4.client());
    expect_chain!(["client", "-f"], p4.client().force());
    expect_chain!(["client", "-t", "tpl"], p4.client().template("tpl"));
    expect_chain!(["client", "-T", "graph"], p4.client().workspace_type(g));
    expect_chain!(
        ["client", "-f", "-t", "tpl"],
        p4.client().force().template("tpl")
    );
    expect_chain!(
        ["client", "-f", "-T", "graph"],
        p4.client().force().workspace_type(g)
    );
    expect_chain!(
        ["client", "-t", "tpl", "-T", "graph"],
        p4.client().template("tpl").workspace_type(g)
    );
    expect_chain!(
        ["client", "-t", "tpl", "-T", "graph"],
        p4.client().workspace_type(g).template("tpl")
    );

    // Every permutation (3!) of the full option set converges identically.
    expect_chain!(
        ["client", "-f", "-t", "tpl", "-T", "graph"],
        p4.client().force().template("tpl").workspace_type(g)
    );
    expect_chain!(
        ["client", "-f", "-t", "tpl", "-T", "graph"],
        p4.client().force().workspace_type(g).template("tpl")
    );
    expect_chain!(
        ["client", "-f", "-t", "tpl", "-T", "graph"],
        p4.client().template("tpl").force().workspace_type(g)
    );
    expect_chain!(
        ["client", "-f", "-t", "tpl", "-T", "graph"],
        p4.client().template("tpl").workspace_type(g).force()
    );
    expect_chain!(
        ["client", "-f", "-t", "tpl", "-T", "graph"],
        p4.client().workspace_type(g).force().template("tpl")
    );
    expect_chain!(
        ["client", "-f", "-t", "tpl", "-T", "graph"],
        p4.client().workspace_type(g).template("tpl").force()
    );

    // Every documented `-T type` value.
    for (ty, value) in [
        (WorkSpaceType::Writable, "writeable"),
        (WorkSpaceType::Readonly, "readonly"),
        (WorkSpaceType::Partitioned, "partitioned"),
        (WorkSpaceType::PartitionedJnl, "partitioned-jnl"),
        (WorkSpaceType::Graph, "graph"),
    ] {
        expect_args(&p4.client().workspace_type(ty), &["client", "-T", value]);
    }

    // The `set_*` builder variants mutate an already-selected option.
    let mut cmd = p4
        .client()
        .template("tmp")
        .workspace_type(WorkSpaceType::Readonly);
    cmd.set_template("tpl").set_workspace_type(g);
    assert_eq!(cmd.get_template(), "tpl");
    assert_eq!(cmd.get_workspace_type(), g);
    expect_args(&cmd, &["client", "-t", "tpl", "-T", "graph"]);

    // The trailing `name` operand is optional; it is appended last on
    // spawn, both with the unit parameter and the one-arg parameter.
    let mut cmd = echo_client();
    assert_eq!(spawned_args_unit(&mut cmd), ["client"]);
    let mut cmd = echo_client();
    assert_eq!(spawned_args(&mut cmd, ("ws-name",)), ["client", "ws-name"]);

    let mut cmd = echo_client().force().template("tpl").workspace_type(g);
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-f", "-t", "tpl", "-T", "graph", "ws-name"]
    );
}

// Form 2 -------------------------------------------------------------------
// `p4 client -d [-f [-Fs|-Fd]] name`
//
// `-f` must accompany `-Fs`/`-Fd`, but the documented form imposes no
// selection order: every path below must reach the same command line.

fn form_2_delete() {
    let p4 = P4Cli::default();

    expect_chain!(["client", "-d"], p4.client().delete());

    // `-d -f`: either order of `-d` and `-f`.
    expect_chain!(["client", "-d", "-f"], p4.client().delete().force());
    expect_chain!(["client", "-d", "-f"], p4.client().force().delete());

    // `-d -f -Fs`: all five legal selection orders.
    expect_chain!(
        ["client", "-d", "-f", "-Fs"],
        p4.client().delete().force().keep_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "-Fs"],
        p4.client().force().delete().keep_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "-Fs"],
        p4.client().force().keep_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "-Fs"],
        p4.client().keep_shelved().force()
    );
    expect_chain!(
        ["client", "-d", "-f", "-Fs"],
        p4.client().delete().keep_shelved().force()
    );

    // `-d -f -Fd`: all five legal selection orders.
    expect_chain!(
        ["client", "-d", "-f", "-Fd"],
        p4.client().delete().force().delete_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "-Fd"],
        p4.client().force().delete().delete_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "-Fd"],
        p4.client().force().delete_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "-Fd"],
        p4.client().delete_shelved().force()
    );
    expect_chain!(
        ["client", "-d", "-f", "-Fd"],
        p4.client().delete().delete_shelved().force()
    );

    // Getters reflect the terminal selection.
    let cmd = p4.client().delete_shelved().force();
    assert_eq!(cmd.get_shelved(), Some(ShelvedHandling::Delete));
    let cmd = p4.client().keep_shelved().force();
    assert_eq!(cmd.get_shelved(), Some(ShelvedHandling::Keep));

    // `name` is mandatory and is the trailing operand for every variant.
    let mut cmd = echo_client().delete();
    assert_eq!(
        spawned_args(&mut cmd, ("old-ws",)),
        ["client", "-d", "old-ws"]
    );

    let mut cmd = echo_client().delete_shelved().force();
    assert_eq!(
        spawned_args(&mut cmd, ("old-ws",)),
        ["client", "-d", "-f", "-Fd", "old-ws"]
    );

    let mut cmd = echo_client().keep_shelved().force();
    assert_eq!(
        spawned_args(&mut cmd, ("old-ws",)),
        ["client", "-d", "-f", "-Fs", "old-ws"]
    );
}

// Form 3 -------------------------------------------------------------------
// `p4 client -o [-t template] [-T type] [name]`
//
// `-t`/`-T` may be selected before or after entering the `-o` state, in
// any order; `-f` is not part of this form.

fn form_3_stdout() {
    let p4 = P4Cli::default();
    let y = WorkSpaceType::Partitioned;

    expect_chain!(["client", "-o"], p4.client().stdout());

    // `-t` and `-T`, selected before `.stdout()`.
    expect_chain!(
        ["client", "-o", "-t", "tpl"],
        p4.client().template("tpl").stdout()
    );
    expect_chain!(
        ["client", "-o", "-T", "partitioned"],
        p4.client().workspace_type(y).stdout()
    );
    expect_chain!(
        ["client", "-o", "-t", "tpl", "-T", "partitioned"],
        p4.client().template("tpl").workspace_type(y).stdout()
    );
    expect_chain!(
        ["client", "-o", "-t", "tpl", "-T", "partitioned"],
        p4.client().workspace_type(y).template("tpl").stdout()
    );

    // `-t` and `-T`, selected after `.stdout()`.
    expect_chain!(
        ["client", "-o", "-t", "tpl"],
        p4.client().stdout().template("tpl")
    );
    expect_chain!(
        ["client", "-o", "-T", "partitioned"],
        p4.client().stdout().workspace_type(y)
    );
    expect_chain!(
        ["client", "-o", "-t", "tpl", "-T", "partitioned"],
        p4.client().stdout().template("tpl").workspace_type(y)
    );
    expect_chain!(
        ["client", "-o", "-t", "tpl", "-T", "partitioned"],
        p4.client().stdout().workspace_type(y).template("tpl")
    );

    // One option before `.stdout()` and one after it, in both orders.
    expect_chain!(
        ["client", "-o", "-t", "tpl", "-T", "partitioned"],
        p4.client().template("tpl").stdout().workspace_type(y)
    );
    expect_chain!(
        ["client", "-o", "-t", "tpl", "-T", "partitioned"],
        p4.client().workspace_type(y).stdout().template("tpl")
    );

    // Optional `name`.
    let mut cmd = echo_client().stdout();
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-o", "ws-name"]
    );
    let mut cmd = echo_client().template("tpl").workspace_type(y).stdout();
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-o", "-t", "tpl", "-T", "partitioned", "ws-name"]
    );
}

// Form 4 -------------------------------------------------------------------
// `p4 client -S stream [[-c change] -o] [name]`
//
// `-o` optionally follows `-S stream`; `-c change` is only meaningful
// together with `-o` and is always injected before it.

fn form_4_stream() {
    let p4 = P4Cli::default();

    let cmd = p4.client().stream("//stream/main");
    assert_eq!(cmd.get_stream(), "//stream/main");
    expect_args(&cmd, &["client", "-S", "//stream/main"]);

    // `set_stream` mutates the selected stream.
    let mut cmd = p4.client().stream("old");
    cmd.set_stream("//stream/dev");
    expect_args(&cmd, &["client", "-S", "//stream/dev"]);

    expect_chain!(
        ["client", "-S", "//stream/main", "-o"],
        p4.client().stream("//stream/main").stdout()
    );

    let mut cmd = p4.client().stream("//stream/main").stdout();
    assert_eq!(cmd.get_change(), None);
    cmd.set_change("4321");
    assert_eq!(cmd.get_change(), Some("4321"));
    expect_args(&cmd, &["client", "-S", "//stream/main", "-c", "4321", "-o"]);
    expect_chain!(
        ["client", "-S", "//stream/main", "-c", "4321", "-o"],
        p4.client().stream("//stream/main").stdout().change("4321")
    );

    // Optional `name` for each of the three variants.
    let mut cmd = echo_client().stream("//stream/main");
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-S", "//stream/main", "ws-name"]
    );
    let mut cmd = echo_client().stream("//stream/main").stdout();
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-S", "//stream/main", "-o", "ws-name"]
    );
    let mut cmd = echo_client()
        .stream("//stream/main")
        .stdout()
        .change("4321");
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        [
            "client",
            "-S",
            "//stream/main",
            "-c",
            "4321",
            "-o",
            "ws-name"
        ]
    );
}

// Form 5 -------------------------------------------------------------------
// `p4 client -s [-f] -S stream [name]`
//
// Two entry paths, symmetric with form 6:
//
// 1. direct:   `switch_view_stream(stream)` — `-f` may precede or follow
// 2. two-step: `stream(stream)` then `switch_view()` — the intermediate
//    `-S stream` form rejects `-f`, so force can only follow `switch_view`
//
// `name` is optional on every variant (unit or one-arg spawn).

fn form_5_switch_stream() {
    let p4 = P4Cli::default();

    // --- Path 1: direct `switch_view_stream`, no `-f` ---

    let cmd = p4.client().switch_view_stream("//stream/dev");
    assert_eq!(cmd.get_stream(), "//stream/dev");
    expect_args(&cmd, &["client", "-s", "-S", "//stream/dev"]);

    let mut cmd = p4.client().switch_view_stream("old");
    cmd.set_stream("//stream/dev");
    expect_args(&cmd, &["client", "-s", "-S", "//stream/dev"]);

    // `-f` before or after the switch transition, same command line.
    expect_chain!(
        ["client", "-s", "-f", "-S", "//stream/dev"],
        p4.client().force().switch_view_stream("//stream/dev")
    );
    expect_chain!(
        ["client", "-s", "-f", "-S", "//stream/dev"],
        p4.client().switch_view_stream("//stream/dev").force()
    );
    // `set_stream` then `-f` as well.
    let mut cmd = p4.client().switch_view_stream("old");
    cmd.set_stream("//stream/dev");
    expect_args(&cmd.force(), &["client", "-s", "-f", "-S", "//stream/dev"]);

    // The value stays editable (set / builder / getter) once `-f` is set.
    let mut cmd = p4.client().switch_view_stream("old").force();
    cmd.set_stream("//stream/dev");
    expect_args(&cmd, &["client", "-s", "-f", "-S", "//stream/dev"]);
    expect_chain!(
        ["client", "-s", "-f", "-S", "//stream/dev"],
        p4.client()
            .switch_view_stream("old")
            .force()
            .stream("//stream/dev")
    );
    let cmd = p4.client().force().switch_view_stream("//stream/dev");
    assert_eq!(cmd.get_stream(), "//stream/dev");

    // --- Path 2: `stream(..).switch_view()` ---

    // Without `-f`: selecting `-s` injects no `-f`.
    expect_chain!(
        ["client", "-s", "-S", "//stream/dev"],
        p4.client().stream("//stream/dev").switch_view()
    );

    // The stream value is still changeable on the intermediate `-S`
    // form before `-s` is added.
    let mut cmd = p4.client().stream("old");
    cmd.set_stream("//stream/dev");
    expect_args(&cmd.switch_view(), &["client", "-s", "-S", "//stream/dev"]);
    expect_chain!(
        ["client", "-s", "-S", "//stream/dev"],
        p4.client()
            .stream("old")
            .stream("//stream/dev")
            .switch_view()
    );

    // `-f` comes after `switch_view`; the intermediate `-S` form is the
    // form 4 state, which rejects `-f`.
    expect_chain!(
        ["client", "-s", "-f", "-S", "//stream/dev"],
        p4.client().stream("//stream/dev").switch_view().force()
    );
    let cmd = p4.client().stream("//stream/dev").switch_view();
    assert_eq!(cmd.get_stream(), "//stream/dev");

    // --- Spawns: path 1, `name` omitted (unit), all three `-f` positions ---

    let mut cmd = echo_client().switch_view_stream("//stream/dev");
    assert_eq!(
        spawned_args_unit(&mut cmd),
        ["client", "-s", "-S", "//stream/dev"]
    );
    let mut cmd = echo_client().force().switch_view_stream("//stream/dev");
    assert_eq!(
        spawned_args_unit(&mut cmd),
        ["client", "-s", "-f", "-S", "//stream/dev"]
    );
    let mut cmd = echo_client().switch_view_stream("//stream/dev").force();
    assert_eq!(
        spawned_args_unit(&mut cmd),
        ["client", "-s", "-f", "-S", "//stream/dev"]
    );

    // --- Spawns: path 1, trailing `name` ---

    let mut cmd = echo_client().switch_view_stream("//stream/dev");
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-s", "-S", "//stream/dev", "ws-name"]
    );
    let mut cmd = echo_client().force().switch_view_stream("//stream/dev");
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-s", "-f", "-S", "//stream/dev", "ws-name"]
    );
    let mut cmd = echo_client().switch_view_stream("//stream/dev").force();
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-s", "-f", "-S", "//stream/dev", "ws-name"]
    );

    // --- Spawns: path 2, `name` omitted and trailing, without/with `-f` ---

    let mut cmd = echo_client().stream("//stream/dev").switch_view();
    assert_eq!(
        spawned_args_unit(&mut cmd),
        ["client", "-s", "-S", "//stream/dev"]
    );
    let mut cmd = echo_client().stream("//stream/dev").switch_view().force();
    assert_eq!(
        spawned_args_unit(&mut cmd),
        ["client", "-s", "-f", "-S", "//stream/dev"]
    );

    let mut cmd = echo_client().stream("//stream/dev").switch_view();
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-s", "-S", "//stream/dev", "ws-name"]
    );
    let mut cmd = echo_client().stream("//stream/dev").switch_view().force();
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-s", "-f", "-S", "//stream/dev", "ws-name"]
    );
}

// Form 6 -------------------------------------------------------------------
// `p4 client -s [-f] -t template [name]`
//
// Reached either directly with `switch_view_template`, or by selecting
// `-t template` in the general form and then `switch_view`.

fn form_6_switch_template() {
    let p4 = P4Cli::default();

    let cmd = p4.client().switch_view_template("tpl");
    assert_eq!(cmd.get_template(), "tpl");
    expect_args(&cmd, &["client", "-s", "-t", "tpl"]);

    let mut cmd = p4.client().switch_view_template("old");
    cmd.set_template("tpl");
    expect_args(&cmd, &["client", "-s", "-t", "tpl"]);

    expect_chain!(
        ["client", "-s", "-t", "tpl"],
        p4.client().template("tpl").switch_view()
    );

    // `-f` in every legal position relative to the two entry paths.
    expect_chain!(
        ["client", "-s", "-f", "-t", "tpl"],
        p4.client().force().switch_view_template("tpl")
    );
    expect_chain!(
        ["client", "-s", "-f", "-t", "tpl"],
        p4.client().switch_view_template("tpl").force()
    );
    expect_chain!(
        ["client", "-s", "-f", "-t", "tpl"],
        p4.client().force().template("tpl").switch_view()
    );
    expect_chain!(
        ["client", "-s", "-f", "-t", "tpl"],
        p4.client().template("tpl").switch_view().force()
    );

    // Optional `name`, with and without `-f`.
    let mut cmd = echo_client().switch_view_template("tpl");
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-s", "-t", "tpl", "ws-name"]
    );
    let mut cmd = echo_client().force().switch_view_template("tpl");
    assert_eq!(
        spawned_args(&mut cmd, ("ws-name",)),
        ["client", "-s", "-f", "-t", "tpl", "ws-name"]
    );
}

// Form 7 -------------------------------------------------------------------
// `p4 client -i [-f]`
//
// The specification arrives on standard input; this form takes no `name`
// operand and spawns with a stdin configuration instead.

fn form_7_stdin() {
    let p4 = P4Cli::default();

    expect_chain!(["client", "-i"], p4.client().stdin());
    expect_chain!(["client", "-i", "-f"], p4.client().force().stdin());
    expect_chain!(["client", "-i", "-f"], p4.client().stdin().force());

    // Spawn uses piped stdin (`()` parameter) or an explicit `Stdio`.
    let mut cmd = echo_client().stdin();
    assert_eq!(spawned_args_unit(&mut cmd), ["client", "-i"]);

    let mut cmd = echo_client().force().stdin();
    let output = cmd
        .spawn_with((Stdio::null(),))
        .expect("spawn echo helper")
        .wait_with_output()
        .expect("wait for echo helper");
    assert_eq!(parse_echo(&output.stdout), ["client", "-i", "-f"]);
}

// Form 8 -------------------------------------------------------------------
// `p4 client -d -f --serverid=X [-Fs] name`
//
// `--serverid=X` combines with no shelved option or with `-Fs` (never
// `-Fd`); again, the documented form fixes neither entry path nor option
// order, so every legal permutation must converge.

fn form_8_server_id() {
    let p4 = P4Cli::default();
    let x = "edge.1667";

    // `-d -f --serverid=X`: all five legal selection orders.
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667"],
        p4.client().force().server_id(x)
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667"],
        p4.client().server_id(x).force()
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667"],
        p4.client().delete().force().server_id(x)
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667"],
        p4.client().force().delete().server_id(x)
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667"],
        p4.client().delete().server_id(x).force()
    );

    // Getter/setter on the selected value, before force is added.
    let mut cmd = p4.client().server_id(x);
    assert_eq!(cmd.get_server_id(), x);
    assert_eq!(cmd.get_shelved(), None);
    cmd.set_server_id("edge.42");
    let cmd = cmd.force();
    assert_eq!(cmd.get_server_id(), "edge.42");
    expect_args(&cmd, &["client", "-d", "-f", "--serverid=edge.42"]);

    // `-d -f --serverid=X -Fs`: every permutation of
    // {`-f`, `--serverid=X`, `-Fs`} (3! = 6) ...
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().force().server_id(x).keep_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().force().keep_shelved().server_id(x)
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().server_id(x).force().keep_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().server_id(x).keep_shelved().force()
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().keep_shelved().force().server_id(x)
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().keep_shelved().server_id(x).force()
    );

    // ... and the same six with an explicit `-d` selected first.
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().delete().force().server_id(x).keep_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().delete().force().keep_shelved().server_id(x)
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().delete().server_id(x).force().keep_shelved()
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().delete().server_id(x).keep_shelved().force()
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().delete().keep_shelved().force().server_id(x)
    );
    expect_chain!(
        ["client", "-d", "-f", "--serverid=edge.1667", "-Fs"],
        p4.client().delete().keep_shelved().server_id(x).force()
    );

    // Getters on a final state.
    let cmd = p4.client().keep_shelved().server_id(x).force();
    assert_eq!(cmd.get_server_id(), x);
    assert_eq!(cmd.get_shelved(), Some(ShelvedHandling::Keep));

    // Mandatory trailing `name` operand.
    let mut cmd = echo_client().server_id(x).force();
    assert_eq!(
        spawned_args(&mut cmd, ("old-ws",)),
        ["client", "-d", "-f", "--serverid=edge.1667", "old-ws"]
    );
    let mut cmd = echo_client().keep_shelved().server_id(x).force();
    assert_eq!(
        spawned_args(&mut cmd, ("old-ws",)),
        [
            "client",
            "-d",
            "-f",
            "--serverid=edge.1667",
            "-Fs",
            "old-ws"
        ]
    );
}

// Rejected combinations -----------------------------------------------------
//
// The chains below contradict the documented forms and must fail to
// compile; they are kept as executable documentation of the type-state
// rules. Uncomment any line to verify it is rejected.
//
// // `-Fd` can never be combined with `--serverid=X` (form 8 allows [-Fs] only).
// let _ = P4Cli::default().client().server_id("x").delete_shelved();
// let _ = P4Cli::default().client().delete_shelved().server_id("x");
// let _ = P4Cli::default().client().force().server_id("x").delete_shelved();
// let _ = P4Cli::default().client().force().delete_shelved().server_id("x");
//
// // `-Fs` and `-Fd` are mutually exclusive.
// let _ = P4Cli::default().client().keep_shelved().delete_shelved();
// let _ = P4Cli::default().client().delete_shelved().keep_shelved();
//
// // `-Fs` / `-Fd` / `--serverid` REQUIRE `-f`: selecting them is allowed in
// // any order, but the resulting intermediate state cannot be spawned.
// let mut pending = echo_client().delete_shelved();
// pending.spawn_with(("old-ws",));
// let mut pending = echo_client().server_id("x");
// pending.spawn_with(("old-ws",));
// let mut pending = echo_client().keep_shelved();
// pending.spawn_with(("old-ws",));
//
// // `-o` does not accept `-f`.
// let _ = P4Cli::default().client().force().stdout();
//
// // `-S stream` does not accept `-f`, `-t`, or `-T`.
// let _ = P4Cli::default().client().force().stream("//s");
// let _ = P4Cli::default().client().template("t").stream("//s");
// let _ = P4Cli::default().client().workspace_type(WorkSpaceType::Graph).stream("//s");
//
// // `-c change` requires `-o` on the `-S stream` form.
// let _ = P4Cli::default().client().stream("//s").change("4321");
//
// // Once `-o` is selected on the `-S stream` form, the `-s` switch is no
// // longer available (the `-S stream -o` form has no `-s` flag).
// let _ = P4Cli::default().client().stream("//s").stdout().switch_view();
//
// // The two-step `-s -S stream` path cannot carry a preselected `-f`:
// // the intermediate `-S` form rejects it, so force must follow
// // `switch_view` (use `force().switch_view_stream(s)` for force-first).
// let _ = P4Cli::default().client().force().stream("//s").switch_view();
//
// // `-i` takes no `name` operand: the specification comes from stdin.
// // (`Client<StdinMode>` implements no one-argument spawn.)
//
// // `-d` forms require `name`: `Client<DeleteMode, _>` implements no unit
// // spawn.

fn main() -> io::Result<()> {
    form_1_general();
    form_2_delete();
    form_3_stdout();
    form_4_stream();
    form_5_switch_stream();
    form_6_switch_template();
    form_7_stdin();
    form_8_server_id();
    println!("all p4 client syntax combinations verified");
    Ok(())
}