"Fossies" - the Fresh Open Source Software Archive

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

    1 #!/bin/bash
    2 
    3 # ARG_POSITIONAL_SINGLE([filename])
    4 # ARG_OPTIONAL_SINGLE([unit],[u],[What unit we accept (b for bytes, k for kibibytes, M for mebibytes)],[b])
    5 # ARG_VERSION([echo $0 v0.1])
    6 # ARG_OPTIONAL_BOOLEAN([verbose])
    7 # ARG_HELP([This program tells you size of file that you pass to it in chosen units.])
    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='uvh'
   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 # THE DEFAULTS INITIALIZATION - OPTIONALS
   35 _arg_unit="b"
   36 _arg_verbose="off"
   37 
   38 
   39 print_help()
   40 {
   41   printf '%s\n' "This program tells you size of file that you pass to it in chosen units."
   42   printf 'Usage: %s [-u|--unit <arg>] [-v|--version] [--(no-)verbose] [-h|--help] <filename>\n' "$0"
   43   printf '\t%s\n' "-u, --unit: What unit we accept (b for bytes, k for kibibytes, M for mebibytes) (default: 'b')"
   44   printf '\t%s\n' "-v, --version: Prints version"
   45   printf '\t%s\n' "-h, --help: Prints help"
   46 }
   47 
   48 
   49 parse_commandline()
   50 {
   51   _positionals_count=0
   52   while test $# -gt 0
   53   do
   54     _key="$1"
   55     case "$_key" in
   56       -u|--unit)
   57         test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
   58         _arg_unit="$2"
   59         shift
   60         ;;
   61       --unit=*)
   62         _arg_unit="${_key##--unit=}"
   63         ;;
   64       -u*)
   65         _arg_unit="${_key##-u}"
   66         ;;
   67       -v|--version)
   68         echo $0 v0.1
   69         exit 0
   70         ;;
   71       -v*)
   72         echo $0 v0.1
   73         exit 0
   74         ;;
   75       --no-verbose|--verbose)
   76         _arg_verbose="on"
   77         test "${1:0:5}" = "--no-" && _arg_verbose="off"
   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="'filename'"
  101   test "${_positionals_count}" -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 1 (namely: $_required_args_string), but got only ${_positionals_count}." 1
  102   test "${_positionals_count}" -le 1 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 1 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
  103 }
  104 
  105 
  106 assign_positional_args()
  107 {
  108   local _positional_name _shift_for=$1
  109   _positional_names="_arg_filename "
  110 
  111   shift "$_shift_for"
  112   for _positional_name in ${_positional_names}
  113   do
  114     test $# -gt 0 || break
  115     eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
  116     shift
  117   done
  118 }
  119 
  120 parse_commandline "$@"
  121 handle_passed_args_count
  122 assign_positional_args 1 "${_positionals[@]}"
  123 
  124 # OTHER STUFF GENERATED BY Argbash
  125 
  126 ### END OF CODE GENERATED BY Argbash (sortof) ### ])