use clap::{CommandFactory, Parser, Subcommand, ValueHint};
use worktree::commands::init::Shell;
use worktree::commands::{back, cleanup, create, init, jump, list, remove, status, sync_config};
use worktree::Result;
#[derive(Parser)]
#[command(name = "worktree")]
#[command(about = "A CLI tool for managing git worktrees with enhanced features")]
pub struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Create {
#[arg(value_hint = ValueHint::Other)]
branch: String,
#[arg(long, conflicts_with = "existing_branch")]
new_branch: bool,
#[arg(long, conflicts_with = "new_branch")]
existing_branch: bool,
},
List {
#[arg(long)]
current: bool,
},
Remove {
#[arg(value_hint = ValueHint::Other)]
target: String,
#[arg(long)]
keep_branch: bool,
},
Status,
SyncConfig {
#[arg(value_hint = ValueHint::Other)]
from: String,
#[arg(value_hint = ValueHint::Other)]
to: String,
},
Init {
#[arg(value_enum)]
shell: Shell,
},
Completions {
#[arg(value_enum)]
shell: Shell,
},
Jump {
#[arg(value_hint = ValueHint::Other)]
target: Option<String>,
#[arg(long)]
interactive: bool,
#[arg(long, hide = true)]
list_completions: bool,
#[arg(long)]
current: bool,
},
Cleanup,
Back,
}
fn main() -> Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Create {
branch,
new_branch,
existing_branch,
} => {
let mode = if new_branch {
create::CreateMode::NewBranch
} else if existing_branch {
create::CreateMode::ExistingBranch
} else {
create::CreateMode::Smart
};
create::create_worktree(&branch, mode)?;
}
Commands::List { current } => {
list::list_worktrees(current)?;
}
Commands::Remove {
target,
keep_branch,
} => {
remove::remove_worktree(&target, !keep_branch)?;
}
Commands::Status => {
status::show_status()?;
}
Commands::SyncConfig { from, to } => {
sync_config::sync_config(&from, &to)?;
}
Commands::Init { shell } => {
init::generate_shell_integration(shell);
}
Commands::Jump {
target,
interactive,
list_completions,
current,
} => {
jump::jump_worktree(target.as_deref(), interactive, list_completions, current)?;
}
Commands::Completions { shell } => {
let mut cmd = Cli::command();
init::generate_completions(shell, &mut cmd);
}
Commands::Cleanup => {
cleanup::cleanup_worktrees()?;
}
Commands::Back => {
back::back_to_origin()?;
}
}
Ok(())
}