Private/Help.ps1
|
Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' # Help and version text, mirroring zoxide 0.10.0 src/cmd/cmd.rs: the HelpTemplate struct (used by # the root command and the six subcommands) and clap's default template (used by the hidden edit # subcommands and the import sources). The text matches tests/fixtures/help_*.txt byte for byte, # where '{bin}' is the program-name token clap prints in usage lines. $script:ZoVersionNumber = '0.10.0' $script:ZoHelpAuthor = @( "Ajeet D'Souza <98ajeet@gmail.com>" 'https://github.com/ajeetdsouza/zoxide' ) $script:ZoHelpEnvironment = @( 'Environment variables:' ' _ZO_DATA_DIR Path for zoxide data files' ' _ZO_ECHO Print the matched directory before navigating to it when set to 1' ' _ZO_EXCLUDE_DIRS List of directory globs to be excluded' ' _ZO_FZF_OPTS Custom flags to pass to fzf' ' _ZO_MAXAGE Maximum total age after which entries start getting deleted' ' _ZO_RESOLVE_SYMLINKS Resolve symlinks when storing paths' ) $script:ZoHelpOptionsTail = @( ' -h, --help Print help' ' -V, --version Print version' ) # Per command: the about line, the usage, and the {all-args} block. $script:ZoHelpPages = @{ root = @{ About = 'A smarter cd command for your terminal' Usage = '{bin} <COMMAND>' Args = @( 'Commands:' ' add Add a new directory or increment its rank' ' edit Edit the database' ' import Import entries from another application' ' init Generate shell configuration' ' query Search for a directory in the database' ' remove Remove a directory from the database' '' 'Options:' ) + $script:ZoHelpOptionsTail } add = @{ About = 'Add a new directory or increment its rank' Usage = '{bin} add [OPTIONS] <PATHS>...' Args = @( 'Arguments:' ' <PATHS>... ' '' 'Options:' " -s, --score <SCORE> The rank to increment the entry if it exists or initialize it with if it doesn't" ' -h, --help Print help' ' -V, --version Print version' ) } edit = @{ About = 'Edit the database' Usage = '{bin} edit' Args = @('Options:') + $script:ZoHelpOptionsTail } import = @{ About = 'Import entries from another application' Usage = '{bin} import [OPTIONS] <COMMAND>' Args = @( 'Commands:' ' atuin Import from atuin' ' autojump Import from autojump' ' fasd Import from fasd' ' z Import from z' ' z.lua Import from z.lua' ' zsh-z Import from zsh-z' '' 'Options:' ' --merge Merge into existing database' ) + $script:ZoHelpOptionsTail } init = @{ About = 'Generate shell configuration' Usage = '{bin} init [OPTIONS] <SHELL>' Args = @( 'Arguments:' ' <SHELL> [possible values: bash, elvish, fish, nushell, posix, powershell, tcsh, xonsh, zsh]' '' 'Options:' ' --no-cmd Prevents zoxide from defining the `z` and `zi` commands' ' --cmd <CMD> Changes the prefix of the `z` and `zi` commands [default: z]' " --hook <HOOK> Changes how often zoxide increments a directory's score [default: pwd] [possible values: none, prompt, pwd]" ' -h, --help Print help' ' -V, --version Print version' ) } query = @{ About = 'Search for a directory in the database' Usage = '{bin} query [OPTIONS] [KEYWORDS]...' Args = @( 'Arguments:' ' [KEYWORDS]... ' '' 'Options:' ' -a, --all Show unavailable directories' ' -i, --interactive Use interactive selection' ' -l, --list List all matching directories' ' -s, --score Print score with results' ' --exclude <path> Exclude the current directory' ' --base-dir <path> Only search within this directory' ' -h, --help Print help' ' -V, --version Print version' ) } remove = @{ About = 'Remove a directory from the database' Usage = '{bin} remove [PATHS]...' Args = @( 'Arguments:' ' [PATHS]... ' '' 'Options:' ) + $script:ZoHelpOptionsTail } } function Get-ZoBinName { <# .SYNOPSIS The program-name token clap prints in usage lines: 'zoxide.exe' on Windows, 'zoxide' elsewhere. .DESCRIPTION clap takes the name from the executable file, so the Windows binary prints 'zoxide.exe' (as the fixtures show) and the Linux and macOS binaries print 'zoxide'. The module prints what the binary prints on the same platform. .PARAMETER Windows Use the Windows name. Defaults to the host platform. #> [CmdletBinding()] [OutputType([string])] param( [bool]$Windows = $IsWindows ) if ($Windows) { 'zoxide.exe' } else { 'zoxide' } } function Get-ZoVersion { <# .SYNOPSIS The version line clap prints for -V/--version. .DESCRIPTION Mirrors clap's `version` with propagate_version: the command's display name (the names from the root down, joined with '-') and 0.10.0, for example 'zoxide 0.10.0' or 'zoxide-import-z.lua 0.10.0'. .PARAMETER Command The subcommand path below the root, space-separated ('query', 'edit delete'); empty for the root. #> [CmdletBinding()] [OutputType([string])] param( [AllowEmptyString()] [string]$Command = '' ) $names = @('zoxide') + @($Command.Split(' ', [StringSplitOptions]::RemoveEmptyEntries)) "$($names -join '-') $($script:ZoVersionNumber)" } function Get-ZoHelp { <# .SYNOPSIS The help text clap prints for -h/--help, without the final newline. .DESCRIPTION Mirrors the HelpTemplate in cmd/cmd.rs for 'root', 'add', 'edit', 'import', 'init', 'query' and 'remove': name and version, author, homepage, about, usage, the argument blocks and the environment variables. The nested commands 'edit decrement|delete|increment|reload' and 'import atuin|autojump|fasd|z|z.lua|zsh-z' use clap's default template instead, as the binary does. Lines are joined with LF; the caller adds the final newline. .PARAMETER Command 'root', a subcommand name, or a nested path such as 'edit delete' or 'import z'. #> [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory)] [string]$Command ) $bin = Get-ZoBinName $lines = if ($script:ZoHelpPages.ContainsKey($Command)) { $page = $script:ZoHelpPages[$Command] $versionPath = if ($Command -eq 'root') { '' } else { $Command } @(Get-ZoVersion -Command $versionPath) + $script:ZoHelpAuthor + @('', $page.About, '', 'Usage:', " $($page.Usage)", '') + $page.Args + @('') + $script:ZoHelpEnvironment } elseif ($Command -match '^edit (decrement|delete|increment)$') { @("Usage: {bin} $Command <PATH>", '', 'Arguments:', ' <PATH> ', '', 'Options:') + $script:ZoHelpOptionsTail } elseif ($Command -eq 'edit reload') { @("Usage: {bin} $Command", '', 'Options:') + $script:ZoHelpOptionsTail } elseif ($Command -match '^import (atuin|autojump|fasd|z|z\.lua|zsh-z)$') { @("Import from $($Matches[1])", '', "Usage: {bin} $Command [OPTIONS]", '', 'Options:', ' --merge Merge into existing database') + $script:ZoHelpOptionsTail } else { throw "no help for command: $Command" } ($lines -join "`n").Replace('{bin}', $bin) } |