"Fossies" - the Fresh Open Source Software Archive

Member "argbash-2.10.0/src/argbash-init.m4" (22 Sep 2020, 6558 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.m4": 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=_ARGBASH_VERSION
    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 
   22 
   23 _variables=()
   24 HAVE_POSITIONAL_ARG=no
   25 
   26 
   27 # This should be in sync with _translit_var in stuff.m4
   28 _translit_var()
   29 {
   30     printf "\$_arg_%s" "$1" | tr '[:upper:]' '[:lower:]' | tr '-' '_'
   31 }
   32 
   33 
   34 optional_argument_without_hints()
   35 {
   36     echo "# ARG_OPTIONAL_SINGLE([$1])"
   37 }
   38 
   39 
   40 optional_argument_with_hints()
   41 {
   42     echo "# ARG_OPTIONAL_SINGLE([$1], [<short option character (optional)>], [<help message (optional)>], [<default (optional)>])"
   43 }
   44 
   45 
   46 optional_argument()
   47 {
   48     "${FUNCNAME[0]}_$2" "$1"
   49     _variables+=("printf 'Value of --%s: %s\\n' '$1' \"$(_translit_var "$1")\"")
   50 }
   51 
   52 
   53 boolean_argument_with_hints()
   54 {
   55     echo "# ARG_OPTIONAL_BOOLEAN([$1], [<short option character (optional)>], [<help message (optional)>], [<default (optional), off by default>])"
   56 }
   57 
   58 
   59 boolean_argument_without_hints()
   60 {
   61     echo "# ARG_OPTIONAL_BOOLEAN([$1])"
   62 }
   63 
   64 
   65 boolean_argument()
   66 {
   67     "${FUNCNAME[0]}_$2" "$1"
   68     _variables+=("printf \"'%s' is %s\\\\n\" '$1' \"$(_translit_var "$1")\"")
   69 }
   70 
   71 
   72 positional_argument_with_hints()
   73 {
   74     echo "# ARG_POSITIONAL_SINGLE([$1], [<help message (optional)>], [<default (optional)])"
   75 }
   76 
   77 
   78 positional_argument_without_hints()
   79 {
   80     echo "# ARG_POSITIONAL_SINGLE([$1])"
   81 }
   82 
   83 
   84 positional_argument()
   85 {
   86     HAVE_POSITIONAL_ARG=yes
   87     "${FUNCNAME[0]}_$2" "$1"
   88     _variables+=("printf \"Value of '%s': %s\\\\n\" '$1' \"$(_translit_var "$1")\"")
   89 }
   90 
   91 
   92 do_header()
   93 {
   94     echo "#!/bin/bash"
   95     echo
   96     # We if separate == 2, we don't want to pass this to argbash at all
   97     test "$_arg_separate" = 2 && test "$1" = "script" && echo "# Created by argbash-init v$version" && return
   98     echo "# m4_ignore("
   99     if test "$1" = "lib"
  100     then
  101         echo "echo \"This is just a parsing library template, not the library - pass the parent script '$outfname' to 'argbash' to fix this.\" >&2"
  102     elif test "$1" = "standalone_lib"
  103     then
  104         echo "echo \"This is just a parsing library template, not the library - pass this file to 'argbash' to fix this.\" >&2"
  105     else
  106         echo "echo \"This is just a script template, not the script (yet) - pass it to 'argbash' to fix this.\" >&2"
  107     fi
  108     echo "exit 11  #)Created by argbash-init v$version"
  109 }
  110 
  111 
  112 do_args()
  113 {
  114     local _mode="without_hints"
  115     if test "$_arg_hints" = on
  116     then
  117         echo "# Rearrange the order of options below according to what you would like to see in the help message."
  118         _mode="with_hints"
  119     fi
  120     for name in "${_arg_opt[@]}"
  121     do optional_argument "$name" "$_mode"; done
  122     for name in "${_arg_opt_bool[@]}"
  123     do boolean_argument "$name" "$_mode"; done
  124     for name in "${_arg_pos[@]}"
  125     do positional_argument "$name" "$_mode"; done
  126 }
  127 
  128 
  129 do_args_footer()
  130 {
  131     if test "$_arg_mode" = "full"
  132     then
  133         echo '# ARGBASH_SET_DELIM([ =])'
  134         echo '# ARG_OPTION_STACKING([getopt])'
  135         echo '# ARG_RESTRICT_VALUES([no-local-options])'
  136     elif test "$_arg_mode" = "minimal"
  137     then
  138         echo '# ARGBASH_SET_DELIM([ ])'
  139         echo '# ARG_OPTION_STACKING([none])'
  140         echo '# ARG_RESTRICT_VALUES([none])'
  141     fi
  142     test "$HAVE_POSITIONAL_ARG" = yes && echo '# ARG_DEFAULTS_POS'
  143     echo "# ARG_HELP([<The general help message of my script>])"
  144     echo "# ARGBASH_GO"
  145 }
  146 
  147 
  148 do_script_assisted()
  149 {
  150     do_header script
  151 
  152     echo "# DEFINE_SCRIPT_DIR()"
  153     echo "# INCLUDE_PARSING_CODE([$(basename "${parse_fname_stem}.sh")])"
  154     echo "# ARGBASH_GO"
  155 
  156     do_body_protected
  157 }
  158 
  159 
  160 #
  161 # $1: The filename of the parsing library
  162 do_script_separate()
  163 {
  164     do_header script
  165     parse_fname=${parse_fname_stem}.sh
  166 
  167     echo "# Run 'argbash --strip user-content \"$1\" -o \"$parse_fname\"' to generate the '$parse_fname' file."
  168     echo "# If you need to make changes later, edit '$parse_fname' directly, and regenerate by running"
  169     echo "# 'argbash --strip user-content \"$parse_fname\" -o \"$parse_fname\"'"
  170     echo 'script_dir="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"'
  171     echo '. "${script_dir}/'"$(basename "$parse_fname")\" || { echo \"Couldn't find '$(basename "$parse_fname")' parsing library in the '"'$script_dir'"' directory\"; exit 1; }"
  172     echo
  173 
  174     do_body
  175 }
  176 
  177 
  178 do_body()
  179 {
  180     echo "# vvv  PLACE YOUR CODE HERE  vvv"
  181     if test "${#_variables[@]}" -gt 0; then
  182         echo "# For example:"
  183         for stat in "${_variables[@]}"
  184         do
  185             echo "$stat"
  186         done
  187         echo
  188     else
  189         echo
  190         echo "# PUT YOUR CODE HERE"
  191         echo
  192     fi
  193     echo "# ^^^  TERMINATE YOUR CODE BEFORE THE BOTTOM ARGBASH MARKER  ^^^"
  194 }
  195 
  196 
  197 do_body_protected()
  198 {
  199     echo
  200     echo "# [ <-- needed because of Argbash"
  201     echo
  202     do_body
  203     echo
  204     echo "# ] <-- needed because of Argbash"
  205 }
  206 
  207 
  208 do_stuff()
  209 {
  210     do_header "$1"
  211     do_args
  212     do_args_footer
  213 
  214     test "$_arg_separate" = 0 && do_body_protected
  215 }
  216 
  217 outfname="$_arg_output"
  218 # we canonicize the empty string input to output filename to the dash
  219 test -n "$outfname" || outfname='-'
  220 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."
  221 
  222 if test "$outfname" = '-'
  223 then
  224     do_stuff 'script'
  225 else
  226     if test "$_arg_separate" = 0
  227     then
  228         do_stuff 'script' > "$outfname"
  229     else
  230         parse_fname_stem="$(echo "${outfname}" | sed -e 's/\.\(sh\|m4\)$//')-parsing"
  231 
  232         # IMPORTANT NOTION:
  233         # do_stuff has to be called FIRST, because it sets the _variables array content as its side-effect
  234         if test "$_arg_separate" = 1
  235         then
  236             do_stuff 'lib' > "${parse_fname_stem}.m4"
  237             do_script_assisted > "$outfname"
  238         else
  239             test "$_arg_separate" = 2 || echo "The greatest separation value is 2, got $_arg_separate" >&2
  240             parsing_library_file="${parse_fname_stem}.m4"
  241             do_stuff 'standalone_lib'  > "${parsing_library_file}"
  242             do_script_separate "$parsing_library_file" > "$outfname"
  243         fi
  244     fi
  245     chmod a+x "$outfname"
  246 fi
  247 
  248 # ]dnl
  249 
  250 dnl vim: filetype=sh