From 018ca73444e8ccd10f88079f7cf786fd2ddeb42d Mon Sep 17 00:00:00 2001 From: Jesse Wattenbarger Date: Sat, 17 May 2025 11:23:25 -0400 Subject: [PATCH] Add --bare option to `pyenv version` (#2783) --- COMMANDS.md | 7 +++++++ libexec/pyenv-version | 24 ++++++++++++++++++++---- test/version.bats | 9 +++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/COMMANDS.md b/COMMANDS.md index 1d634471..c062175f 100644 --- a/COMMANDS.md +++ b/COMMANDS.md @@ -272,8 +272,15 @@ version of Python, or install a package that provides binaries. Displays the currently active Python version, along with information on how it was set. + Usage: pyenv version [--bare] + + --bare show just the version name. An alias to `pyenv version-name' + + $ pyenv version 2.7.6 (set by /home/yyuu/.pyenv/version) + $ pyenv version --bare + 2.7.6 ## `pyenv versions` diff --git a/libexec/pyenv-version b/libexec/pyenv-version index 33de9325..75e2287c 100755 --- a/libexec/pyenv-version +++ b/libexec/pyenv-version @@ -1,9 +1,8 @@ #!/usr/bin/env bash # Summary: Show the current Python version(s) and its origin +# Usage: pyenv version [--bare] # -# Shows the currently selected Python version(s) and how it was -# selected. To obtain only the version string, use `pyenv -# version-name'. +# --bare show just the version name. An alias to `pyenv version-name' set -e [ -n "$PYENV_DEBUG" ] && set -x @@ -13,8 +12,25 @@ OLDIFS="$IFS" IFS=: PYENV_VERSION_NAMES=($(pyenv-version-name)) || exitcode=$? IFS="$OLDIFS" +unset bare +for arg; do + case "$arg" in + --complete ) + echo --bare + exit ;; + --bare ) bare=1 ;; + * ) + pyenv-help --usage version >&2 + exit 1 + ;; + esac +done for PYENV_VERSION_NAME in "${PYENV_VERSION_NAMES[@]}"; do - echo "$PYENV_VERSION_NAME (set by $(pyenv-version-origin))" + if [[ -n $bare ]]; then + echo "$PYENV_VERSION_NAME" + else + echo "$PYENV_VERSION_NAME (set by $(pyenv-version-origin))" + fi done exit $exitcode diff --git a/test/version.bats b/test/version.bats index 451fc3a7..78c6681b 100644 --- a/test/version.bats +++ b/test/version.bats @@ -70,3 +70,12 @@ pyenv-version-without-stderr() { 3.3.3 (set by PYENV_VERSION environment variable) OUT } + +@test "--bare prints just the name" { + create_version "3.3.3" + PYENV_VERSION=3.3.3 run pyenv-version --bare + assert_success + assert_output <