summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-27 06:53:49 +0200
committerPaul Buetow <paul@buetow.org>2026-03-27 06:53:49 +0200
commit1cadb8e0ce613bc9327393a95059b598187417e9 (patch)
treeeabecc8a1f342f331e1f07add8c190d6aefcb084 /scripts
parent65ad83b914245fbfbed188b4fcea9b40cfc297ad (diff)
Harden formatter wrapper for task 1d9a1522-7f86-4c43-a004-1b906e2a49a3
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/formatthecode.sh46
1 files changed, 40 insertions, 6 deletions
diff --git a/scripts/formatthecode.sh b/scripts/formatthecode.sh
index a146f33..6263d17 100755
--- a/scripts/formatthecode.sh
+++ b/scripts/formatthecode.sh
@@ -7,7 +7,7 @@ REPO_ROOT=$(cd "${SCRIPT_DIR}/.." && pwd)
usage() {
cat <<'EOF'
-Usage: ./scripts/formatthecode.sh [JAVA_FILE ...]
+Usage: ./scripts/formatthecode.sh [--] [JAVA_FILE ...]
Formats Java sources with Artistic Style using the project's historical style.
@@ -16,6 +16,7 @@ Without arguments, formats all Java files under:
- src/test/java
With arguments, formats only the provided files.
+Use `--` before filenames that begin with `-`.
Options:
-h, --help Show this help text
@@ -36,8 +37,22 @@ collect_default_targets() {
-type f -name '*.java' -print0
}
+sanitize_path_for_astyle() {
+ local path="$1"
+
+ if [[ "${path}" == -* ]]; then
+ printf './%s\n' "${path}"
+ return
+ fi
+
+ printf '%s\n' "${path}"
+}
+
main() {
- if [[ $# -gt 0 ]]; then
+ local -a file_args=()
+ local -a astyle_args=()
+
+ while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
@@ -47,21 +62,40 @@ main() {
check_deps
return 0
;;
+ --)
+ shift
+ file_args=("$@")
+ break
+ ;;
+ -*)
+ echo "error: unknown option: $1" >&2
+ echo "Use -- before filenames that begin with '-'." >&2
+ return 1
+ ;;
+ *)
+ file_args+=("$1")
+ ;;
esac
- fi
+
+ shift
+ done
check_deps
- if [[ $# -gt 0 ]]; then
+ if [[ ${#file_args[@]} -gt 0 ]]; then
local target
- for target in "$@"; do
+ for target in "${file_args[@]}"; do
if [[ ! -f "${target}" ]]; then
echo "error: file not found: ${target}" >&2
return 1
fi
done
- astyle --style=java --mode=java -n "$@"
+ for target in "${file_args[@]}"; do
+ astyle_args+=("$(sanitize_path_for_astyle "${target}")")
+ done
+
+ astyle --style=java --mode=java -n "${astyle_args[@]}"
return 0
fi