diff --git a/.github/workflows/pr-bash-examples-tests.yml b/.github/workflows/pr-bash-examples-tests.yml index 6cfc8f8..f7e8f3f 100644 --- a/.github/workflows/pr-bash-examples-tests.yml +++ b/.github/workflows/pr-bash-examples-tests.yml @@ -44,6 +44,10 @@ jobs: version-injector dist/script.sh --style sh --version "${{ env.STAMPED_VERSION }}" version-injector dist/script.ps1 --style ps1 --version "${{ env.STAMPED_VERSION }}" + - name: Create script alias for Leia examples + shell: bash + run: ln -sf script.sh dist/script + - name: Put prepared script on PATH shell: bash run: echo "$PWD/dist" >> "$GITHUB_PATH" diff --git a/.github/workflows/release-tests.yml b/.github/workflows/release-tests.yml index 4671fb1..7c7d10e 100644 --- a/.github/workflows/release-tests.yml +++ b/.github/workflows/release-tests.yml @@ -43,6 +43,8 @@ jobs: actual="$(bash ./dist/script.sh --version)" printf 'actual=%s\n' "$actual" test "$actual" = "${{ env.STAMPED_VERSION }}" + help_output="$(bash ./dist/script.sh --help)" + printf '%s\n' "$help_output" | grep -E 'Usage: .*script\.sh ' release-powershell: runs-on: windows-2025 @@ -77,3 +79,7 @@ jobs: if ($version -ne "${{ env.STAMPED_VERSION }}") { throw "expected version ${{ env.STAMPED_VERSION }}, got $version" } + $help = & ./dist/script.ps1 -Help + if (-not ($help | Select-String -Pattern 'Usage: .*script\.ps1 ' -Quiet)) { + throw 'expected help output to mention script.ps1' + } diff --git a/examples/cli-contract-bash/README.md b/examples/cli-contract-bash/README.md index c7dca48..c2be359 100644 --- a/examples/cli-contract-bash/README.md +++ b/examples/cli-contract-bash/README.md @@ -1,17 +1,20 @@ # Script Bash CLI Contract Example -This example keeps coverage on the Bash-facing contract of `script.sh`: help output and version +This example keeps coverage on the Bash-facing contract of `script`: help output and version output. ## Testing ```bash # should show the debug flag in help output -script.sh --help | grep -- '--debug' +script --help | grep -- '--debug' # should show the version flag in help output -script.sh --help | grep -- '--version' +script --help | grep -- '--version' + +# should show the invoked command name in usage output +script --help | grep -E 'Usage: .*script ' # should print a version string -test -n "$(script.sh --version)" +test -n "$(script --version)" ``` diff --git a/examples/cli-contract-powershell/README.md b/examples/cli-contract-powershell/README.md index 94a102a..21a2fc4 100644 --- a/examples/cli-contract-powershell/README.md +++ b/examples/cli-contract-powershell/README.md @@ -1,18 +1,21 @@ # Script PowerShell CLI Contract Example -This example keeps coverage on the PowerShell-facing contract of `script.ps1`: help output and +This example keeps coverage on the PowerShell-facing contract of `script`: help output and version output. ## Testing ```powershell # should show the debug flag in help output -& script.ps1 -Help | Select-String -Pattern '-Debug' +script -Help | Select-String -Pattern '-Debug' # should show the version flag in help output -& script.ps1 -Help | Select-String -Pattern '-Version' +script -Help | Select-String -Pattern '-Version' + +# should show the invoked command name in usage output +script -Help | Select-String -Pattern 'Usage: .*script ' # should print a version string -$version = & script.ps1 -Version +$version = script -Version if ([string]::IsNullOrWhiteSpace($version)) { throw 'expected version output' } ``` diff --git a/examples/minimal-bash/README.md b/examples/minimal-bash/README.md index 41d246e..e2dc491 100644 --- a/examples/minimal-bash/README.md +++ b/examples/minimal-bash/README.md @@ -1,20 +1,20 @@ # Script Bash Minimal Example -This example is the smallest markdown-first smoke test for `script.sh`. It runs the default +This example is the smallest markdown-first smoke test for `script`. It runs the default no-argument flow and verifies the placeholder output from the prepared `dist/script.sh` artifact. ## Setup ```bash # should run the default placeholder flow -script.sh > run.log 2>&1 +script > run.log 2>&1 ``` ## Testing ```bash # should print the placeholder execution message -grep -F 'Replace the body of script.sh with your project logic.' run.log +grep -F 'Replace the body of script with your project logic.' run.log ``` ## Destroy tests diff --git a/examples/minimal-powershell/README.md b/examples/minimal-powershell/README.md index e77c703..8078d67 100644 --- a/examples/minimal-powershell/README.md +++ b/examples/minimal-powershell/README.md @@ -1,20 +1,20 @@ # Script PowerShell Minimal Example -This example is the smallest markdown-first smoke test for `script.ps1`. It runs the default +This example is the smallest markdown-first smoke test for `script`. It runs the default no-argument flow and verifies the placeholder output from the prepared `dist/script.ps1` artifact. ## Setup ```powershell # should run the default placeholder flow -script.ps1 | Set-Content -Path run.log +script | Set-Content -Path run.log ``` ## Testing ```powershell # should print the placeholder execution message -Get-Content run.log | Select-String -Pattern 'Replace the body of script.ps1 with your project logic.' +Get-Content run.log | Select-String -Pattern 'Replace the body of script with your project logic.' ``` ## Destroy tests diff --git a/script.ps1 b/script.ps1 index 0df4574..bdc26bb 100644 --- a/script.ps1 +++ b/script.ps1 @@ -29,7 +29,19 @@ param( Set-StrictMode -Version 3 $ErrorActionPreference = 'Stop' -$CLI_NAME = if ($PSCommandPath) { Split-Path -Leaf $PSCommandPath } else { $MyInvocation.MyCommand.Name } +$CLI_NAME = if ( + -not [string]::IsNullOrWhiteSpace($MyInvocation.InvocationName) -and + $MyInvocation.InvocationName -notin @('.', '&') -and + $MyInvocation.InvocationName -notmatch '^(?:pwsh|powershell)(?:\.exe)?$' +) { + Split-Path -Leaf $MyInvocation.InvocationName +} elseif ($PSCommandPath) { + Split-Path -Leaf $PSCommandPath +} elseif ($MyInvocation.MyCommand.Name) { + $MyInvocation.MyCommand.Name +} else { + 'script.ps1' +} # Keep a single top-level assignment so release automation can stamp the entrypoint in place. $SCRIPT_VERSION = if (-not [string]::IsNullOrWhiteSpace($env:SCRIPT_VERSION)) { $env:SCRIPT_VERSION } else { try { $resolved = (& git describe --tags --always --abbrev=1 2>$null | Out-String).Trim(); if ($LASTEXITCODE -eq 0 -and -not [string]::IsNullOrWhiteSpace($resolved)) { $resolved } else { '0.0.0-dev' } } catch { '0.0.0-dev' } } $ESCAPE = [char]27 diff --git a/script.sh b/script.sh index 62ce260..6c9cec5 100755 --- a/script.sh +++ b/script.sh @@ -40,7 +40,15 @@ tty_reset="$(tty_escape 0)" tty_tp="$(tty_escape '38;2;0;200;138')" tty_ts="$(tty_escape '38;2;219;39;119')" -CLI_NAME="${0##*/}" +CLI_NAME_SOURCE="${BASH_SOURCE[0]:-${0}}" +CLI_NAME="${CLI_NAME_SOURCE##*/}" + +case "${CLI_NAME}" in + '' | stdin | bash | -bash | sh | -sh) + CLI_NAME="script.sh" + ;; +esac + # Keep a single top-level assignment so release automation can stamp the entrypoint in place. SCRIPT_VERSION="${SCRIPT_VERSION:-$(git describe --tags --always --abbrev=1 2>/dev/null || printf '%s' '0.0.0-dev')}"