Skip to content

Commit

Permalink
Add help mode
Browse files Browse the repository at this point in the history
  • Loading branch information
xshoji committed Jul 26, 2018
1 parent d64ef92 commit fd981f9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
10 changes: 6 additions & 4 deletions ScriptStarter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cat << "_EOT_"
_EOT_
cat << _EOT_
Usage:
./$(basename "$0") --naming scriptName --author name [ --required paramName,sample --require ... --option paramName,sample --option ... --flag flagName --flag ... --env varName,sample --env ... ]
./$(basename "$0") --naming scriptName --author name [ --description "ScriptStarter's description here." --required paramName,sample --require ... --option paramName,sample --option ... --flag flagName --flag ... --env varName,sample --env ... --short]
Description:
This script generates a template of bash script tool.
Expand All @@ -20,11 +20,11 @@ cat << _EOT_
--author,-a authorName : Script author.
Optional parameters:
--description,-d "Description" : Description of this script. [ example: --description "ScriptStarter's description here." ]
--required,-r paramName,sample : Required parameter setting. [ example: --required id,1001 ]
--option,-o paramName,sample : Optional parameter setting. [ example: --option name,John ]
--option,-o paramName,sample : Optional parameter setting. [ example: --option name,xshoji ]
--flag,-f paramName : Optional flag parameter setting. [ example: --flag dryRun ]
--env,-e varName,sample : Required environment variable. [ example: --env API_HOST,example.com ]
--description,-d "Description" : Description of this script. [ example: --description "This is my script." ]
--short,-s : Enable short parameter. [ example: --short ]
_EOT_
Expand Down Expand Up @@ -385,6 +385,7 @@ for ARG in "$@"
do
SHIFT="true"
[ "${ARG}" == "--debug" ] && { shift 1; set -eux; SHIFT="false"; }
([ "${ARG}" == "--help" ] || [ "${ARG}" == "-h" ]) && { shift 1; IS_HELP="true"; SHIFT="false"; }
__EOT__

