Browse code

Add argument parsing

Robert Cranston authored on 21/12/2021 09:19:37
Showing 2 changed files

... ...
@@ -5,6 +5,48 @@ Run [GLSL][] code on the CPU.
5 5
 [`glslrun`]: https://git.rcrnstn.net/rcrnstn/glslrun
6 6
 [GLSL]: https://en.wikipedia.org/wiki/OpenGL_Shading_Language
7 7
 
8
+## Usage
9
+
10
+`glslrun --help`:
11
+
12
+```
13
+glslrun 1.0
14
+
15
+Run GLSL code snippets on the CPU.
16
+
17
+usage:
18
+  glslrun [options] [--] [<file>]
19
+  glslrun -h|--help
20
+  glslrun --version
21
+
22
+arguments:
23
+  <file>
24
+    File containing code to run. Specify - to read from stdin instead.
25
+
26
+options:
27
+  -m, --main
28
+    Generate an enclosing main() function.
29
+
30
+  -p, --print
31
+    Print the generated code that is passed to the compiler.
32
+```
33
+
34
+## Dependencies
35
+
36
+All dependencies are [POSIX][] and are highly likely to be installed by default
37
+(e.g. they all have the [Debian priority][] `required`).
38
+
39
+-   `sh` (e.g. from [`bash`][] or [`dash`][])
40
+-   `sed` (e.g. from [`sed`][])
41
+-   `awk` (e.g. from [`mawk`][])
42
+
43
+[POSIX]: https://en.wikipedia.org/wiki/POSIX
44
+[Debian priority]: https://www.debian.org/doc/debian-policy/ch-archive.html#s-priorities
45
+[`bash`]: https://packages.debian.org/bash
46
+[`dash`]: https://packages.debian.org/dash
47
+[`sed`]: https://packages.debian.org/sed
48
+[`mawk`]: https://packages.debian.org/mawk
49
+
8 50
 ## License
9 51
 
10 52
 Licensed under the [ISC License][] unless otherwise noted, see the
11 53
new file mode 100755
... ...
@@ -0,0 +1,61 @@
1
+#!/bin/sh
2
+set -euC
3
+
4
+##/// glslrun 1.0
5
+#////
6
+#//// Run GLSL code snippets on the CPU.
7
+#////
8
+###// usage:
9
+#////   glslrun [options] [--] [<file>]
10
+#////   glslrun -h|--help
11
+#////   glslrun --version
12
+#////
13
+###// arguments:
14
+####/   <file>
15
+#////     File containing code to run. Specify - to read from stdin instead.
16
+#////
17
+###// options:
18
+####/   -m, --main
19
+#////     Generate an enclosing main() function.
20
+#////
21
+####/   -p, --print
22
+#////     Print the generated code that is passed to the compiler.
23
+
24
+## Messages
25
+help()    { sed -n 's|^#[#/]*/ \?||p' "$0";              exit 0;    }
26
+version() { help | awk '/^$/{++p;next}p==0';             exit 0;    }
27
+usage()   { help | awk '/^$/{++p;next}p==2';             exit 0;    }
28
+parse()   { printf '%s: error: %s\n'   "$0" "$1"; usage; exit 1;    } >&2
29
+error()   { printf '%s: error: %s\n'   "$0" "$1";        exit 1;    } >&2
30
+warning() { printf '%s: warning: %s\n' "$0" "$1";                   } >&2
31
+opt()     { [ $# -gt 1 ] || parse "option '$1' value not provided"; }
32
+arg()     { [ $# -gt 1 ] || parse "argument '$1' not provided";     }
33
+
34
+## Parse special options
35
+case "${1-}"
36
+in
37
+  '-h'|'--help') help; ;;
38
+  '--version') version; ;;
39
+esac
40
+
41
+## Parse options
42
+main=''
43
+print=''
44
+while [ $# -gt 0 ]
45
+do
46
+  case "$1"
47
+  in
48
+    '-m'|'--main') main='y'; ;;
49
+    '-p'|'--print') print='y'; ;;
50
+    '--') shift; break; ;;
51
+    '-'?*) parse "unrecognized option '$1'"; ;;
52
+    *) break; ;;
53
+  esac
54
+  shift
55
+done
56
+
57
+## Parse optional arguments
58
+file="${1-}"; shift $(($#>0));
59
+
60
+## Parse unrecognized arguments
61
+[ $# -eq 0 ] || parse "unrecognized argument: '$1'"