"Fossies" - the Fresh Open Source Software Archive

Member "argbash-2.10.0/bin/argbash-init" (22 Sep 2020, 11644 Bytes) of package /linux/privat/argbash-2.10.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "argbash-init": 2.9.0_vs_2.10.0.

    1 #!/bin/bash
    2 
    3 # shellcheck disable=SC2001,SC2016
    4 # SC2001: See if you can use ${variable//search/replace} instead.
    5 # SC2016: Expressions don't expand in single quotes, use double quotes for that.
    6 
    7 version=2.10.0
    8 # ARG_POSITIONAL_SINGLE([output],[Name of the output template],[-])
    9 # ARG_OPTIONAL_INCREMENTAL([separate],[s],[Separate the parsing logic (specify two times for complete separation)])
   10 # ARG_OPTIONAL_BOOLEAN([hints],[],[Whether to write hints to the script template])
   11 # ARG_OPTIONAL_REPEATED([pos],[],[Add a single-valued positional argument])
   12 # ARG_OPTIONAL_REPEATED([opt],[],[Add an single-valued optional argument])
   13 # ARG_OPTIONAL_REPEATED([opt-bool],[],[Add an optional boolean argument])
   14 # ARG_OPTIONAL_REPEATED([wrap],[],[What script(s) to wrap])
   15 # ARG_OPTIONAL_SINGLE([mode],[m],[The slider between feature-rich and simple script.],[default])
   16 # ARG_TYPE_GROUP_SET([mode],[MODE],[mode],[default,full,minimal])
   17 # ARG_VERSION([echo "argbash-init v$version"])
   18 # ARG_HELP([Make a template for scripts.])
   19 
   20 # ARGBASH_GO()
   21 # needed because of Argbash --> m4_ignore([
   22 ### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
   23 # Argbash is a bash code generator used to get arguments parsing right.
   24 # Argbash is FREE SOFTWARE, see https://argbash.io for more info
   25 
   26 
   27 die()
   28 {
   29     local _ret="${2:-1}"
   30     test "${_PRINT_HELP:-no}" = yes && print_help >&2
   31     echo "$1" >&2
   32     exit "${_ret}"
   33 }
   34 
   35 # validators
   36 
   37 mode()
   38 {
   39     local _allowed=("default" "full" "minimal") _seeking="$1"
   40     for element in "${_allowed[@]}"
   41     do
   42         test "$element" = "$_seeking" && echo "$element" && return 0
   43     done
   44     die "Value '$_seeking' (of argument '$2') doesn't match the list of allowed values: 'default', 'full' and 'minimal'" 4
   45 }
   46 
   47 
   48 begins_with_short_option()
   49 {
   50     local first_option all_short_options='smvh'
   51     first_option="${1:0:1}"
   52     test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
   53 }
   54 
   55 # THE DEFAULTS INITIALIZATION - POSITIONALS
   56 _positionals=()
   57 _arg_output="-"
   58 # THE DEFAULTS INITIALIZATION - OPTIONALS
   59 _arg_separate=0
   60 _arg_hints="off"
   61 _arg_pos=()
   62 _arg_opt=()
   63 _arg_opt_bool=()
   64 _arg_wrap=()
   65 _arg_mode="default"
   66 
   67 
   68 print_help()
   69 {
   70     printf '%s\n' "Make a template for scripts."
   71     printf 'Usage: %s [-s|--separate] [--(no-)hints] [--pos <arg>] [--opt <arg>] [--opt-bool <arg>] [--wrap <arg>] [-m|--mode <MODE>] [-v|--version] [-h|--help] [<output>]\n' "$0"
   72     printf '\t%s\n' "<output>: Name of the output template (default: '-')"
   73     printf '\t%s\n' "-s, --separate: Separate the parsing logic (specify two times for complete separation)"
   74     printf '\t%s\n' "--hints, --no-hints: Whether to write hints to the script template (off by default)"
   75     printf '\t%s\n' "--pos: Add a single-valued positional argument (empty by default)"
   76     printf '\t%s\n' "--opt: Add an single-valued optional argument (empty by default)"
   77     printf '\t%s\n' "--opt-bool: Add an optional boolean argument (empty by default)"
   78     printf '\t%s\n' "--wrap: What script(s) to wrap (empty by default)"
   79     printf '\t%s\n' "-m, --mode: The slider between feature-rich and simple script.. Can be one of: 'default', 'full' and 'minimal' (default: 'default')"
   80     printf '\t%s\n' "-v, --version: Prints version"
   81     printf '\t%s\n' "-h, --help: Prints help"
   82 }
   83 
   84 
   85 parse_commandline()
   86 {
   87     _positionals_count=0
   88     while test $# -gt 0
   89     do
   90         _key="$1"
   91         case "$_key" in
   92             -s|--separate)
   93                 _arg_separate=$((_arg_separate + 1))
   94                 ;;
   95             -s*)
   96                 _arg_separate=$((_arg_separate + 1))
   97                 _next="${_key##-s}"
   98                 if test -n "$_next" -a "$_next" != "$_key"
   99                 then
  100                     { begins_with_short_option "$_next" && shift && set -- "-s" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
  101                 fi
  102                 ;;
  103             --no-hints|--hints)
  104                 _arg_hints="on"
  105                 test "${1:0:5}" = "--no-" && _arg_hints="off"
  106                 ;;
  107             --pos)
  108                 test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
  109                 _arg_pos+=("$2")
  110                 shift
  111                 ;;
  112             --pos=*)
  113                 _arg_pos+=("${_key##--pos=}")
  114                 ;;
  115             --opt)
  116                 test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
  117                 _arg_opt+=("$2")
  118                 shift
  119                 ;;
  120             --opt=*)
  121                 _arg_opt+=("${_key##--opt=}")
  122                 ;;
  123             --opt-bool)
  124                 test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
  125                 _arg_opt_bool+=("$2")
  126                 shift
  127                 ;;
  128             --opt-bool=*)
  129                 _arg_opt_bool+=("${_key##--opt-bool=}")
  130                 ;;
  131             --wrap)
  132                 test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
  133                 _arg_wrap+=("$2")
  134                 shift
  135                 ;;
  136             --wrap=*)
  137                 _arg_wrap+=("${_key##--wrap=}")
  138                 ;;
  139             -m|--mode)
  140                 test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
  141                 _arg_mode="$(mode "$2" "mode")" || exit 1
  142                 shift
  143                 ;;
  144             --mode=*)
  145                 _arg_mode="$(mode "${_key##--mode=}" "mode")" || exit 1
  146                 ;;
  147             -m*)
  148                 _arg_mode="$(mode "${_key##-m}" "mode")" || exit 1
  149                 ;;
  150             -v|--version)
  151                 echo "argbash-init v$version"
  152                 exit 0
  153                 ;;
  154             -v*)
  155                 echo "argbash-init v$version"
  156                 exit 0
  157                 ;;
  158             -h|--help)
  159                 print_help
  160                 exit 0
  161                 ;;
  162             -h*)
  163                 print_help
  164                 exit 0
  165                 ;;
  166             *)
  167                 _last_positional="$1"
  168                 _positionals+=("$_last_positional")
  169                 _positionals_count=$((_positionals_count + 1))
  170                 ;;
  171         esac
  172         shift
  173     done
  174 }
  175 
  176 
  177 handle_passed_args_count()
  178 {
  179     test "${_positionals_count}" -le 1 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect between 0 and 1, but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
  180 }
  181 
  182 
  183 assign_positional_args()
  184 {
  185     local _positional_name _shift_for=$1
  186     _positional_names="_arg_output "
  187 
  188     shift "$_shift_for"
  189     for _positional_name in ${_positional_names}
  190     do
  191         test $# -gt 0 || break
  192         eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
  193         shift
  194     done
  195 }
  196 
  197 parse_commandline "$@"
  198 handle_passed_args_count
  199 assign_positional_args 1 "${_positionals[@]}"
  200 
  201 # OTHER STUFF GENERATED BY Argbash
  202 # Validation of values
  203 
  204 
  205 ### END OF CODE GENERATED BY Argbash (sortof) ### ])
  206 # [ <-- needed because of Argbash
  207 
  208 
  209 _variables=()
  210 HAVE_POSITIONAL_ARG=no
  211 
  212 
  213 # This should be in sync with _translit_var in stuff.m4
  214 _translit_var()
  215 {
  216     printf "\$_arg_%s" "$1" | tr '[:upper:]' '[:lower:]' | tr '-' '_'
  217 }
  218 
  219 
  220 optional_argument_without_hints()
  221 {
  222     echo "# ARG_OPTIONAL_SINGLE([$1])"
  223 }
  224 
  225 
  226 optional_argument_with_hints()
  227 {
  228     echo "# ARG_OPTIONAL_SINGLE([$1], [<short option character (optional)>], [<help message (optional)>], [<default (optional)>])"
  229 }
  230 
  231 
  232 optional_argument()
  233 {
  234     "${FUNCNAME[0]}_$2" "$1"
  235     _variables+=("printf 'Value of --%s: %s\\n' '$1' \"$(_translit_var "$1")\"")
  236 }
  237 
  238 
  239 boolean_argument_with_hints()
  240 {
  241     echo "# ARG_OPTIONAL_BOOLEAN([$1], [<short option character (optional)>], [<help message (optional)>], [<default (optional), off by default>])"
  242 }
  243 
  244 
  245 boolean_argument_without_hints()
  246 {
  247     echo "# ARG_OPTIONAL_BOOLEAN([$1])"
  248 }
  249 
  250 
  251 boolean_argument()
  252 {
  253     "${FUNCNAME[0]}_$2" "$1"
  254     _variables+=("printf \"'%s' is %s\\\\n\" '$1' \"$(_translit_var "$1")\"")
  255 }
  256 
  257 
  258 positional_argument_with_hints()
  259 {
  260     echo "# ARG_POSITIONAL_SINGLE([$1], [<help message (optional)>], [<default (optional)])"
  261 }
  262 
  263 
  264 positional_argument_without_hints()
  265 {
  266     echo "# ARG_POSITIONAL_SINGLE([$1])"
  267 }
  268 
  269 
  270 positional_argument()
  271 {
  272     HAVE_POSITIONAL_ARG=yes
  273     "${FUNCNAME[0]}_$2" "$1"
  274     _variables+=("printf \"Value of '%s': %s\\\\n\" '$1' \"$(_translit_var "$1")\"")
  275 }
  276 
  277 
  278 do_header()
  279 {
  280     echo "#!/bin/bash"
  281     echo
  282     # We if separate == 2, we don't want to pass this to argbash at all
  283     test "$_arg_separate" = 2 && test "$1" = "script" && echo "# Created by argbash-init v$version" && return
  284     echo "# m4_ignore("
  285     if test "$1" = "lib"
  286     then
  287         echo "echo \"This is just a parsing library template, not the library - pass the parent script '$outfname' to 'argbash' to fix this.\" >&2"
  288     elif test "$1" = "standalone_lib"
  289     then
  290         echo "echo \"This is just a parsing library template, not the library - pass this file to 'argbash' to fix this.\" >&2"
  291     else
  292         echo "echo \"This is just a script template, not the script (yet) - pass it to 'argbash' to fix this.\" >&2"
  293     fi
  294     echo "exit 11  #)Created by argbash-init v$version"
  295 }
  296 
  297 
  298 do_args()
  299 {
  300     local _mode="without_hints"
  301     if test "$_arg_hints" = on
  302     then
  303         echo "# Rearrange the order of options below according to what you would like to see in the help message."
  304         _mode="with_hints"
  305     fi
  306     for name in "${_arg_opt[@]}"
  307     do optional_argument "$name" "$_mode"; done
  308     for name in "${_arg_opt_bool[@]}"
  309     do boolean_argument "$name" "$_mode"; done
  310     for name in "${_arg_pos[@]}"
  311     do positional_argument "$name" "$_mode"; done
  312 }
  313 
  314 
  315 do_args_footer()
  316 {
  317     if test "$_arg_mode" = "full"
  318     then
  319         echo '# ARGBASH_SET_DELIM([ =])'
  320         echo '# ARG_OPTION_STACKING([getopt])'
  321         echo '# ARG_RESTRICT_VALUES([no-local-options])'
  322     elif test "$_arg_mode" = "minimal"
  323     then
  324         echo '# ARGBASH_SET_DELIM([ ])'
  325         echo '# ARG_OPTION_STACKING([none])'
  326         echo '# ARG_RESTRICT_VALUES([none])'
  327     fi
  328     test "$HAVE_POSITIONAL_ARG" = yes && echo '# ARG_DEFAULTS_POS'
  329     echo "# ARG_HELP([<The general help message of my script>])"
  330     echo "# ARGBASH_GO"
  331 }
  332 
  333 
  334 do_script_assisted()
  335 {
  336     do_header script
  337 
  338     echo "# DEFINE_SCRIPT_DIR()"
  339     echo "# INCLUDE_PARSING_CODE([$(basename "${parse_fname_stem}.sh")])"
  340     echo "# ARGBASH_GO"
  341 
  342     do_body_protected
  343 }
  344 
  345 
  346 #
  347 # $1: The filename of the parsing library
  348 do_script_separate()
  349 {
  350     do_header script
  351     parse_fname=${parse_fname_stem}.sh
  352 
  353     echo "# Run 'argbash --strip user-content \"$1\" -o \"$parse_fname\"' to generate the '$parse_fname' file."
  354     echo "# If you need to make changes later, edit '$parse_fname' directly, and regenerate by running"
  355     echo "# 'argbash --strip user-content \"$parse_fname\" -o \"$parse_fname\"'"
  356     echo 'script_dir="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"'
  357     echo '. "${script_dir}/'"$(basename "$parse_fname")\" || { echo \"Couldn't find '$(basename "$parse_fname")' parsing library in the '"'$script_dir'"' directory\"; exit 1; }"
  358     echo
  359 
  360     do_body
  361 }
  362 
  363 
  364 do_body()
  365 {
  366     echo "# vvv  PLACE YOUR CODE HERE  vvv"
  367     if test "${#_variables[@]}" -gt 0; then
  368         echo "# For example:"
  369         for stat in "${_variables[@]}"
  370         do
  371             echo "$stat"
  372         done
  373         echo
  374     else
  375         echo
  376         echo "# PUT YOUR CODE HERE"
  377         echo
  378     fi
  379     echo "# ^^^  TERMINATE YOUR CODE BEFORE THE BOTTOM ARGBASH MARKER  ^^^"
  380 }
  381 
  382 
  383 do_body_protected()
  384 {
  385     echo
  386     echo "# [ <-- needed because of Argbash"
  387     echo
  388     do_body
  389     echo
  390     echo "# ] <-- needed because of Argbash"
  391 }
  392 
  393 
  394 do_stuff()
  395 {
  396     do_header "$1"
  397     do_args
  398     do_args_footer
  399 
  400     test "$_arg_separate" = 0 && do_body_protected
  401 }
  402 
  403 outfname="$_arg_output"
  404 # we canonicize the empty string input to output filename to the dash
  405 test -n "$outfname" || outfname='-'
  406 test "$outfname" = "-" -a "$_arg_separate" -gt 0 && die "If you want to separate parsing and script body, you have to specify the outname, stdout doesn't work."
  407 
  408 if test "$outfname" = '-'
  409 then
  410     do_stuff 'script'
  411 else
  412     if test "$_arg_separate" = 0
  413     then
  414         do_stuff 'script' > "$outfname"
  415     else
  416         parse_fname_stem="$(echo "${outfname}" | sed -e 's/\.\(sh\|m4\)$//')-parsing"
  417 
  418         # IMPORTANT NOTION:
  419         # do_stuff has to be called FIRST, because it sets the _variables array content as its side-effect
  420         if test "$_arg_separate" = 1
  421         then
  422             do_stuff 'lib' > "${parse_fname_stem}.m4"
  423             do_script_assisted > "$outfname"
  424         else
  425             test "$_arg_separate" = 2 || echo "The greatest separation value is 2, got $_arg_separate" >&2
  426             parsing_library_file="${parse_fname_stem}.m4"
  427             do_stuff 'standalone_lib'  > "${parsing_library_file}"
  428             do_script_separate "$parsing_library_file" > "$outfname"
  429         fi
  430     fi
  431     chmod a+x "$outfname"
  432 fi
  433 
  434 #
  435 # ] <-- needed because of Argbash