Browse code

Add parsing

Robert Cranston authored on 30/01/2022 19:20:03
Showing 1 changed files

1 1
new file mode 100755
... ...
@@ -0,0 +1,45 @@
1
+#!/bin/sh
2
+set -euC
3
+
4
+##/// background 1.0
5
+#////
6
+#//// Set X Window System background image/video.
7
+#////
8
+###// usage:
9
+#////   background [<path>]
10
+#////   background -h|--help
11
+#////   background --version
12
+#////
13
+###// arguments:
14
+####/   <path>
15
+#////     If not a directory, set it as background. If a directory, search
16
+#////     recursively in it (resolving symlinks) and set a random file as
17
+#////     background.
18
+#////     [default: ${XDG_DATA_HOME:-$HOME/.local/share}/backgrounds]
19
+
20
+## Messages
21
+prog="$(basename "$0")"
22
+help()    { sed -n 's|^#[#/]*/ \?||p' "$0";                 exit 0; }
23
+version() { help | awk '/^$/{++p;next}p==0';                exit 0; }
24
+usage()   { help | awk '/^$/{++p;next}p==2';                exit 0; }
25
+parse()   { printf '%s: error: %s\n'   "$prog" "$1"; usage; exit 1; } >&2
26
+error()   { printf '%s: error: %s\n'   "$prog" "$1";        exit 1; } >&2
27
+warning() { printf '%s: warning: %s\n' "$prog" "$1";                } >&2
28
+opt()     { [ $# -gt 1 ] || parse "option '$1' value not provided"; }
29
+arg()     { [ $# -gt 1 ] || parse "argument '$1' not provided";     }
30
+argend()  { [ $# -eq 0 ] || parse "argument '$1' not recognized";   }
31
+dep()     { [ "$(command -v "$1")" ] || error "'$1' not available"; }
32
+
33
+## Parse special options
34
+case "${1:-}"
35
+in
36
+  '-h'|'--help') help; ;;
37
+  '--version') version; ;;
38
+esac
39
+
40
+## Parse optional arguments
41
+path="${XDG_DATA_HOME:-"$HOME/.local/share"}/backgrounds"
42
+[ $# -eq 0 ] || { path="$1"; shift; }
43
+
44
+## Parse unrecognized arguments
45
+argend "$@"