Polish CLI help behavior

This commit is contained in:
2026-04-17 09:41:45 +08:00
parent 75ecf7e624
commit 67a743a02b
3 changed files with 23 additions and 5 deletions

View File

@@ -15,8 +15,6 @@ on_error() {
}
trap 'on_error "$?" "$LINENO"' ERR
require_root || exit $?
# shellcheck source=lib/storage.sh
source "${LIB_DIR}/storage.sh"
# shellcheck source=lib/persist.sh
@@ -153,6 +151,14 @@ run_batch() {
}
main() {
case ${1-} in
--help|-h)
usage
return 0
;;
esac
require_root || return $?
bootstrap
if (($# == 0)); then
@@ -161,9 +167,6 @@ main() {
fi
case ${1-} in
--help|-h)
usage
;;
--batch)
shift
run_batch "$@"

View File

@@ -23,6 +23,7 @@ run_test() {
}
run_test "${ROOT_DIR}/tests/test_common.sh"
run_test "${ROOT_DIR}/tests/test_cli.sh"
run_test "${ROOT_DIR}/tests/test_storage.sh"
run_test "${ROOT_DIR}/tests/test_env_check.sh"
run_test "${ROOT_DIR}/tests/test_rules_unit.sh"

14
tests/test_cli.sh Normal file
View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
# shellcheck source=tests/lib/assert.sh
source "${ROOT_DIR}/tests/lib/assert.sh"
help_output=$("${ROOT_DIR}/iptables-forward.sh" --help)
assert_contains "${help_output}" '--batch add <proto> <listen_port> <target_ip> <target_port> <ipver> [desc]' 'main script help should describe batch add'
install_help=$("${ROOT_DIR}/install.sh" --help)
assert_contains "${install_help}" '仅创建符号链接,不复制源码。' 'install help should explain symlink behavior'
pass 'test_cli.sh'