"Fossies" - the Fresh Open Source Software Archive

Member "argbash-2.10.0/resources/examples/simple-wrapper.sh" (22 Sep 2020, 4667 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 "simple-wrapper.sh": 2.9.0_vs_2.10.0.

    1 #!/bin/bash
    2 
    3 # DEFINE_SCRIPT_DIR()
    4 # ARG_POSITIONAL_INF([directory],[Directories to go through],[1])
    5 # ARG_OPTIONAL_SINGLE([glob],[],[What files to match in the directory],[*])
    6 # ARGBASH_WRAP([simple-parsing],[filename])
    7 # ARG_HELP([This program tells you size of specified files in given directories in units you choose.])
    8 # ARGBASH_SET_INDENT([  ])
    9 # ARGBASH_GO()
   10 # needed because of Argbash --> m4_ignore([
   11 ### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
   12 # Argbash is a bash code generator used to get arguments parsing right.
   13 # Argbash is FREE SOFTWARE, see https://argbash.io for more info
   14 
   15 
   16 die()
   17 {
   18   local _ret="${2:-1}"
   19   test "${_PRINT_HELP:-no}" = yes && print_help >&2
   20   echo "$1" >&2
   21   exit "${_ret}"
   22 }
   23 
   24 
   25 begins_with_short_option()
   26 {
   27   local first_option all_short_options='uh'
   28   first_option="${1:0:1}"
   29   test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
   30 }
   31 
   32 # THE DEFAULTS INITIALIZATION - POSITIONALS
   33 _positionals=()
   34 _arg_directory=('' )
   35 # THE DEFAULTS INITIALIZATION - OPTIONALS
   36 _arg_glob="*"
   37 _arg_unit="b"
   38 _arg_verbose="off"
   39 
   40 
   41 print_help()
   42 {
   43   printf '%s\n' "This program tells you size of specified files in given directories in units you choose."
   44   printf 'Usage: %s [--glob <arg>] [-u|--unit <arg>] [--(no-)verbose] [-h|--help] <directory-1> [<directory-2>] ... [<directory-n>] ...\n' "$0"
   45   printf '\t%s\n' "<directory>: Directories to go through"
   46   printf '\t%s\n' "--glob: What files to match in the directory (default: '*')"
   47   printf '\t%s\n' "-u, --unit: What unit we accept (b for bytes, k for kibibytes, M for mebibytes) (default: 'b')"
   48   printf '\t%s\n' "-h, --help: Prints help"
   49 }
   50 
   51 
   52 parse_commandline()
   53 {
   54   _positionals_count=0
   55   while test $# -gt 0
   56   do
   57     _key="$1"
   58     case "$_key" in
   59       --glob)
   60         test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
   61         _arg_glob="$2"
   62         shift
   63         ;;
   64       --glob=*)
   65         _arg_glob="${_key##--glob=}"
   66         ;;
   67       -u|--unit)
   68         test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
   69         _arg_unit="$2"
   70         _args_simple_parsing_opt+=("${_key}" "$2")
   71         shift
   72         ;;
   73       --unit=*)
   74         _arg_unit="${_key##--unit=}"
   75         _args_simple_parsing_opt+=("$_key")
   76         ;;
   77       -u*)
   78         _arg_unit="${_key##-u}"
   79         _args_simple_parsing_opt+=("$_key")
   80         ;;
   81       --no-verbose|--verbose)
   82         _arg_verbose="on"
   83         _args_simple_parsing_opt+=("${_key}")
   84         test "${1:0:5}" = "--no-" && _arg_verbose="off"
   85         ;;
   86       -h|--help)
   87         print_help
   88         exit 0
   89         ;;
   90       -h*)
   91         print_help
   92         exit 0
   93         ;;
   94       *)
   95         _last_positional="$1"
   96         _positionals+=("$_last_positional")
   97         _positionals_count=$((_positionals_count + 1))
   98         ;;
   99     esac
  100     shift
  101   done
  102 }
  103 
  104 
  105 handle_passed_args_count()
  106 {
  107   local _required_args_string="'directory'"
  108   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
  109 }
  110 
  111 
  112 assign_positional_args()
  113 {
  114   local _positional_name _shift_for=$1
  115   _positional_names="_arg_directory "
  116   _our_args=$((${#_positionals[@]} - 1))
  117   for ((ii = 0; ii < _our_args; ii++))
  118   do
  119     _positional_names="$_positional_names _arg_directory[$((ii + 1))]"
  120   done
  121 
  122   shift "$_shift_for"
  123   for _positional_name in ${_positional_names}
  124   do
  125     test $# -gt 0 || break
  126     eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
  127     shift
  128   done
  129 }
  130 
  131 parse_commandline "$@"
  132 handle_passed_args_count
  133 assign_positional_args 1 "${_positionals[@]}"
  134 
  135 # OTHER STUFF GENERATED BY Argbash
  136 _args_simple_parsing=("${_args_simple_parsing_opt[@]}" "${_args_simple_parsing_pos[@]}")
  137 script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || { echo "Couldn't determine the script's running directory, which probably matters, bailing out" >&2; exit 2; }
  138 
  139 ### END OF CODE GENERATED BY Argbash (sortof) ### ])
  140 # [ <-- needed because of Argbash
  141 
  142 
  143 script="$script_dir/simple.sh"
  144 test -f "$script" || { echo "Missing the wrapped script, was expecting it next to me, in '$script_dir'."; exit 1; }
  145 
  146 for directory in "${_arg_directory[@]}"
  147 do
  148   test -d "$directory" || die "We expected a directory, got '$directory', bailing out."
  149   printf "Contents of '%s' matching '%s':\n" "$directory" "$_arg_glob"
  150   for file in "$directory"/$_arg_glob
  151   do
  152     test -f "$file" && printf "\t%s: %s\n" "$(basename "$file")" "$("$script" "${_args_simple_parsing_opt[@]}" "$file")"
  153   done
  154 done
  155 
  156 # ] <-- needed because of Argbash