From f844160d02df6314b7f008e11e499e1c8ce55a14 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Sat, 10 Dec 2022 23:12:07 +0100 Subject: [PATCH] sequencer - output only step help with -h and use less if output is longer than one line --- sequencer.sh | 84 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 33 deletions(-) diff --git a/sequencer.sh b/sequencer.sh index 1e920d7..82e69c2 100755 --- a/sequencer.sh +++ b/sequencer.sh @@ -1030,10 +1030,10 @@ createTemplate() { exe cp "${sqr_origin}/${seq_template}" "${seq_template}" } -# displayHelp [NO TEMPLATE] [STEP NUMBER OR ALIAS] -# [NO TEMPLATE] -# 0 (default) : Ask for template creation -# 1 : Do not ask for template creation +# displayHelp [OPTIONS] [STEP NUMBER OR ALIAS] +# [OPTIONS] +# -t : Ask for template creation +# -s : Display only step help # [STEP NUMBER OR ALIAS] # [NO TEMPLATE] must be set # Display step info function only for given step @@ -1044,45 +1044,52 @@ displayHelp() { sqr::debugPause local i local answer + local arg + local fullHelp=1 local clusterSize=0 local lastClusterSize=0 - local createTemplate=1 + local createTemplate=0 local stepFound=0 local loopStart=0 local loopEnd="${_sqr_stepMax}" _sqr_contextHelp=1 - # check if help is requested for a single step - if [ -n "${2:-}" ]; then - parseAlias - loopStart="$(checkStep "$2")" - fi - - if [[ "$loopStart" == "0" ]] ; then - helpSequencer - loopStart=1 - else - # Output loop only for one step - loopEnd=$loopStart - fi - - if [ -n "${1:-}" ] && [[ ${1:-} -eq 1 ]] ; then - createTemplate=0 - fi - + while getopts "st" arg; do + case "${arg}" in + s) fullHelp=0 ;; + t) createTemplate=1 ;; + *) ;; + esac + done + shift "$((OPTIND-1))"; OPTIND=1 + # check if step definition exists by looking for a step_*() function for ((i=1; i<=_sqr_stepMax; i++)); do if ! exists -f "step_${i}" ; then continue fi - stepFound=$i + stepFound=${i} break done - if [[ "$stepFound" -eq 0 ]] ; then + # check if help is requested for a single step + if [ -n "${1:-}" ]; then + parseAlias + loopStart="$(checkStep "${1}")" + fi + + if (( ! loopStart )) ; then + (( fullHelp )) || (( ! stepFound )) && helpSequencer + loopStart=1 + else + # Output loop only for one step + loopEnd=${loopStart} + fi + + if (( ! stepFound )) ; then printf '\n %s\n' "It seems ${0##*/} was called directly." printf ' %s\n\n' "Please create a sequence script first." - if [ $createTemplate -ne 0 ] ; then + if (( createTemplate )) ; then if confirm " Create a template now?" ; then if createTemplate ; then printf '\n %s\n' "${seq_template} created." @@ -1100,15 +1107,15 @@ displayHelp() { # Display step reference in help if step function exists if ! exists -f "step_${i}" ; then - if [ $clusterSize -ne 0 ] ; then + if (( clusterSize )) ; then # Mark the end of a cluster lastClusterSize=$clusterSize clusterSize=0 fi continue fi - ((clusterSize+=1)) - if [ $lastClusterSize -gt 1 ] ; then + (( clusterSize+=1 )) + if [[ $lastClusterSize -gt 1 ]] ; then # Add separation at the end of a cluster lastClusterSize=0 echo @@ -1530,7 +1537,10 @@ sqr::main() { sqr_args+=" $1" _sqr_dry=1 shift ;; - --help|-h) # show only help + --help) # show only full help + local _sqr_optHelp=2 + shift ;; + -h) # show only step help local _sqr_optHelp=1 shift ;; --helpapi|-ha) #show build-in functions @@ -1577,8 +1587,16 @@ sqr::main() { esac done - ((${_sqr_optHelp:-})) && displayHelp 1 "${1:-}" && exit 0 - ((${_sqr_optHelpapi:-})) && helpApi | less -R && exit 0 + if (( ${_sqr_optHelp:-} )) ; then + if [[ ${_sqr_optHelp} == 2 ]] ; then + _sqr_optHelp="" + else + _sqr_optHelp="-s" + fi + displayHelp ${_sqr_optHelp:-} "${1:-}" | less -FREX + exit 0 + fi + (( ${_sqr_optHelpapi:-} )) && helpApi | less -R && exit 0 # debug mode if [[ "${_sqr_debug:-}" = "1" ]]; then @@ -1594,7 +1612,7 @@ sqr::main() { if [ -z "${1:-}" ] && (( ! quickStartOne )) ; then if ! quiet && [[ ${LOG_LEVEL} -ge $log_info ]] ; then # Empty -> show help - displayHelp + displayHelp -st fi # Assume starting at one for interactive mode toStart=( "1" )