diff --git a/README.md b/README.md index 6512330..f6ee542 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/cookbook-args-lists-strings.sh b/examples/cookbook-args-lists-strings.sh new file mode 100755 index 0000000..8b5d8db --- /dev/null +++ b/examples/cookbook-args-lists-strings.sh @@ -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" diff --git a/examples/cookbook-cleanup-temp.sh b/examples/cookbook-cleanup-temp.sh new file mode 100755 index 0000000..4909517 --- /dev/null +++ b/examples/cookbook-cleanup-temp.sh @@ -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 diff --git a/tests/validate.sh b/tests/validate.sh index 1f462eb..33787bc 100755 --- a/tests/validate.sh +++ b/tests/validate.sh @@ -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 @@ -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 \ @@ -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'