Skip to content

Commit

Permalink
[oil-language] Examples of rewriting to use arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Aug 10, 2019
1 parent 8717fc8 commit 0c55896
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
42 changes: 42 additions & 0 deletions oil_lang/testdata/array-rewrite-1.sh
@@ -0,0 +1,42 @@
#!/bin/bash
#
# Example of Oil arrays.
#
# Adapted from ./linux-4.8.7/scripts/tags.sh

# Show args to a command
argv() { spec/bin/argv.py "$@"; }

#
# OLD, BASH-COMPATIBLE WAY
#

regex=('one' 'two')
flags=()

for r in "${regex[@]}"; do
flags[${#flags[@]}]="--regex=$r"
done

argv "${flags[@]}"


#
# NEW OIL WAY
#
# Things fixed:
# - verbose "${a[@]}" becomes @a
# - a=() is weird because it doesn't allow spaces around =
# - builtin 'push' for appending
#

shopt -s static-word-eval oil-parse-at

var regex2 = @(two three)
var flags2 = @()

for r in @regex2; do
push flags2 _ "--regex=$r"
done

argv @flags2
55 changes: 55 additions & 0 deletions oil_lang/testdata/array-rewrite-2.sh
@@ -0,0 +1,55 @@
#!/bin/bash
#
# Example of rewriting string-based argv processing to use arrays.
#
# Adapted from ./linux-4.8.7/scripts/link-vmlinux.sh

# Show args to a command
argv() { spec/bin/argv.py "$@"; }

#
# OLD, BASH-COMPATIBLE WAY
#
# This style can't handle paths with spaces

path='/etc/path with spaces'

CONFIG_HAVE_FOO=yes

flags=''

if [ -n "${CONFIG_HAVE_FOO}" ]; then
flags="${flags} --foo=$path"
fi

if [ -n "${CONFIG_HAVE_BAR}" ]; then
flags="${flags} --bar"
fi

argv ${flags} # unquoted splitting


#
# NEW OIL WAY
#
# - no quoting is necessary because of static-word-eval
# - splice arrays with @
# - builtin 'push' for appending
# - I might want to change the ignored delimiter character _ to something like
# : or :: or \\ . Opinions?

shopt -s oil-parse-at static-word-eval

CONFIG_HAVE_FOO=yes

var flags = @()

if test -n $CONFIG_HAVE_FOO; then
push flags _ --foo=$path
fi

if test -n $CONFIG_KALLSYMS_BASE_RELATIVE; then
push flags _ --bar
fi

argv @flags

0 comments on commit 0c55896

Please sign in to comment.