Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,17 @@ Use `base_bash_libs_require_version` to require a minimum library version:
base_bash_libs_require_version 1.1.0
```

See `examples/std-usage.sh` for a small standalone script that sources the
stdlib, imports the file helpers, logs progress, and runs a checked command.
## Examples

- [`examples/std-usage.sh`](examples/std-usage.sh)
Small standalone script that sources the stdlib, imports the file helpers,
logs progress, and runs a checked command.
- [`examples/cookbook-cleanup-temp.sh`](examples/cookbook-cleanup-temp.sh)
Cleanup hooks, temp paths, version checks, command resolution, and timeout
command execution.
- [`examples/cookbook-args-lists-strings.sh`](examples/cookbook-args-lists-strings.sh)
Argument parsing, list helpers, and in-place string transformations working
together.

## Versioning

Expand Down
39 changes: 39 additions & 0 deletions examples/cookbook-args-lists-strings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" || exit 1

# shellcheck source=/dev/null
source "$repo_root/lib/bash/std/lib_std.sh"

import "$repo_root/lib/bash/arg/lib_arg.sh"
import "$repo_root/lib/bash/list/lib_list.sh"
import "$repo_root/lib/bash/str/lib_str.sh"

declare -A options=()
declare -a positionals=()
declare -a specs=(
"verbose|flag|--verbose|-v"
"tag|value|--tag|-t"
)

arg_parse options positionals specs -- --tag " Release Candidate " --verbose alpha beta

tag="${options[tag]-default}"
str_trim tag
str_lower tag

declare -a values=()
declare -a unique_values=()
summary=""
count=""

list_append values "$tag" "${positionals[@]}" "$tag"
list_unique unique_values values
list_length count unique_values
str_join summary "," unique_values

if [[ "${options[verbose]-}" == "1" ]]; then
log_info "Cookbook parsed $count unique values."
fi

print_message "summary=$summary"
32 changes: 32 additions & 0 deletions examples/cookbook-cleanup-temp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" || exit 1

# shellcheck source=/dev/null
source "$repo_root/lib/bash/std/lib_std.sh"

base_bash_libs_require_version 1.0.0

workspace_dir=""
report_file=""

std_make_temp_dir workspace_dir "base-cookbook"
std_make_temp_file report_file "base-cookbook"

cleanup_marker() {
log_debug "cleaning cookbook workspace: $workspace_dir"
}

std_register_cleanup_hook cleanup_marker

printf 'workspace=%s\n' "$workspace_dir" >"$report_file"
std_run_with_timeout --no-exit --quiet 5 test -s "$report_file"

printf_path=""
if std_command_path printf_path printf; then
std_run --no-exit --quiet "$printf_path" 'report_file=%s\n' "$report_file"
fi

if std_function_exists cleanup_marker; then
log_info "Registered cleanup hook for cookbook example."
fi
6 changes: 6 additions & 0 deletions tests/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ required_files=(
.github/workflows/project-intake.yml
.github/workflows/tests.yml
examples/std-usage.sh
examples/cookbook-cleanup-temp.sh
examples/cookbook-args-lists-strings.sh
lib/bash/README.md
lib/bash/std/lib_std.sh
lib/bash/std/tests/lib_std.bats
Expand Down Expand Up @@ -90,6 +92,8 @@ done
shellcheck --severity=error \
tests/validate.sh \
examples/std-usage.sh \
examples/cookbook-cleanup-temp.sh \
examples/cookbook-args-lists-strings.sh \
lib/bash/std/lib_std.sh \
lib/bash/file/lib_file.sh \
lib/bash/git/lib_git.sh \
Expand All @@ -107,5 +111,7 @@ bats \
lib/bash/list/tests/lib_list.bats

examples/std-usage.sh >/dev/null
examples/cookbook-cleanup-temp.sh >/dev/null
examples/cookbook-args-lists-strings.sh >/dev/null

printf 'Bash library validation passed.\n'
Loading