Files
fish-config/functions/__fish_variable_check.fish
T
rootiest e651566e14 docs(functions): split RETURNS into EXIT STATUS and stdout RETURNS
RETURNS previously conflated fish's $status exit code with genuine
stdout/printed output, e.g. rm listing "0/1" as if they were print
values rather than exit codes. Rename RETURNS to EXIT STATUS across
all 83 documented functions, and reintroduce RETURNS as a distinct
label reserved for the 15 functions that actually print to stdout.

Update build-manual.py's ENTRY_HEADS to render Exit Status before
Returns, manualtools.py's SECTIONS constant, and AGENTS.md's label
order and label-usage guidance to match. Add two verify-manual.py
regression tests: EXIT STATUS bodies must never contain stray
stdout/printed language, and Returns: must always render after
Exit Status: when both are present. Regenerate docs/fish-config.md.
2026-07-26 16:41:57 -04:00

60 lines
1.6 KiB
Fish

# Copyright (C) 2026 Rootiest
# SPDX-License-Identifier: AGPL-3.0-or-later
# SYNOPSIS
# __fish_variable_check <variable_name>
#
# DESCRIPTION
# Evaluates a given variable's value to determine its truthiness or falsiness.
# Safely dereferences the variable name and performs case-insensitive matching.
# Pass the variable name (without $), not its value.
#
# ARGUMENTS
# variable_name Name of the variable to check (without $ prefix)
#
# EXIT STATUS
# 0 True (opt-in): value matches 1, true, yes, on, or y
# 1 False (opt-out): value matches 0, false, no, off, or n
# 2 Empty: variable is unset or empty
# 3 Garbage: value is unrecognized
#
# EXAMPLE
# __fish_variable_check __fish_config_strict_mode
# if test $status -eq 0
# echo "Strict mode enabled!"
# end
function __fish_variable_check --description "Check if a variable is set to a truthy or falsy value."
set -l var_name $argv[1]
# Make sure they actually passed an argument
if test -z "$var_name"
return 2
end
# 2: Empty / Unset (Check if the variable exists in the environment at all)
if not set -q $var_name
return 2
end
# Dereference the variable name to get its actual value using $$
set -l val (string lower "$$var_name")
# 2: Empty / Unset (Check if it exists but is just an empty string)
if test -z "$val"
return 2
end
# 0: True (Opt-In)
if contains -- "$val" 1 true yes on y
return 0
end
# 1: False (Opt-Out)
if contains -- "$val" 0 false no off n
return 1
end
# 3: Garbage / Unrecognized
return 3
end