You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.4 KiB
109 lines
3.4 KiB
1 year ago
|
#!/bin/bash
|
||
|
# Общественное достояние, 2024, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_parse_arg_lib@narod.ru>
|
||
|
|
||
|
# Обработка входных параметров
|
||
|
|
||
|
# Формат:
|
||
|
# "Однобуквенная команда|Расширенная команда|Справка|Параметр|Значение по умолчанию|Команда на исполнение"
|
||
|
# Параметр: Пусто - нет параметров, : - есть параметр, :: - параметр не обязателен
|
||
|
|
||
|
# Пример
|
||
|
#common_params=(
|
||
|
# "i|input|Входной текстовый файл.|:|'1.txt'|"
|
||
|
# )
|
||
|
|
||
|
function ProcessParams {
|
||
|
local iparams=$1[@]
|
||
|
local work_func=$2
|
||
|
local params=("${!iparams}")
|
||
|
local custom_arg1="$3"
|
||
|
local custom_arg2="$4"
|
||
|
|
||
|
SAVE_IFS=$IFS
|
||
|
IFS=""
|
||
|
|
||
|
for (( i=0; i< ${#params[*]}; i++))
|
||
|
do
|
||
|
p="${params[i]}"
|
||
|
readarray -d "|" -t cur_params <<< "$p"
|
||
|
small_cmd="${cur_params[0]}"
|
||
|
large_cmd="${cur_params[1]}"
|
||
|
comment="${cur_params[2]}"
|
||
|
use_param="${cur_params[3]}"
|
||
|
default="${cur_params[4]}"
|
||
|
custom_cmd="${cur_params[5]}"
|
||
|
|
||
|
$work_func "$small_cmd" "$large_cmd" "$comment" "$use_param" "$default" "$custom_cmd" "$custom_arg1" "$custom_arg2"
|
||
|
done
|
||
|
|
||
|
IFS=$SAVE_IFS
|
||
|
}
|
||
|
|
||
|
param_var='local small_cmd="$1";local large_cmd="$2";local comment="$3";local use_param="$4";local default="$5";local custom_cmd="$6";local custom_arg1="$7";local custom_arg2="$8";'
|
||
|
|
||
|
function Params2InitVar {
|
||
|
eval "$param_var"
|
||
|
|
||
|
if [ "$use_param" != '' ]; then
|
||
|
eval "${large_cmd}=${default}"
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
function Params2Help {
|
||
|
eval "$param_var"
|
||
|
|
||
|
echo "-${small_cmd}, -${large_cmd}, --${large_cmd}"
|
||
|
echo " ${comment/"!DEFAULT!"/"${!large_cmd}"}"
|
||
|
}
|
||
|
|
||
|
function Params2small_list {
|
||
|
eval "$param_var"
|
||
|
|
||
|
echo -n "${small_cmd}${use_param}"
|
||
|
}
|
||
|
|
||
|
function Params2large_list {
|
||
|
eval "$param_var"
|
||
|
|
||
|
echo -n ",${large_cmd}${use_param}"
|
||
|
}
|
||
|
|
||
|
function Params2Case {
|
||
|
eval "$param_var"
|
||
|
|
||
|
if [ "$custom_arg1" = "-${small_cmd}" ] || [ "$custom_arg1" = "-${large_cmd}" ] || [ "$custom_arg1" = "--${large_cmd}" ]; then
|
||
|
clear_custom_cmd="${custom_cmd//[$'\t\r\n ']/}"
|
||
|
if [ "${clear_custom_cmd}" != "" ]; then
|
||
|
eval "${custom_cmd}"
|
||
|
else
|
||
|
eval "${large_cmd}=\"$custom_arg2\""
|
||
|
fi
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
# Инициализация переменных
|
||
|
ProcessParams all_params Params2InitVar
|
||
|
|
||
|
# $@ is all command line parameters passed to the script.
|
||
|
# -o is for short options like -v
|
||
|
# -l is for long options with double dash like --version
|
||
|
# the comma separates different long options
|
||
|
# -a is for long options with single dash like -version
|
||
|
# Example
|
||
|
# 'h' is a no-value option.
|
||
|
# 'v:' implies that option -v has value and is a mandatory option. ':' means has a value.
|
||
|
# 't::' implies that option -t has value but is optional. '::' means optional.
|
||
|
small_params_list=$(ProcessParams all_params Params2small_list)
|
||
|
small_params_list="${small_params_list:1}"
|
||
|
|
||
|
large_params_list=$(ProcessParams all_params Params2large_list)
|
||
|
large_params_list="${large_params_list:1}"
|
||
|
|
||
|
options=$(getopt --long "$large_params_list" -o "$small_params_list" -a -- "$@")
|
||
|
|
||
|
# set --:
|
||
|
# If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters
|
||
|
# are set to the arguments, even if some of them begin with a ‘-’.
|
||
|
eval set -- "$options"
|
||
|
|