Skip to content

Commit 8244f2d

Browse files
committed
🎉 Initial commit...
**Adds** - `argument-parser.awk` script, main source code of this repository - `.github/README.md` file, documents how to utilize this repository - `.gitignore` configuration, defines directory/file patterns Git ignores from version tracking - `LICENSE` copyright file - `Makefile` script, symbolically links repository script(s) to `PATH` accessible directory - `.travis-ci/` directory, contains functions, and scripts, for testing project features - `.travis.yml` configuration, defines steps for Continuous Integration tests
0 parents  commit 8244f2d

File tree

10 files changed

+1961
-0
lines changed

10 files changed

+1961
-0
lines changed

.github/README.md

Lines changed: 722 additions & 0 deletions
Large diffs are not rendered by default.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/gawk -f
2+
3+
4+
## For updates see -> https://github.com/awk-utilities/includes-argument-parser
5+
@include "argument-parser"
6+
7+
8+
BEGIN {
9+
delete parsed_arguments
10+
delete acceptable_arguments
11+
12+
acceptable_arguments["string"] = "--string|-s:value"
13+
acceptable_arguments["boolean"] = "--boolean|-B:bool"
14+
acceptable_arguments["usage"] = "--usage:bool"
15+
acceptable_arguments["increment"] = "--increment|-I:increment"
16+
acceptable_arguments["array"] = "--array:array"
17+
18+
argument_parser(acceptable_arguments, parsed_arguments)
19+
for (k in parsed_arguments) {
20+
if (k == "array") {
21+
for (i in parsed_arguments[k]) {
22+
print "parsed_arguments[\"" k "\"][" i "] ->", parsed_arguments[k][i]
23+
}
24+
} else {
25+
print "parsed_arguments[\"" k "\"] ->", parsed_arguments[k]
26+
}
27+
}
28+
}
29+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
4+
tests__include_installed() {
5+
printf 'Started: %s\n' "${FUNCNAME[0]}"
6+
7+
local -a _awk_arguments=( "${@}" )
8+
9+
## Find true directory function resides in
10+
local _source="${BASH_SOURCE[0]}"
11+
while [[ -h "${_source}" ]]; do
12+
_source="$(find "${_source}" -type l -ls | sed -n 's@^.* -> \(.*\)@\1@p')"
13+
done
14+
local _dir="$(cd -P "$(dirname "${_source}")" && pwd)"
15+
16+
local _expected_output
17+
local _script_output
18+
read -rd '' _expected_output <<'EOF'
19+
parsed_arguments["array"][1] -> one
20+
parsed_arguments["array"][2] -> two
21+
parsed_arguments["boolean"] -> 1
22+
parsed_arguments["increment"] -> 3
23+
parsed_arguments["string"] -> string like value
24+
EOF
25+
26+
_script_output="$("${_dir}/include-installed.awk" "${_awk_arguments[@]}")"
27+
28+
if [[ "${_script_output}" != "${_expected_output}" ]]; then
29+
local -a _error_message=(
30+
"Function: ${FUNCNAME[0]}"
31+
"Arguments: ${_awk_arguments[*]@Q}"
32+
'Expected output ___'
33+
"${_expected_output}"
34+
'___'
35+
'Script output ___'
36+
"${_script_output}"
37+
'___'
38+
)
39+
printf >&2 ' %s\n' "${_error_message[@]}"
40+
return 1
41+
fi
42+
43+
printf 'Finished: %s\n' "${FUNCNAME[0]}"
44+
}
45+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
3+
4+
tests__include_parameter() {
5+
printf 'Started: %s\n' "${FUNCNAME[0]}"
6+
local _include_path="${1:?No include path provided}"
7+
local -a _argument_list=( "${@}" )
8+
local -a _awk_arguments=( "${_argument_list[@]:1}" )
9+
10+
local _expected_output
11+
local _script_output
12+
read -rd '' _expected_output <<'EOF'
13+
parsed_arguments["array"][1] -> one
14+
parsed_arguments["array"][2] -> two
15+
parsed_arguments["boolean"] -> 1
16+
parsed_arguments["increment"] -> 3
17+
parsed_arguments["string"] -> string like value
18+
EOF
19+
20+
_script_output="$(
21+
gawk --include="${_include_path}"\
22+
'BEGIN {
23+
delete parsed_arguments
24+
delete acceptable_arguments
25+
26+
acceptable_arguments["string"] = "--string|-s:value"
27+
acceptable_arguments["boolean"] = "--boolean|-B:bool"
28+
acceptable_arguments["usage"] = "--usage:bool"
29+
acceptable_arguments["increment"] = "--increment|-I:increment"
30+
acceptable_arguments["array"] = "--array:array"
31+
32+
argument_parser(acceptable_arguments, parsed_arguments)
33+
for (k in parsed_arguments) {
34+
if (k == "array") {
35+
for (i in parsed_arguments[k]) {
36+
print "parsed_arguments[\"" k "\"][" i "] ->", parsed_arguments[k][i]
37+
}
38+
} else {
39+
print "parsed_arguments[\"" k "\"] ->", parsed_arguments[k]
40+
}
41+
}
42+
}' "${_awk_arguments[@]}"
43+
)"
44+
45+
if [[ "${_script_output}" != "${_expected_output}" ]]; then
46+
local -a _error_message=(
47+
"Function: ${FUNCNAME[0]}"
48+
"Arguments: ${_awk_arguments[*]@Q}"
49+
'Expected output ___'
50+
"${_expected_output}"
51+
'___'
52+
'Script output ___'
53+
"${_script_output}"
54+
'___'
55+
)
56+
printf >&2 ' %s\n' "${_error_message[@]}"
57+
return 1
58+
fi
59+
60+
printf 'Finished: %s\n' "${FUNCNAME[0]}"
61+
}
62+

