<commit_before>#!/bin/sh
set -e

COMMAND="${0}"
VERSION="0.0"

DEFAULT_REGEX="^[a-zA-Z0-9_\-\.]+$"
LANANA_REGEX="^[a-z0-9]+$"

# One of "run", "list", and "report"
mode="run"

verbose="off"
reverse="off"
exit_on_error="off"
new_session="off"
regex="${DEFAULT_REGEX}"
umask="022"
dir=""

# Starts with a comma to ease searching
ignore_suffixes=",.rpmsave,.rpmorig,.rpmnew,.swp,.cfsaved,"

# One of "run", "directory", "umask", or "arg"
parsemode="run"

may_fail() {
	local command="${1}"
	local outvar="${2}"

	set +e
	"${command}"
	eval "${outvar}"=$?
	set -e
}

show_version() {
	echo "run-parts ${VERSION}"
}

show_help() {
	echo "Usage: ${COMMAND} [OPTIONS] [--] DIRECTORY"
	echo "    --test"
	echo "    --list"
	echo "    -v, --verbose"
	echo "    --report"
	echo "    --reverse"
	echo "    --exit-on-error"
	echo "    --new-session"
	echo "    --regex=RE"
	echo "    -u, --umask=UMASK"
	echo "    --ignore-suffixes=SUFFIX1[,SUFFIX2,...]"
	echo "    -h, --help"
	echo "    -V, --version"
}

parse_long_argument() {
	local input="${1}"
	echo $(echo "${input}" | sed "s:--[a-z_\-]\+=::" -)
}

dispatch_parse() {
	case ${arg} in
		--test )
			mode="test"
			;;
		--list )
			mode="list"
			;;
		--report )
			verbose="report"
			;;
		-v | --verbose )
			verbose="verbose"
			;;
		--reverse )
			reverse="on"
			;;
		--exit-on-error )
			exit_on_error="on"
			;;
		-h | --help )
			show_help
			exit 0
			;;
		-V | --version )
			show_version
			exit 0
			;;
		--regex=* )
			regex=$(parse_long_argument "${arg}")
			;;
		-u)
			parsemode="umask"
			;;
		--umask=* )
			umask=$(parse_long_argument "${arg}")
			;;
		-- )
			parsemode="directory"
			;;
		* )
			echo "Unknown argument ${arg}"
			show_help
			exit 1
			;;
	esac
}

gotarg="no"
for arg in $@; do
	gotarg="yes"

	case "${parsemode}" in
		"directory" )
			dir="${arg}"
			;;
		"umask" )
			umask="${arg}"
			parsemode="run"
			;;
		"run" )
			dispatch_parse "${arg}"
			;;
		* )
			echo "Unknown state ${parsemode}"
			exit 1
			;;
	esac

	shift 1
done

if [ ${gotarg} = "no" ]; then
	show_help
	exit 0
fi

if [ "${dir}" = "" ]; then
	echo "No directory provided"
	show_help
	exit 1
fi

if [ ! -d "${dir}" ] || [ ! -x "${dir}" ]; then
	echo "Could not list contents of ${dir}"
	exit 1
fi

umask "${umask}"

