refactor(wt): extract help module

Amolith created

Assisted-by: Claude Opus 4.5 via Amp

Change summary

dist/wt         | 111 +++++++++++++++++++++---------------
src/main.lua    | 144 +-----------------------------------------------
src/wt/help.lua | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 220 insertions(+), 188 deletions(-)

Detailed changes

dist/wt 🔗

@@ -706,55 +706,18 @@ end
 return M
 ]]
 
-
-if _VERSION < "Lua 5.2" then
-	io.stderr:write("error: wt requires Lua 5.2 or later\n")
-	os.exit(1)
-end
+_EMBEDDED_MODULES["wt.help"] = [[-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
+--
+-- SPDX-License-Identifier: GPL-3.0-or-later
 
 local exit = require("wt.exit")
 local EXIT_SUCCESS = exit.EXIT_SUCCESS
-local EXIT_USER_ERROR = exit.EXIT_USER_ERROR
-local EXIT_SYSTEM_ERROR = exit.EXIT_SYSTEM_ERROR
 
-local shell = require("wt.shell")
-local run_cmd = shell.run_cmd
-local run_cmd_silent = shell.run_cmd_silent
-local get_cwd = shell.get_cwd
-local die = shell.die
-
-local path_mod = require("wt.path")
-local branch_to_path = path_mod.branch_to_path
-local split_path = path_mod.split_path
-local relative_path = path_mod.relative_path
-local path_inside = path_mod.path_inside
-local escape_pattern = path_mod.escape_pattern
-
-local git_mod = require("wt.git")
-local find_project_root = git_mod.find_project_root
-local detect_source_worktree = git_mod.detect_source_worktree
-local branch_exists_local = git_mod.branch_exists_local
-local find_branch_remotes = git_mod.find_branch_remotes
-local detect_cloned_default_branch = git_mod.detect_cloned_default_branch
-local get_default_branch = git_mod.get_default_branch
-local parse_branch_remotes = git_mod.parse_branch_remotes
-local parse_worktree_list = git_mod.parse_worktree_list
-local branch_checked_out_at = git_mod.branch_checked_out_at
-
-local config_mod = require("wt.config")
-local resolve_url_template = config_mod.resolve_url_template
-local extract_project_name = config_mod.extract_project_name
-local load_global_config = config_mod.load_global_config
-local load_project_config = config_mod.load_project_config
-
-local hooks_mod = require("wt.hooks")
-local load_hook_permissions = hooks_mod.load_hook_permissions
-local save_hook_permissions = hooks_mod.save_hook_permissions
-local summarize_hooks = hooks_mod.summarize_hooks
-local run_hooks = hooks_mod.run_hooks
+---@class wt.help
+local M = {}
 
 ---Print usage information
-local function print_usage()
+function M.print_usage()
 	print("wt - git worktree manager")
 	print("")
 	print("Usage: wt <command> [options]")
@@ -771,7 +734,7 @@ local function print_usage()
 end
 
 -- Per-command help text (using table.concat for performance)
-local COMMAND_HELP = {
+M.COMMAND_HELP = {
 	c = table.concat({
 		"wt c <url> [--remote name]... [--own]",
 		"",
@@ -885,16 +848,70 @@ local COMMAND_HELP = {
 
 ---Show help for a specific command
 ---@param cmd string
-local function show_command_help(cmd)
-	local help = COMMAND_HELP[cmd]
+function M.show_command_help(cmd)
+	local help = M.COMMAND_HELP[cmd]
 	if help then
 		print(help)
 	else
-		print_usage()
+		M.print_usage()
 	end
 	os.exit(EXIT_SUCCESS)
 end
 
+return M
+]]
+
+
+if _VERSION < "Lua 5.2" then
+	io.stderr:write("error: wt requires Lua 5.2 or later\n")
+	os.exit(1)
+end
+
+local exit = require("wt.exit")
+local EXIT_SUCCESS = exit.EXIT_SUCCESS
+local EXIT_USER_ERROR = exit.EXIT_USER_ERROR
+local EXIT_SYSTEM_ERROR = exit.EXIT_SYSTEM_ERROR
+
+local shell = require("wt.shell")
+local run_cmd = shell.run_cmd
+local run_cmd_silent = shell.run_cmd_silent
+local get_cwd = shell.get_cwd
+local die = shell.die
+
+local path_mod = require("wt.path")
+local branch_to_path = path_mod.branch_to_path
+local split_path = path_mod.split_path
+local relative_path = path_mod.relative_path
+local path_inside = path_mod.path_inside
+local escape_pattern = path_mod.escape_pattern
+
+local git_mod = require("wt.git")
+local find_project_root = git_mod.find_project_root
+local detect_source_worktree = git_mod.detect_source_worktree
+local branch_exists_local = git_mod.branch_exists_local
+local find_branch_remotes = git_mod.find_branch_remotes
+local detect_cloned_default_branch = git_mod.detect_cloned_default_branch
+local get_default_branch = git_mod.get_default_branch
+local parse_branch_remotes = git_mod.parse_branch_remotes
+local parse_worktree_list = git_mod.parse_worktree_list
+local branch_checked_out_at = git_mod.branch_checked_out_at
+
+local config_mod = require("wt.config")
+local resolve_url_template = config_mod.resolve_url_template
+local extract_project_name = config_mod.extract_project_name
+local load_global_config = config_mod.load_global_config
+local load_project_config = config_mod.load_project_config
+
+local hooks_mod = require("wt.hooks")
+local load_hook_permissions = hooks_mod.load_hook_permissions
+local save_hook_permissions = hooks_mod.save_hook_permissions
+local summarize_hooks = hooks_mod.summarize_hooks
+local run_hooks = hooks_mod.run_hooks
+
+local help_mod = require("wt.help")
+local print_usage = help_mod.print_usage
+local show_command_help = help_mod.show_command_help
+
 ---@param args string[]
 local function cmd_clone(args)
 	-- Parse arguments: <url> [--remote name]... [--own]

src/main.lua 🔗

@@ -50,147 +50,9 @@ local save_hook_permissions = hooks_mod.save_hook_permissions
 local summarize_hooks = hooks_mod.summarize_hooks
 local run_hooks = hooks_mod.run_hooks
 
----Print usage information
-local function print_usage()
-	print("wt - git worktree manager")
-	print("")
-	print("Usage: wt <command> [options]")
-	print("")
-	print("Commands:")
-	print("  c <url> [--remote name]... [--own]   Clone into bare worktree structure")
-	print("  n <project-name> [--remote name]...  Initialize fresh project")
-	print("  a <branch> [-b [<start-point>]]      Add worktree with optional hooks")
-	print("  r <branch> [-b] [-f]                 Remove worktree, optionally delete branch")
-	print("  l                                    List worktrees with status")
-	print("  f                                    Fetch all remotes")
-	print("  init [--dry-run] [-y]                Convert existing repo to bare structure")
-	print("  help                                 Show this help message")
-end
-
--- Per-command help text (using table.concat for performance)
-local COMMAND_HELP = {
-	c = table.concat({
-		"wt c <url> [--remote name]... [--own]",
-		"",
-		"Clone a repository into bare worktree structure.",
-		"",
-		"Arguments:",
-		"  <url>              Git URL to clone",
-		"",
-		"Options:",
-		"  --remote <name>    Add configured remote from ~/.config/wt/config.lua",
-		"                     Can be specified multiple times",
-		"  --own              Treat as your own project: first remote becomes 'origin'",
-		"                     (default: 'origin' renamed to 'upstream', your remotes added)",
-		"",
-		"Examples:",
-		"  wt c https://github.com/user/repo.git",
-		"  wt c git@github.com:user/repo.git --remote github --own",
-	}, "\n"),
-
-	n = table.concat({
-		"wt n <project-name> [--remote name]...",
-		"",
-		"Initialize a fresh project with bare worktree structure.",
-		"",
-		"Arguments:",
-		"  <project-name>     Name of the new project directory",
-		"",
-		"Options:",
-		"  --remote <name>    Add configured remote from ~/.config/wt/config.lua",
-		"                     Can be specified multiple times",
-		"",
-		"Examples:",
-		"  wt n my-project",
-		"  wt n my-project --remote github --remote gitlab",
-	}, "\n"),
-
-	a = table.concat({
-		"wt a <branch> [-b [<start-point>]]",
-		"",
-		"Add a worktree for a branch.",
-		"",
-		"Arguments:",
-		"  <branch>           Branch name to checkout or create",
-		"",
-		"Options:",
-		"  -b                 Create a new branch",
-		"  <start-point>      Base commit/branch for new branch (only with -b)",
-		"",
-		"If run from inside an existing worktree, hooks from .wt.lua will be applied.",
-		"",
-		"Examples:",
-		"  wt a main                    # Checkout existing branch",
-		"  wt a feature/new -b          # Create new branch from HEAD",
-		"  wt a feature/new -b main     # Create new branch from main",
-	}, "\n"),
-
-	r = table.concat({
-		"wt r <branch> [-b] [-f]",
-		"",
-		"Remove a worktree.",
-		"",
-		"Arguments:",
-		"  <branch>           Branch name of worktree to remove",
-		"",
-		"Options:",
-		"  -b                 Also delete the branch after removing worktree",
-		"  -f                 Force removal even with uncommitted changes",
-		"",
-		"Examples:",
-		"  wt r feature/old             # Remove worktree, keep branch",
-		"  wt r feature/old -b          # Remove worktree and delete branch",
-		"  wt r feature/old -f          # Force remove with uncommitted changes",
-	}, "\n"),
-
-	l = table.concat({
-		"wt l",
-		"",
-		"List all worktrees with status information.",
-		"",
-		"Displays a table showing:",
-		"  - Branch name",
-		"  - Relative path from project root",
-		"  - Commit status (ahead/behind remote)",
-		"  - Working tree status (clean/dirty)",
-	}, "\n"),
-
-	f = table.concat({
-		"wt f",
-		"",
-		"Fetch from all configured remotes.",
-		"",
-		"Runs 'git fetch --all' in the bare repository.",
-	}, "\n"),
-
-	init = table.concat({
-		"wt init [--dry-run] [-y]",
-		"",
-		"Convert an existing git repository to bare worktree structure.",
-		"",
-		"Options:",
-		"  --dry-run          Show what would be done without making changes",
-		"  -y                 Skip confirmation prompt",
-		"",
-		"This command:",
-		"  1. Moves .git/ to .bare/",
-		"  2. Creates .git file pointing to .bare/",
-		"  3. Creates a worktree for the current branch",
-		"  4. Removes orphaned files from project root",
-	}, "\n"),
-}
-
----Show help for a specific command
----@param cmd string
-local function show_command_help(cmd)
-	local help = COMMAND_HELP[cmd]
-	if help then
-		print(help)
-	else
-		print_usage()
-	end
-	os.exit(EXIT_SUCCESS)
-end
+local help_mod = require("wt.help")
+local print_usage = help_mod.print_usage
+local show_command_help = help_mod.show_command_help
 
 ---@param args string[]
 local function cmd_clone(args)

src/wt/help.lua 🔗

@@ -0,0 +1,153 @@
+-- SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
+--
+-- SPDX-License-Identifier: GPL-3.0-or-later
+
+local exit = require("wt.exit")
+local EXIT_SUCCESS = exit.EXIT_SUCCESS
+
+---@class wt.help
+local M = {}
+
+---Print usage information
+function M.print_usage()
+	print("wt - git worktree manager")
+	print("")
+	print("Usage: wt <command> [options]")
+	print("")
+	print("Commands:")
+	print("  c <url> [--remote name]... [--own]   Clone into bare worktree structure")
+	print("  n <project-name> [--remote name]...  Initialize fresh project")
+	print("  a <branch> [-b [<start-point>]]      Add worktree with optional hooks")
+	print("  r <branch> [-b] [-f]                 Remove worktree, optionally delete branch")
+	print("  l                                    List worktrees with status")
+	print("  f                                    Fetch all remotes")
+	print("  init [--dry-run] [-y]                Convert existing repo to bare structure")
+	print("  help                                 Show this help message")
+end
+
+-- Per-command help text (using table.concat for performance)
+M.COMMAND_HELP = {
+	c = table.concat({
+		"wt c <url> [--remote name]... [--own]",
+		"",
+		"Clone a repository into bare worktree structure.",
+		"",
+		"Arguments:",
+		"  <url>              Git URL to clone",
+		"",
+		"Options:",
+		"  --remote <name>    Add configured remote from ~/.config/wt/config.lua",
+		"                     Can be specified multiple times",
+		"  --own              Treat as your own project: first remote becomes 'origin'",
+		"                     (default: 'origin' renamed to 'upstream', your remotes added)",
+		"",
+		"Examples:",
+		"  wt c https://github.com/user/repo.git",
+		"  wt c git@github.com:user/repo.git --remote github --own",
+	}, "\n"),
+
+	n = table.concat({
+		"wt n <project-name> [--remote name]...",
+		"",
+		"Initialize a fresh project with bare worktree structure.",
+		"",
+		"Arguments:",
+		"  <project-name>     Name of the new project directory",
+		"",
+		"Options:",
+		"  --remote <name>    Add configured remote from ~/.config/wt/config.lua",
+		"                     Can be specified multiple times",
+		"",
+		"Examples:",
+		"  wt n my-project",
+		"  wt n my-project --remote github --remote gitlab",
+	}, "\n"),
+
+	a = table.concat({
+		"wt a <branch> [-b [<start-point>]]",
+		"",
+		"Add a worktree for a branch.",
+		"",
+		"Arguments:",
+		"  <branch>           Branch name to checkout or create",
+		"",
+		"Options:",
+		"  -b                 Create a new branch",
+		"  <start-point>      Base commit/branch for new branch (only with -b)",
+		"",
+		"If run from inside an existing worktree, hooks from .wt.lua will be applied.",
+		"",
+		"Examples:",
+		"  wt a main                    # Checkout existing branch",
+		"  wt a feature/new -b          # Create new branch from HEAD",
+		"  wt a feature/new -b main     # Create new branch from main",
+	}, "\n"),
+
+	r = table.concat({
+		"wt r <branch> [-b] [-f]",
+		"",
+		"Remove a worktree.",
+		"",
+		"Arguments:",
+		"  <branch>           Branch name of worktree to remove",
+		"",
+		"Options:",
+		"  -b                 Also delete the branch after removing worktree",
+		"  -f                 Force removal even with uncommitted changes",
+		"",
+		"Examples:",
+		"  wt r feature/old             # Remove worktree, keep branch",
+		"  wt r feature/old -b          # Remove worktree and delete branch",
+		"  wt r feature/old -f          # Force remove with uncommitted changes",
+	}, "\n"),
+
+	l = table.concat({
+		"wt l",
+		"",
+		"List all worktrees with status information.",
+		"",
+		"Displays a table showing:",
+		"  - Branch name",
+		"  - Relative path from project root",
+		"  - Commit status (ahead/behind remote)",
+		"  - Working tree status (clean/dirty)",
+	}, "\n"),
+
+	f = table.concat({
+		"wt f",
+		"",
+		"Fetch from all configured remotes.",
+		"",
+		"Runs 'git fetch --all' in the bare repository.",
+	}, "\n"),
+
+	init = table.concat({
+		"wt init [--dry-run] [-y]",
+		"",
+		"Convert an existing git repository to bare worktree structure.",
+		"",
+		"Options:",
+		"  --dry-run          Show what would be done without making changes",
+		"  -y                 Skip confirmation prompt",
+		"",
+		"This command:",
+		"  1. Moves .git/ to .bare/",
+		"  2. Creates .git file pointing to .bare/",
+		"  3. Creates a worktree for the current branch",
+		"  4. Removes orphaned files from project root",
+	}, "\n"),
+}
+
+---Show help for a specific command
+---@param cmd string
+function M.show_command_help(cmd)
+	local help = M.COMMAND_HELP[cmd]
+	if help then
+		print(help)
+	else
+		M.print_usage()
+	end
+	os.exit(EXIT_SUCCESS)
+end
+
+return M