Skip to content

Commit 1ea41fd

Browse files
committed
docs: Document all functions with examples
1 parent 8d332d6 commit 1ea41fd

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,59 @@ Useful functions for any Bash program. Often vital for Basalt programs
44

55
## Summary
66

7-
Added functions: `core.trap_add`, `core.trap_remove` `core.shopt_push`, and `core.shopt_pop`
7+
### `init`
8+
9+
- `core.init`
10+
11+
Initialize global variables used by other functions
12+
13+
Add and remove traps. With these, multiple packages can add or remove traps handlers for signals
14+
15+
### `trap`
16+
17+
- `core.trap_add`
18+
- `core.trap_remove`
19+
20+
Example
21+
22+
```sh
23+
some_handler() { printf '%s\n' 'This was called on USR1! ^w^'; }
24+
core.trap_add 'some_handler' 'USR1'
25+
kill -USR1 $$
26+
core.trap_remove 'some_handler' 'USR1'
27+
```
28+
29+
### `shopt`
30+
31+
- `core.shopt_push`
32+
- `core.shopt_pop`
33+
34+
Example
35+
36+
```sh
37+
core.shopt_push -s extglob
38+
[[ 'variable' == @(foxtrot|golf|echo|variable) ]] && printf '%s\n' 'Woof!'
39+
core.shopt_pop
40+
```
41+
42+
### `err`
43+
44+
I suppose it can look redundant (compared to `if ! fn; then :; fi`), but it can help make errors a bit more safe in larger applications, since you don't have to worry about a caller forgetting to `if ! fn` or `fn ||` (and terminating the script if `set -e`). It also makes it easier to communicate specific error codes and helps separate between calculated / expected errors and unexpected errors (fatal / faults)
45+
46+
- `core.err_set`
47+
- `core.err_clear`
48+
- `core.err_exists`
49+
50+
Example
51+
52+
```sh
53+
create_some_error() { core.err_clear; core.err_set 'Some error' }
54+
55+
create_some_error || fatal "Did not expect an error"
56+
if core.err_exists; then
57+
printf '%s\n' 'Something happened'
58+
fi
59+
```
860
961
## Installation
1062

pkg/lib/public/error.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# TODO: deprecate public/bash-error.sh
1+
# shellcheck shell=bash
22

33
core.err_set() {
44
if (($# == 1)); then

tests/util/init.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# shellcheck shell=bash
22

3-
eval "$(basalt-package-init)"
4-
basalt.package-init
5-
basalt.package-load
3+
eval "$(basalt-package-init)" || exit
4+
basalt.package-init || exit
5+
basalt.package-load || exit
66
basalt.load 'github.com/hyperupcall/bats-all' 'load.bash'
77

88
load './util/test_util.sh'

0 commit comments

Comments
 (0)