|
| 1 | +//! CLI argument parsing for `vp-setup`. |
| 2 | +
|
| 3 | +use clap::Parser; |
| 4 | + |
| 5 | +/// Vite+ Installer — standalone installer for the vp CLI. |
| 6 | +#[derive(Parser, Debug)] |
| 7 | +#[command(name = "vp-setup", about = "Install the Vite+ CLI")] |
| 8 | +struct Cli { |
| 9 | + /// Accept defaults without prompting (for CI/unattended installs) |
| 10 | + #[arg(short = 'y', long = "yes")] |
| 11 | + yes: bool, |
| 12 | + |
| 13 | + /// Suppress all output except errors |
| 14 | + #[arg(short = 'q', long = "quiet")] |
| 15 | + quiet: bool, |
| 16 | + |
| 17 | + /// Install a specific version (default: latest) |
| 18 | + #[arg(long = "version")] |
| 19 | + version: Option<String>, |
| 20 | + |
| 21 | + /// npm dist-tag to install (default: latest) |
| 22 | + #[arg(long = "tag", default_value = "latest")] |
| 23 | + tag: String, |
| 24 | + |
| 25 | + /// Custom installation directory (default: ~/.vite-plus) |
| 26 | + #[arg(long = "install-dir")] |
| 27 | + install_dir: Option<String>, |
| 28 | + |
| 29 | + /// Custom npm registry URL |
| 30 | + #[arg(long = "registry")] |
| 31 | + registry: Option<String>, |
| 32 | + |
| 33 | + /// Skip Node.js version manager setup |
| 34 | + #[arg(long = "no-node-manager")] |
| 35 | + no_node_manager: bool, |
| 36 | + |
| 37 | + /// Do not modify the User PATH |
| 38 | + #[arg(long = "no-modify-path")] |
| 39 | + no_modify_path: bool, |
| 40 | +} |
| 41 | + |
| 42 | +/// Parsed installation options. |
| 43 | +pub struct Options { |
| 44 | + pub yes: bool, |
| 45 | + pub quiet: bool, |
| 46 | + pub version: Option<String>, |
| 47 | + pub tag: String, |
| 48 | + pub install_dir: Option<String>, |
| 49 | + pub registry: Option<String>, |
| 50 | + pub no_node_manager: bool, |
| 51 | + pub no_modify_path: bool, |
| 52 | +} |
| 53 | + |
| 54 | +/// Parse CLI arguments, merging with environment variables. |
| 55 | +/// |
| 56 | +/// CLI flags take precedence over environment variables. |
| 57 | +pub fn parse() -> Options { |
| 58 | + let cli = Cli::parse(); |
| 59 | + |
| 60 | + // Environment variable overrides (CLI flags take precedence) |
| 61 | + let version = cli.version.or_else(|| std::env::var("VP_VERSION").ok()); |
| 62 | + let install_dir = cli.install_dir.or_else(|| std::env::var("VP_HOME").ok()); |
| 63 | + let registry = cli.registry.or_else(|| std::env::var("NPM_CONFIG_REGISTRY").ok()); |
| 64 | + |
| 65 | + let no_node_manager = cli.no_node_manager |
| 66 | + || std::env::var("VP_NODE_MANAGER") |
| 67 | + .ok() |
| 68 | + .is_some_and(|v| v.eq_ignore_ascii_case("no")); |
| 69 | + |
| 70 | + // quiet implies yes |
| 71 | + let yes = cli.yes || cli.quiet; |
| 72 | + |
| 73 | + Options { |
| 74 | + yes, |
| 75 | + quiet: cli.quiet, |
| 76 | + version, |
| 77 | + tag: cli.tag, |
| 78 | + install_dir, |
| 79 | + registry, |
| 80 | + no_node_manager, |
| 81 | + no_modify_path: cli.no_modify_path, |
| 82 | + } |
| 83 | +} |
0 commit comments