worm_hole 2.0.0

CLI tool to easily jump between directories
// Copyright (C) 2025 Rignchen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::commands;
use clap::{CommandFactory, FromArgMatches, Parser};

structstruck::strike! {
	/// Worm hole is a simple CLI tool to easily navigate between directories.
	///
	/// With Worm Hole you can set a name for a directory and then use that name to move to that
	/// directory from anywhere.
	#[strikethrough[derive(Parser, Debug)]]
	#[clap(verbatim_doc_comment)]
	#[command(version)]
	#[command(name = "worm_hole", about = "A simple CLI tool to easily navigate between directories.")]
	pub struct Args {
		#[clap(subcommand)]
		pub cmd: pub enum Command {
			/// Move to the given location alias
			Query(commands::Query),
			/// Add a location alias to the list
			#[clap(name = "add")]
			AddAlias(commands::AddAlias),
			/// Remove a location alias from the list
			#[clap(name = "rm")]
			RemoveAlias(commands::RemoveAlias),
			/// List all location aliases
			#[clap(name = "ls")]
			ListAliases(commands::ListAliases),
			/// Search for the aliases which have a path maching a pattern
			#[clap(name = "search")]
			SearchAliases(commands::SearchAliases),
			/// Edit the location of an alias
			#[clap(name = "edit")]
			EditAlias(commands::EditAlias),
			/// Change the name of an alias
			#[clap(name = "rename")]
			RenameAlias(commands::RenameAlias),
			/// Initialize the database and the bash config to make wormhole work
			Init(commands::Init),
		},
		/// The path to the sqlite database file
		#[clap(long, default_value_t = format!("{}/.wormhole.db", std::env::var("HOME").unwrap()))]
		pub db_path: String,
	}
}

impl Args {
	pub fn parse_args() -> Self {
		let mut command = Args::command();
		let command_clone = command.clone();
		command = clap_autocomplete::add_subcommand(command);
		let matches = command.get_matches();
		if let Some(result) = clap_autocomplete::test_subcommand(&matches, command_clone) {
			if let Err(err) = result {
				eprintln!("Error: {}", err);
				std::process::exit(1);
			} else {
				std::process::exit(0);
			}
		}
		Self::from_arg_matches(&matches).unwrap()
	}
}