printParseArgument ${ARGS_REQUIRED[@]+"${ARGS_REQUIRED[@]}"}
Expand All @@ -400,8 +401,9 @@ __EOT__
[ ${#ARGS_REQUIRED[@]} -gt 0 ] && { echo "# Check required parameters"; }
printCheckRequiredArgument ${ARGS_REQUIRED[@]+"${ARGS_REQUIRED[@]}"}
echo '[ ! -z "${INVALID_STATE+x}" ] && { usage; exit 1; }'
echo '[ ! -z "${IS_HELP+x}" ] && { usage; exit 0; }'
if [ ${#ARGS_OPTIONAL[@]} -gt 0 ] || [ ${#ARGS_FLAG[@]} -gt 0 ]; then
echo "# Set default"
echo "# Initialize optional variables"
fi
printSetDefaultArgument ${ARGS_OPTIONAL[@]+"${ARGS_OPTIONAL[@]}"}
printSetDefaultArgument ${ARGS_FLAG[@]+"${ARGS_FLAG[@]}"}
Expand Down
86 changes: 44 additions & 42 deletions ScriptStarterTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,27 @@ echo ${COUNT}". ok"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --naming test --author user > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
${TEST_FILE}

echo ""
echo "================="
echo ${COUNT}". execute generated tool"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --naming test --author user --required aaa,aaa > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
bash ${TEST_FILE} --aaa aaa
${TEST_FILE}
${TEST_FILE} --aaa aaa

echo ""
echo "================="
echo ${COUNT}". execute generated tool with env"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --naming test --author user --required aaa,aaa --env TEST,test > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
bash ${TEST_FILE} --aaa aaa
${TEST_FILE}
${TEST_FILE} --aaa aaa
export TEST=test
bash ${TEST_FILE} --aaa aaa
${TEST_FILE} --aaa aaa
unset TEST

echo ""
Expand All @@ -92,81 +92,83 @@ echo ${COUNT}". execute generated tool with option"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --naming test --author user --required aaa,aaa --option bbb,bbb > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
bash ${TEST_FILE} --aaa aaa
bash ${TEST_FILE} --aaa aaa --bbb bbb
${TEST_FILE}
${TEST_FILE} --aaa aaa
${TEST_FILE} --aaa aaa --bbb bbb

echo ""
echo "================="
echo ${COUNT}". execute generated tool with option and flag"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --naming test --author user --required aaa,aaa --option bbb,bbb --flag ccc > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
bash ${TEST_FILE} --aaa aaa
bash ${TEST_FILE} --aaa aaa --bbb bbb
bash ${TEST_FILE} --aaa aaa --bbb bbb --ccc
${TEST_FILE}
${TEST_FILE} --aaa aaa
${TEST_FILE} --aaa aaa --bbb bbb
${TEST_FILE} --aaa aaa --bbb bbb --ccc

echo ""
echo "================="
echo ${COUNT}". execute generated tool without required parameter"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --naming test --author user --option bbb,bbb --flag ccc > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
bash ${TEST_FILE} --bbb bbb
bash ${TEST_FILE} --bbb bbb --ccc
${TEST_FILE}
${TEST_FILE} --bbb bbb
${TEST_FILE} --bbb bbb --ccc

echo ""
echo "================="
echo ${COUNT}". execute generated tool strange order parameters"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --xxx --flag ccc --yyy --option bbb,bbb --required xxx,xxx --naming test --author user > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
bash ${TEST_FILE} --xxx xxx
bash ${TEST_FILE} --bbb bbb --xxx xxx
bash ${TEST_FILE} --bbb bbb --ccc --xxx xxx
bash ${TEST_FILE} --debug
bash ${TEST_FILE} --debug --xxx xxx
${TEST_FILE}
${TEST_FILE} --xxx xxx
${TEST_FILE} --bbb bbb --xxx xxx
${TEST_FILE} --bbb bbb --ccc --xxx xxx
${TEST_FILE} --debug
${TEST_FILE} --debug --xxx xxx

echo ""
echo "================="
echo ${COUNT}". execute generated tool strange order parameters shorten"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --xxx -f ccc --yyy -o bbb,bbb -r xxx,xxx -n test -a user > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
bash ${TEST_FILE} --xxx xxx
bash ${TEST_FILE} --bbb bbb --xxx xxx
bash ${TEST_FILE} --bbb bbb --ccc --xxx xxx
bash ${TEST_FILE} --debug
bash ${TEST_FILE} --debug --xxx xxx
${TEST_FILE}
${TEST_FILE} --xxx xxx
${TEST_FILE} --bbb bbb --xxx xxx
${TEST_FILE} --bbb bbb --ccc --xxx xxx
${TEST_FILE} --debug
${TEST_FILE} --debug --xxx xxx

echo ""
echo "================="
echo ${COUNT}". execute generated tool strange order parameters shorten"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --xxx -f ccc --yyy -o bbb,bbb -r xxx,xxx -n test -a user -s > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
bash ${TEST_FILE} --xxx xxx
bash ${TEST_FILE} -b bbb -x xxx
bash ${TEST_FILE} -b bbb -c -x x
bash ${TEST_FILE} --debug
bash ${TEST_FILE} --debug -x xxx
${TEST_FILE}
${TEST_FILE} --xxx xxx
${TEST_FILE} -b bbb -x xxx
${TEST_FILE} -b bbb -c -x x
${TEST_FILE} --debug
${TEST_FILE} --debug -x xxx

echo ""
echo "================="
echo ${COUNT}". execute generated tool strange order parameters shorten with description"
COUNT=$(( COUNT + 1 ))
./${SCRIPT_PATH} --xxx -f ccc --yyy -o bbb,bbb -r xxx,xxx -n test -a user -s -d "Test script" > ${TEST_FILE}
chmod 777 ${TEST_FILE}
bash ${TEST_FILE}
bash ${TEST_FILE} --xxx xxx
bash ${TEST_FILE} -b bbb -x xxx
bash ${TEST_FILE} -b bbb -c -x x
bash ${TEST_FILE} --debug
bash ${TEST_FILE} --debug -x xxx

rm -rf ${TEST_FILE}
${TEST_FILE}
${TEST_FILE} --xxx xxx
${TEST_FILE} -b bbb -x xxx
${TEST_FILE} -b bbb -c -x x
${TEST_FILE} --debug
${TEST_FILE} --debug -x xxx
${TEST_FILE} -x xxx -h
${TEST_FILE} -x xxx --help

# rm -rf ${TEST_FILE}

0 comments on commit fd981f9

Please sign in to comment.