Skip to content

Better shellcheck support #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
32 changes: 32 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"cSpell.words": [
"ARGBASH",
"SPURIONS",
"alnum",
"autom",
"browsable",
"changecom",
"chmod",
"commandline",
"defns",
"docopt",
"errstr",
"eval",
"fname",
"infile",
"infname",
"lolo",
"newerfile",
"outfile",
"outfname",
"positionals",
"posix",
"searchdir",
"shfile",
"sortof",
"srcfile",
"srcfiles",
"srcstem",
"thatfile"
]
}
272 changes: 139 additions & 133 deletions bin/argbash

Large diffs are not rendered by default.

55 changes: 28 additions & 27 deletions bin/argbash-1to2
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
version=2.10.0
# ARG_POSITIONAL_INF([input],[The input file to transform],[1])
# ARG_OPTIONAL_SINGLE([output],[o],[Name of the output file (pass '-' for stdout and empty string for the same as input file)],[""])
# ARG_VERSION([echo "argbash-1to2 v$version"])
# ARG_VERSION([echo "argbash-1to2 v${version}"])
# ARG_HELP([Convert a template for argbash>=1,<2 to argbash>=2,<3])

# ARGBASH_GO()
Expand All @@ -29,7 +29,7 @@ begins_with_short_option()
{
local first_option all_short_options='ovh'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
test "${all_short_options}" = "${all_short_options/${first_option}/}" && return 1 || return 0
}

# THE DEFAULTS INITIALIZATION - POSITIONALS
Expand All @@ -56,9 +56,9 @@ parse_commandline()
while test $# -gt 0
do
_key="$1"
case "$_key" in
case "${_key}" in
-o|--output)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
test $# -lt 2 && die "Missing value for the optional argument '${_key}'." 1
_arg_output="$2"
shift
;;
Expand All @@ -69,11 +69,11 @@ parse_commandline()
_arg_output="${_key##-o}"
;;
-v|--version)
echo "argbash-1to2 v$version"
echo "argbash-1to2 v${version}"
exit 0
;;
-v*)
echo "argbash-1to2 v$version"
echo "argbash-1to2 v${version}"
exit 0
;;
-h|--help)
Expand All @@ -86,7 +86,7 @@ parse_commandline()
;;
*)
_last_positional="$1"
_positionals+=("$_last_positional")
_positionals+=("${_last_positional}")
_positionals_count=$((_positionals_count + 1))
;;
esac
Expand All @@ -98,7 +98,7 @@ parse_commandline()
handle_passed_args_count()
{
local _required_args_string="'input'"
test "${_positionals_count}" -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 1 (namely: $_required_args_string), but got only ${_positionals_count}." 1
test "${_positionals_count}" -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 1 (namely: ${_required_args_string}), but got only ${_positionals_count}." 1
}


Expand All @@ -109,14 +109,14 @@ assign_positional_args()
_our_args=$((${#_positionals[@]} - 1))
for ((ii = 0; ii < _our_args; ii++))
do
_positional_names="$_positional_names _arg_input[$((ii + 1))]"
_positional_names="${_positional_names} _arg_input[$((ii + 1))]"
done

shift "$_shift_for"
shift "${_shift_for}"
for _positional_name in ${_positional_names}
do
test $# -gt 0 || break
eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
eval "${_positional_name}=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
shift
done
}
Expand All @@ -138,36 +138,37 @@ cleanup()
test "${#_files_to_clean[*]}" != 0 && rm -f "${_files_to_clean[@]}"
}

do_stuff ()
do_stuff()
{
# SCRIPT_DIR is likely also a default, but maybe not - it may have been set explicitly
grep -q '\${\?SCRIPT_DIR' -- "$infname" && echo "You probably use a variable 'SCRIPT_DIR' in your script. It may be that you should rename it to 'script_dir', but this is not certain :-(" >&2
grep -q '\${\?SCRIPT_DIR' -- "${infname}" && echo "You probably use a variable 'SCRIPT_DIR' in your script. It may be that you should rename it to 'script_dir', but this is not certain :-(" >&2
# We match $_ARG_FOO as well as ${ARG_FOO...
# and _ARGS_FOO
sed 's/\(\${\?_ARGS\?_\w\+\)/\L\1\l/g' "$infname"
sed 's/\(\${\?_ARGS\?_\w\+\)/\L\1\l/g' "${infname}"
}

outfname="$_arg_output"
test "${#infname[@]}" -gt 1 && test -n "$outfname" && die "You have specified more than one (${#infname[@]}) input filenames, so you probably want to modify the corresponding files in-place. In order to do so, you can't specify an output filename, even '-' does make no sense (currently: '$outfname')"
outfname="${_arg_output}"
test "${#infname[@]}" -gt 1 && test -n "${outfname}" && die "You have specified more than one (${#infname[@]}) input filenames, so you probably want to modify the corresponding files in-place. In order to do so, you can't specify an output filename, even '-' does make no sense (currently: '${outfname}')"

trap cleanup EXIT
for infname in "${_arg_input[@]}"
do
test -f "$infname" || { echo "The input parameter has to be a file (got: '$infname')" >&2; exit 1; }

test -n "$_arg_output" || outfname="$infname"
if test "$outfname" = '-'
then
for infname in "${_arg_input[@]}"; do
test -f "${infname}" || {
echo "The input parameter has to be a file (got: '${infname}')" >&2
exit 1
}

test -n "${_arg_output}" || outfname="${infname}"
if test "${outfname}" = '-'; then
do_stuff
else
# vvv This should catch most of the cases when we want to overwrite the source file
# vvv and we don't want to leave a file (not even an empty one) when something goes wrong.
temp_outfile="temp_$$"
_files_to_clean+=("$temp_outfile")
do_stuff > "$temp_outfile"
mv "$temp_outfile" "$outfname"
_files_to_clean+=("${temp_outfile}")
do_stuff > "${temp_outfile}"
mv "${temp_outfile}" "${outfname}"
# So we don't make .m4 scripts executable
chmod --reference "$infname" "$outfname"
chmod --reference "${infname}" "${outfname}"
fi
done

Expand Down
Loading