Skip to main content

haz_cli/cli/
init.rs

1//! Parser for the `haz init` subcommand.
2//!
3//! Owns [`InitArgs`] and the [`init_command`] parser builder.
4//! `AUX-001` admits the bare interactive form and the `--yes`
5//! minimal-scaffold form; both parse here and the executor selects
6//! the branch.
7
8use bpaf::{Parser, construct, long};
9
10/// Arguments parsed for the `haz init` subcommand.
11///
12/// `AUX-001` admits exactly two invocation shapes: the bare form
13/// (which would run the interactive scaffold flow,
14/// `AUX-003`..`AUX-005`) and `--yes` (which writes the minimal
15/// `projects: []` workspace per `AUX-007`). Both forms parse;
16/// the executor selects the branch.
17#[derive(Debug, Clone, PartialEq, Eq, Default)]
18pub struct InitArgs {
19    /// `--yes`: skip the interactive flow per `AUX-007`.
20    pub yes: bool,
21}
22
23pub(super) fn init_command() -> impl Parser<InitArgs> {
24    let yes = long("yes")
25        .help("Skip the interactive flow and write a minimal workspace (AUX-007)")
26        .switch();
27    construct!(InitArgs { yes })
28}
29
30#[cfg(test)]
31mod tests {
32    use super::InitArgs;
33    use crate::cli::{Cmd, parse};
34
35    #[test]
36    fn aux_001_init_bare_parses_with_yes_false() {
37        assert_eq!(
38            parse(&["init"]).unwrap(),
39            Cmd::Init(InitArgs { yes: false }),
40        );
41    }
42
43    #[test]
44    fn aux_001_init_with_yes_flag_parses() {
45        assert_eq!(
46            parse(&["init", "--yes"]).unwrap(),
47            Cmd::Init(InitArgs { yes: true }),
48        );
49    }
50
51    #[test]
52    fn aux_001_init_rejects_positional_argument() {
53        let err = parse(&["init", "extra"]).unwrap_err();
54        assert!(
55            !err.is_empty(),
56            "positional argument must produce a diagnostic: {err}",
57        );
58    }
59
60    #[test]
61    fn aux_001_init_rejects_unknown_flag() {
62        let err = parse(&["init", "--no-such-flag"]).unwrap_err();
63        assert!(
64            !err.is_empty(),
65            "unknown flag must produce a diagnostic: {err}",
66        );
67    }
68}