use std::path::PathBuf;
use std::str::FromStr;
use clap::ColorChoice;
use clap::Parser;
use clap::builder::Styles;
use clap::builder::styling::AnsiColor;
use clap::builder::styling::Effects;
use mago_php_version::PHPVersion;
use crate::commands::analyze::AnalyzeCommand;
use crate::commands::config::ConfigCommand;
use crate::commands::cst::CstCommand;
use crate::commands::extension::ExtensionCommand;
use crate::commands::format::FormatCommand;
use crate::commands::generate_completions::GenerateCompletionsCommand;
use crate::commands::guard::GuardCommand;
use crate::commands::init::InitCommand;
use crate::commands::inspect_baseline::InspectBaselineCommand;
use crate::commands::lint::LintCommand;
use crate::commands::list_files::ListFilesCommand;
use crate::commands::self_update::SelfUpdateCommand;
use crate::error::Error;
mod args;
pub mod analyze;
pub mod config;
pub mod cst;
pub mod extension;
pub mod format;
pub mod generate_completions;
pub mod guard;
pub mod init;
pub mod inspect_baseline;
pub mod lint;
pub mod list_files;
pub mod self_update;
pub mod stdin_input;
pub const CLAP_STYLING: Styles = Styles::styled()
.header(AnsiColor::Green.on_default().effects(Effects::BOLD))
.usage(AnsiColor::Green.on_default().effects(Effects::BOLD))
.literal(AnsiColor::Cyan.on_default().effects(Effects::BOLD))
.placeholder(AnsiColor::Cyan.on_default())
.error(AnsiColor::Red.on_default().effects(Effects::BOLD))
.valid(AnsiColor::Cyan.on_default().effects(Effects::BOLD))
.invalid(AnsiColor::Yellow.on_default().effects(Effects::BOLD));
#[derive(Parser, Debug)]
pub enum MagoCommand {
#[command(name = "init")]
Init(InitCommand),
#[command(name = "config")]
Config(ConfigCommand),
#[command(name = "extension")]
Extension(ExtensionCommand),
#[command(name = "list-files")]
ListFiles(ListFilesCommand),
#[command(name = "cst")]
Cst(CstCommand),
#[command(name = "lint")]
Lint(LintCommand),
#[command(name = "analyze")]
Analyze(AnalyzeCommand),
#[command(name = "guard")]
Guard(GuardCommand),
#[command(name = "inspect-baseline")]
InspectBaseline(InspectBaselineCommand),
#[command(name = "format")]
Format(FormatCommand),
#[command(name = "self-update")]
SelfUpdate(SelfUpdateCommand),
#[command(name = "generate-completions")]
GenerateCompletions(GenerateCompletionsCommand),
#[command(name = "version")]
Version,
}
#[derive(Parser, Debug)]
#[command(
version,
author,
styles = CLAP_STYLING,
about = "Mago: The powerful PHP toolchain. Lint, format, and analyze your code with ease.",
long_about = r#"
Welcome to Mago!
Mago is a powerful and versatile toolchain for PHP developers, designed to help you write better code, faster.
Features:
* **Linting:** Identify and fix code style issues and potential bugs.
* **Formatting:** Format your code consistently and automatically.
* **Analyzing:** Analyze your code for structure, complexity, and dependencies.
* **CST Inspection:** Dive deep into the structure of your PHP code with Concrete Syntax Tree (CST) visualization.
Get started by exploring the commands below!
"#
)]
pub struct CliArguments {
#[arg(long)]
pub workspace: Option<PathBuf>,
#[arg(long)]
pub config: Option<PathBuf>,
#[arg(long)]
pub php_version: Option<String>,
#[arg(long)]
pub threads: Option<usize>,
#[arg(long, default_value_t = false)]
pub allow_unsupported_php_version: bool,
#[arg(long, default_value_t = false)]
pub no_version_check: bool,
#[arg(long, global = true, default_value_t = false)]
pub no_extensions: bool,
#[arg(long, default_value_t = ColorChoice::Auto)]
pub colors: ColorChoice,
#[clap(subcommand)]
pub command: MagoCommand,
}
impl CliArguments {
pub fn get_php_version(&self) -> Result<Option<PHPVersion>, Error> {
let Some(version) = &self.php_version else {
return Ok(None);
};
match PHPVersion::from_str(version) {
Ok(version) => Ok(Some(version)),
Err(error) => Err(Error::InvalidPHPVersion(version.clone(), error)),
}
}
}