for file in ${dir}/*; do
	if [ ! -x "${file}" ] || [ -d "${file}" ]; then
		continue
	fi

	# Check our suffix ignore list
	filename=$(basename "${file}")
	extension="${filename##*.}"
	if echo "${ignore_suffixes}" | grep -qF ",.${extension},"; then
		continue
	fi

	# Match our filter regex
	if echo "${filename}" | grep -qvE "${regex}"; then
		continue
	fi

	if [ "${mode}" = "run" ]; then
		may_fail "${file}" result

		if [ ${result} -ne 0 ]; then
			if [ ${exit_on_error} = "on" ]; then
				echo "Script ${file} failed"
				exit 1
			else
				continue
			fi
		fi
	fi
done<commit_msg>Make option parsing more flexible, allow disabling the regex<commit_after>#!/bin/sh
set -e

COMMAND="${0}"
VERSION="0.0"

DEFAULT_REGEX='^[a-zA-Z0-9_\-\.]+$'
LANANA_REGEX='^[a-z0-9]+$'

# One of "run", "list", and "report"
mode="run"

verbose="off"
reverse="off"
exit_on_error="off"
new_session="off"
regex="${DEFAULT_REGEX}"
umask="022"
dir=""

# Starts with a comma to ease searching
ignore_suffixes=",.rpmsave,.rpmorig,.rpmnew,.swp,.cfsaved,"

# One of "run", "directory", "umask", "regex", or "arg"
parsemode="run"

may_fail() {
	local command="${1}"
	local outvar="${2}"

	set +e
	"${command}"
	eval "${outvar}"=$?
	set -e
}

show_version() {
	echo "run-parts ${VERSION}"
}

show_help() {
	echo "Usage: ${COMMAND} [OPTIONS] [--] DIRECTORY"
	echo "    --test"
	echo "    --list"
	echo "    -v, --verbose"
	echo "    --report"
	echo "    --reverse"
	echo "    --exit-on-error"
	echo "    --new-session"
	echo "    --regex=RE"
	echo "    -u, --umask=UMASK"
	echo "    --ignore-suffixes=SUFFIX1[,SUFFIX2,...]"
	echo "    -h, --help"
	echo "    -V, --version"
}

parse_long_argument() {
	local input="${1}"
	echo $(echo "${input}" | sed "s:--[a-z_\-]\+=::" -)
}

dispatch_parse() {
	case ${arg} in
		--test )
			mode="test"
			;;
		--list )
			mode="list"
			;;
		--report )
			verbose="report"
			;;
		-v | --verbose )
			verbose="verbose"
			;;
		--reverse )
			reverse="on"
			;;
		--exit-on-error )
			exit_on_error="on"
			;;
		-h | --help )
			show_help
			exit 0
			;;
		-V | --version )
			show_version
			exit 0
			;;
		--regex )
			parsemode="regex"
			;;
		--regex=* )
			regex=$(parse_long_argument "${arg}")
			;;
		-u | --umask )
			parsemode="umask"
			;;
		--umask=* )
			umask=$(parse_long_argument "${arg}")
			;;
		-- )
			parsemode="directory"
			;;
		* )
			echo "Unknown argument ${arg}"
			show_help
			exit 1
			;;
	esac
}

gotarg="no"
for arg in $@; do
	gotarg="yes"

	case "${parsemode}" in
		"directory" )
			dir="${arg}"
			;;
		"umask" )
			umask="${arg}"
			parsemode="run"
			;;
		"regex" )
			regex="${arg}"
			parsemode="run"
			;;
		"run" )
			dispatch_parse "${arg}"
			;;
		* )
			echo "Unknown state ${parsemode}"
			exit 1
			;;
	esac

	shift 1
done

if [ ${gotarg} = "no" ]; then
	show_help
	exit 0
fi

if [ "${dir}" = "" ]; then
	echo "No directory provided"
	show_help
	exit 1
fi

if [ ! -d "${dir}" ] || [ ! -x "${dir}" ]; then
	echo "Could not list contents of ${dir}"
	exit 1
fi

umask "${umask}"

for file in ${dir}/*; do
	if [ ! -x "${file}" ] || [ -d "${file}" ]; then
		continue
	fi

	# Check our suffix ignore list
	filename=$(basename "${file}")
	extension="${filename##*.}"
	if echo "${ignore_suffixes}" | grep -qF ",.${extension},"; then
		continue
	fi

	# Match our filter regex, if we have one
	if [ ! -z "${regex}" ]; then
		if echo "${filename}" | grep -qvE "${regex}"; then
			continue
		fi
	fi

	if [ "${mode}" = "run" ]; then
		may_fail "${file}" result

		if [ ${result} -ne 0 ]; then
			if [ ${exit_on_error} = "on" ]; then
				echo "Script ${file} failed"
				exit 1
			else
				continue
			fi
		fi
	fi
done
{'fraction_non_alphanumeric': 0.2174206617150574, 'fraction_numerical': 0.008440243079000676, 'mean_word_length': 2.7008119925046845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '171950', 'n_tokens_mistral': 2695, 'n_tokens_neox': 2385, 'n_words': 571}