shake
minimal build system that generates Ninja build files
git clone https://9o.is/git/shake.git
commit 7f3edf3c9045ef41046eeefd4f6094a56a801c91 parent abf6473eed93f6f533a5e2b7d9a5eda5cf5a30be Author: Jul <jul@9o.is> Date: Tue, 17 Mar 2026 00:06:59 +0800 add optional flag parsing to rule command Diffstat:
| M | shake | | | 68 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- |
1 file changed, 64 insertions(+), 4 deletions(-)
diff --git a/shake b/shake @@ -252,18 +252,78 @@ build() { printf '\n' >&3 } +_shake_requires_arg() { + case "$2" in + *"$1:"*) return 0;; + *) return 1;; + esac +} + +_shake_parse_args() { + _optstring="$1" + shift + + _opts= + _deps= + _optshift=0 + _optcnt=$# + + while [ $# -gt 0 ]; do + case "$1" in + -*) + _optflag="${1#-}" + if _shake_requires_arg "$_optflag" "$_optstring"; then + _optval="$2" + case "$_optval" in + \$*dir/*) + if [ -z "$_deps" ]; then + _deps='-' + fi + _deps="$_deps $_optval" + ;; + esac + shift 2 + else + _optval= + shift 1 + fi + [ -n "$_optval" ] && _opts="$_opts -$_optflag $_optval" || _opts="$_opts -$_optflag" + ;; + *) + break + ;; + esac + done + + _optrem=$# + _optshift=$(( _optcnt - _optrem )) +} + rule() { - printf 'rule %s\n' "$1" >&3 - bind command "${*:2}" + _r=$1 + shift + printf 'rule %s\n' "$_r" >&3 + + _optstring= + if [ "$1" == '-o' ]; then + _optstring="$2" + shift 2 + fi + + bind command "$*" _d= - for _v in ${*:2}; do + for _v in $*; do case "$_v" in \$*dir/*|/*|./|../) _d="${_d:-'-'} $_v";; esac done - eval "$1() { build $1 \$* $_d; }" + if [ -z "$_optstring" ]; then + eval "$_r() { build $_r \$* $_d; }" + else + eval "$_r() { _shake_parse_args '$_optstring' \$*; shift \$_optshift; build $_r \$* \$_deps $_d; bind opts \"\$_opts\"; }" + fi } default() {