Skip to main content

commit_wizard/cli/
args.rs

1//! Command-line argument definitions and parsing.
2//!
3//! This module defines the CLI argument structure using `clap`, including global flags,
4//! subcommands, and command-specific options. All arguments are converted to [`AppContextArgs`]
5//! for use by the application bootstrap process.
6
7use std::path::PathBuf;
8
9use crate::{cli::cmd, core::bootstrap::AppContextArgs};
10use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};
11
12/// Output format enumeration for results
13#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
14pub enum FormatArg {
15    Auto,
16    Text,
17    Markdown,
18    Json,
19    Jsonl,
20    Plain,
21}
22
23impl FormatArg {
24    pub fn to_output_format_string(&self, use_json: bool) -> String {
25        match (use_json, self) {
26            (_, FormatArg::Text) => "text".to_string(),
27            (_, FormatArg::Markdown) => "markdown".to_string(),
28            (_, FormatArg::Json) => "json".to_string(),
29            (_, FormatArg::Jsonl) => "jsonl".to_string(),
30            (_, FormatArg::Plain) => "plain".to_string(),
31            (true, FormatArg::Auto) => "json".to_string(),
32            (false, FormatArg::Auto) => "text".to_string(),
33        }
34    }
35}
36
37#[derive(Debug, Clone, Copy, ValueEnum)]
38pub enum ColorMode {
39    Auto,
40    Always,
41    Never,
42}
43
44impl std::fmt::Display for ColorMode {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        match self {
47            ColorMode::Auto => write!(f, "auto"),
48            ColorMode::Always => write!(f, "always"),
49            ColorMode::Never => write!(f, "never"),
50        }
51    }
52}
53
54/// 🧙📖 Commit Wizard - A Spellbook for Conventional Commits, Semantic Versioning, and Changelog Automation.
55#[derive(Parser, Debug)]
56#[command(
57    name = "cw",
58    version,
59    author,
60    about = "🧙📜🪄📖✨ Commit Wizard - A Spellbook for Conventional Commits, Semantic Versioning, and Changelog Automation.",
61    propagate_version = true,
62    arg_required_else_help = true
63)]
64pub struct Cli {
65    /// Global flags (apply to all subcommands)
66    #[command(flatten)]
67    pub global: GlobalArgs,
68    /// Subcommands
69    #[command(subcommand)]
70    pub command: Command,
71}
72
73#[derive(Debug, Clone, Subcommand)]
74pub enum Command {
75    /// Add files to staging area interactively
76    Add(cmd::add::Args),
77    /// Bump version based on Conventional Commits
78    Bump(cmd::bump::Args),
79    /// Check commits in a range for Conventional Commit compliance
80    Check(cmd::check::Args),
81    /// Guide the user through writing a Conventional Commit message
82    Commit(cmd::commit::Args),
83    /// View or edit the current project, global or selected config
84    Config(cmd::config::Args),
85    /// Diagnose and fix common issues
86    Doctor(cmd::doctor::Args),
87    /// Initialize a project config (prefers .cwizard.toml; supports cwizard.toml)
88    Init(cmd::init::Args),
89    /// Push commits to a remote repository
90    Push(cmd::push::Args),
91    /// Create a new Git tag (Semantic Versioning) based on Conventional Commits
92    Tag(cmd::tag::Args),
93}
94
95#[derive(Debug, Clone, Args)]
96pub struct GlobalArgs {
97    /// Increase verbosity (-v, -vv, -vvv); combine with -q to reduce
98    #[arg(short = 'v', long = "verbose", action = ArgAction::Count, global = true)]
99    pub verbose: u8,
100    /// Decrease verbosity (-q, -qq)
101    #[arg(short = 'q', long = "quiet", action = ArgAction::Count, global = true)]
102    pub quiet: u8,
103    /// Output Envelop as JSON instead of human-readable text
104    #[arg(long, global = true)]
105    pub json: bool,
106    /// Output Payload format (json, jsonl, markdown, text)
107    #[arg(long, global = true, default_value_t = FormatArg::Auto, value_enum, conflicts_with = "plain")]
108    pub format: FormatArg,
109    /// Output plain text (equivalent to --format plain)
110    #[arg(long, short = 'p', global = true, conflicts_with = "format")]
111    pub plain: bool,
112    /// Simulate actions without changes
113    #[arg(long, global = true)]
114    pub dry_run: bool,
115    /// Color policy for output
116    #[arg(long, value_enum, default_value_t = ColorMode::Auto, global = true)]
117    pub color: ColorMode,
118    /// Run against another directory (default: current working directory)
119    #[arg(short = 'C', long = "cwd", global = true, default_value = ".")]
120    pub cwd: PathBuf,
121    /// Strict, non-interactive mode (assume yes, no prompts, CI-friendly)
122    #[arg(long, global = true, conflicts_with = "non_interactive")]
123    pub ci: bool,
124    /// Strict, non-interactive mode (assume no, no prompts, CI-friendly)
125    #[arg(long, global = true, conflicts_with = "ci")]
126    pub non_interactive: bool,
127    /// Accept defaults automatically (assume yes, no prompts)
128    #[arg(long, short = 'y', global = true)]
129    pub yes: bool,
130    /// Force actions that would normally be prevented (e.g. pushing to protected branches)
131    #[arg(long, global = true)]
132    pub force: bool,
133    /// Use a specific cwizard config not in the default location
134    #[arg(long, global = true)]
135    pub config: Option<PathBuf>,
136    /// Use a specific registry (local file or remote URL)
137    #[arg(long, global = true)]
138    pub registry: Option<String>,
139    /// Use a specific registry reference (e.g. branch, tag, commit)
140    #[arg(long = "registry-ref", global = true)]
141    pub registry_ref: Option<String>,
142    /// Use a specific registry section (e.g. for multiple registries in one file)
143    #[arg(long = "registry-section", global = true)]
144    pub registry_section: Option<String>,
145}
146
147impl From<GlobalArgs> for AppContextArgs {
148    fn from(args: GlobalArgs) -> Self {
149        Self {
150            verbose: args.verbose,
151            quiet: args.quiet,
152            json: args.json,
153            format: if args.plain {
154                "plain".to_string()
155            } else {
156                args.format.to_output_format_string(args.json)
157            },
158            dry_run: args.dry_run,
159            output_color: args.color.to_string(),
160            cwd: args.cwd,
161            ci: args.ci,
162            non_interactive: args.non_interactive,
163            auto_yes: args.yes,
164            force: args.force,
165            config_path: args.config,
166            registry: args.registry,
167            registry_ref: args.registry_ref,
168            registry_section: args.registry_section,
169        }
170    }
171}