"Fossies" - the Fresh Open Source Software Archive

Member "argbash-2.10.0/bin/argbash-1to2" (22 Sep 2020, 4853 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-1to2": 2.9.0_vs_2.10.0.

    1 #!/bin/bash
    2 
    3 # shellcheck disable=SC2016
    4 # SC2016: Expressions don't expand in single quotes, use double quotes for that.
    5 
    6 version=2.10.0
    7 # ARG_POSITIONAL_INF([input],[The input file to transform],[1])
    8 # ARG_OPTIONAL_SINGLE([output],[o],[Name of the output file (pass '-' for stdout and empty string for the same as input file)],[""])
    9 # ARG_VERSION([echo "argbash-1to2 v$version"])
   10 # ARG_HELP([Convert a template for argbash>=1,<2 to argbash>=2,<3])
   11 
   12 # ARGBASH_GO()
   13 # needed because of Argbash --> m4_ignore([
   14 ### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
   15 # Argbash is a bash code generator used to get arguments parsing right.
   16 # Argbash is FREE SOFTWARE, see https://argbash.io for more info
   17 
   18 
   19 die()
   20 {
   21     local _ret="${2:-1}"
   22     test "${_PRINT_HELP:-no}" = yes && print_help >&2
   23     echo "$1" >&2
   24     exit "${_ret}"
   25 }
   26 
   27 
   28 begins_with_short_option()
   29 {
   30     local first_option all_short_options='ovh'
   31     first_option="${1:0:1}"
   32     test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
   33 }
   34 
   35 # THE DEFAULTS INITIALIZATION - POSITIONALS
   36 _positionals=()
   37 _arg_input=('' )
   38 # THE DEFAULTS INITIALIZATION - OPTIONALS
   39 _arg_output=""
   40 
   41 
   42 print_help()
   43 {
   44     printf '%s\n' "Convert a template for argbash>=1,<2 to argbash>=2,<3"
   45     printf 'Usage: %s [-o|--output <arg>] [-v|--version] [-h|--help] <input-1> [<input-2>] ... [<input-n>] ...\n' "$0"
   46     printf '\t%s\n' "<input>: The input file to transform"
   47     printf '\t%s\n' "-o, --output: Name of the output file (pass '-' for stdout and empty string for the same as input file) (default: '""')"
   48     printf '\t%s\n' "-v, --version: Prints version"
   49     printf '\t%s\n' "-h, --help: Prints help"
   50 }
   51 
   52 
   53 parse_commandline()
   54 {
   55     _positionals_count=0
   56     while test $# -gt 0
   57     do
   58         _key="$1"
   59         case "$_key" in
   60             -o|--output)
   61                 test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
   62                 _arg_output="$2"
   63                 shift
   64                 ;;
   65             --output=*)
   66                 _arg_output="${_key##--output=}"
   67                 ;;
   68             -o*)
   69                 _arg_output="${_key##-o}"
   70                 ;;
   71             -v|--version)
   72                 echo "argbash-1to2 v$version"
   73                 exit 0
   74                 ;;
   75             -v*)
   76                 echo "argbash-1to2 v$version"
   77                 exit 0
   78                 ;;
   79             -h|--help)
   80                 print_help
   81                 exit 0
   82                 ;;
   83             -h*)
   84                 print_help
   85                 exit 0
   86                 ;;
   87             *)
   88                 _last_positional="$1"
   89                 _positionals+=("$_last_positional")
   90                 _positionals_count=$((_positionals_count + 1))
   91                 ;;
   92         esac
   93         shift
   94     done
   95 }
   96 
   97 
   98 handle_passed_args_count()
   99 {
  100     local _required_args_string="'input'"
  101     test "${_positionals_count}" -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 1 (namely: $_required_args_string), but got only ${_positionals_count}." 1
  102 }
  103 
  104 
  105 assign_positional_args()
  106 {
  107     local _positional_name _shift_for=$1
  108     _positional_names="_arg_input "
  109     _our_args=$((${#_positionals[@]} - 1))
  110     for ((ii = 0; ii < _our_args; ii++))
  111     do
  112         _positional_names="$_positional_names _arg_input[$((ii + 1))]"
  113     done
  114 
  115     shift "$_shift_for"
  116     for _positional_name in ${_positional_names}
  117     do
  118         test $# -gt 0 || break
  119         eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
  120         shift
  121     done
  122 }
  123 
  124 parse_commandline "$@"
  125 handle_passed_args_count
  126 assign_positional_args 1 "${_positionals[@]}"
  127 
  128 # OTHER STUFF GENERATED BY Argbash
  129 
  130 ### END OF CODE GENERATED BY Argbash (sortof) ### ])
  131 # [ <-- needed because of Argbash
  132 
  133 #
  134 
  135 _files_to_clean=()
  136 cleanup()
  137 {
  138     test "${#_files_to_clean[*]}" != 0 && rm -f "${_files_to_clean[@]}"
  139 }
  140 
  141 do_stuff ()
  142 {
  143     # SCRIPT_DIR is likely also a default, but maybe not - it may have been set explicitly
  144     grep -q '\${\?SCRIPT_DIR' -- "$infname" && echo "You probably use a variable 'SCRIPT_DIR' in your script. It may be that you should rename it to 'script_dir', but this is not certain :-(" >&2
  145     # We match $_ARG_FOO as well as ${ARG_FOO...
  146     # and _ARGS_FOO
  147     sed 's/\(\${\?_ARGS\?_\w\+\)/\L\1\l/g' "$infname"
  148 }
  149 
  150 outfname="$_arg_output"
  151 test "${#infname[@]}" -gt 1 && test -n "$outfname" && die "You have specified more than one (${#infname[@]}) input filenames, so you probably want to modify the corresponding files in-place. In order to do so, you can't specify an output filename, even '-' does make no sense (currently: '$outfname')"
  152 
  153 trap cleanup EXIT
  154 for infname in "${_arg_input[@]}"
  155 do
  156     test -f "$infname" || { echo "The input parameter has to be a file (got: '$infname')" >&2; exit 1; }
  157 
  158     test -n "$_arg_output" || outfname="$infname"
  159     if test "$outfname" = '-'
  160     then
  161         do_stuff
  162     else
  163         # vvv This should catch most of the cases when we want to overwrite the source file
  164         # vvv and we don't want to leave a file (not even an empty one) when something goes wrong.
  165         temp_outfile="temp_$$"
  166         _files_to_clean+=("$temp_outfile")
  167         do_stuff > "$temp_outfile"
  168         mv "$temp_outfile" "$outfname"
  169         # So we don't make .m4 scripts executable
  170         chmod --reference "$infname" "$outfname"
  171     fi
  172 done
  173 
  174 #
  175 # ] <-- needed because of Argbash