.travis-ci/tests.sh

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env bash
2+
3+
4+
## Find true directory script resides in
5+
__SOURCE__="${BASH_SOURCE[0]}"
6+
while [[ -h "${__SOURCE__}" ]]; do
7+
__SOURCE__="$(find "${__SOURCE__}" -type l -ls | sed -n 's@^.* -> \(.*\)@\1@p')"
8+
done
9+
__DIR__="$(cd -P "$(dirname "${__SOURCE__}")" && pwd)"
10+
__PARENT_DIR__="${__DIR__%/*}"
11+
__NAME__="${__SOURCE__##*/}"
12+
__AUTHOR__='S0AndS0'
13+
__DESCRIPTION__='Tests argument-parser.awk script'
14+
15+
16+
set -eE -o functrace
17+
18+
19+
## Provides -> tests__include_parameter '<include-path>' '<awk-arguments[]?>'
20+
# shellcheck source=.travis-ci/features/include-parameter.sh
21+
source "${__DIR__}/features/include-parameter.sh"
22+
23+
## Provides -> tests__include_installed '<awk-arguments[]?>'
24+
# shellcheck source=.travis-ci/features/include-installed.sh
25+
source "${__DIR__}/features/include-installed.sh"
26+
27+
28+
__license__(){
29+
_year="$(date +'%Y')"
30+
cat <<EOF
31+
${__DESCRIPTION__}
32+
Copyright (C) ${_year:-2020} ${__AUTHOR__:-S0AndS0}
33+
34+
This program is free software: you can redistribute it and/or modify
35+
it under the terms of the GNU Affero General Public License as published
36+
by the Free Software Foundation, version 3 of the License.
37+
38+
This program is distributed in the hope that it will be useful,
39+
but WITHOUT ANY WARRANTY; without even the implied warranty of
40+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41+
GNU Affero General Public License for more details.
42+
43+
You should have received a copy of the GNU Affero General Public License
44+
along with this program. If not, see <https://www.gnu.org/licenses/>.
45+
EOF
46+
}
47+
48+
49+
__usage__() {
50+
cat <<EOF
51+
${__DESCRIPTION__}
52+
53+
54+
## Parameters
55+
56+
57+
-h --help <boolean>
58+
59+
{Optional} Prints this message and exists
60+
61+
62+
-l --license <boolean>
63+
64+
{Optional} Prints license and exits
65+
66+
67+
## Example
68+
69+
70+
cd "${__PARENT_DIR__}"
71+
./.travis-ci/${__NAME__}
72+
EOF
73+
}
74+
75+
76+
(( ${#@} )) && {
77+
case "${@}" in
78+
--help|-h|help)
79+
__usage__
80+
_exit_status="$?"
81+
;;
82+
--license|-l|license)
83+
__license__
84+
_exit_status="$?"
85+
;;
86+
*)
87+
__usage__
88+
_exit_status="1"
89+
;;
90+
esac
91+
exit "${_exit_status}"
92+
}
93+
94+
95+
##
96+
# parameter
97+
test_function() {
98+
local _function_name="${1:?No function_name name provided}"
99+
local -a _argument_list=( "$@" )
100+
local -a _function_arguments=( "${_argument_list[@]:1}" )
101+
"${_function_name}" "${_function_arguments[@]}" || {
102+
local _status="${?}"
103+
printf 'Failed -> %s\n' "${_function_name}"
104+
}
105+
return "${_status:-0}"
106+
}
107+
108+
109+
__AWK_TEST_ARGUMENTS__=(
110+
--string "string like value"
111+
--increment
112+
-I
113+
-I
114+
-B
115+
--array one
116+
--array two
117+
)
118+
119+
# test_function tests__include_parameter "${__PARENT_DIR__}/argument-parser.awk"
120+
test_function tests__include_parameter "${__PARENT_DIR__}/argument-parser.awk"\
121+
"${__AWK_TEST_ARGUMENTS__[@]}"
122+
123+
124+
test_function tests__include_installed "${__AWK_TEST_ARGUMENTS__[@]}"
125+

.travis.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## Note, "[ci skio]" or "[skip ci]" within a commit message _should_
2+
## cause Travis Continuous Integration to skip the following
3+
4+
## Clone settings for repository
5+
git:
6+
depth: 10
7+
quite: true
8+
submodules: false
9+
10+
11+
## Attempt to limit environment to only what is required
12+
language: bash
13+
matrix:
14+
fast_finish: true
15+
## Thanks be to, https://docs.travis-ci.com/user/multi-os/
16+
include:
17+
- name: 'Linux Xenial'
18+
os: linux
19+
dist: xenial
20+
21+
22+
branches:
23+
only:
24+
- main
25+
## Allow building of 'test-' previxed branches
26+
- /^(?i:test)-.*$/
27+
except:
28+
- example
29+
30+
31+
#
32+
# Do the things
33+
#
34+
## Install and setup servers this project makes use of
35+
before_install:
36+
- sudo apt-get install -yqq gawk
37+
38+
install:
39+
- sudo make install
40+
41+
before_script:
42+
- argument-parser.awk --lint
43+
44+
script:
45+
- ./.travis-ci/tests.sh
46+
47+
after_failure:
48+
- printf 'after_failure asks -> Where did %s go wrong?\n' "${USER}"
49+
50+
after_success:
51+
- printf 'after_success -> Tests passed!\n'
52+
53+
after_script:
54+
- echo 'after_script -> Is it all good?!'
55+

0 commit comments

Comments
 (0)