From 1078a0d1cfceb0cbaa56b301b3488b8f01a20706 Mon Sep 17 00:00:00 2001 From: Chen Peng Date: Thu, 28 Mar 2024 19:41:50 +0800 Subject: [PATCH] bake.bash: optHelp > opt_help --- bake | 2 +- bake.bash | 28 ++++++++++++++-------------- test/bake2_test.bash | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/bake b/bake index f4ebeeac..2248cd69 100755 --- a/bake +++ b/bake @@ -16,7 +16,7 @@ SCRIPT_DIR="$(dirname "$SCRIPT_PATH")" SCRIPT_FILE="$(basename "$SCRIPT_PATH")" # include common script -source "$SCRIPT_DIR/vendor/bake.bash" +source "$SCRIPT_DIR/bake.bash" ########################################## # app cmd script diff --git a/bake.bash b/bake.bash index b9549331..4107bed7 100644 --- a/bake.bash +++ b/bake.bash @@ -360,14 +360,14 @@ bake._opt_cmd_chain_opts() { # only use by bake.opt, # because "bake.opt" is meta function, use this func to add self bake._opt_internal_add() { - local cmd="$1" opt="$2" type="$3" required="$4" default="$5" abbr="$6" optHelp="$7" + local cmd="$1" opt="$2" type="$3" required="$4" default="$5" abbr="$6" opt_help="$7" _data["$cmd/opts/$opt"]="type:opt" _data["$cmd/opts/$opt/name"]="$opt" _data["$cmd/opts/$opt/type"]="$type" _data["$cmd/opts/$opt/required"]="$required" _data["$cmd/opts/$opt/abbr"]="$abbr" _data["$cmd/opts/$opt/default"]="$default" - _data["$cmd/opts/$opt/optHelp"]="$optHelp" + _data["$cmd/opts/$opt/opt_help"]="$opt_help" } @@ -450,7 +450,7 @@ bake._show_cmd_help() { local required=${_data["$optPath/required"]} local abbr=${_data["$optPath/abbr"]} local default=${_data["$optPath/default"]} - local optHelp="${_data["$optPath/optHelp"]}" + local opt_help="${_data["$optPath/opt_help"]}" local optArgDesc="" if [[ "$type" == "string" ]]; then @@ -461,7 +461,7 @@ bake._show_cmd_help() { fi fi - printf " --%-20s -%-2s %-6s required:[%s] %b\n" "$name $optArgDesc" "$abbr" "$type" "$required" "$optHelp" + printf " --%-20s -%-2s %-6s required:[%s] %b\n" "$name $optArgDesc" "$abbr" "$type" "$required" "$opt_help" done echo " @@ -493,7 +493,7 @@ Available Commands:" # 为cmd配置参数(public api) # Examples: -# bake.opt --cmd "build" --name "is_zip" --type bool --required --abbr z --default true --optHelp "is_zip, build项目时是否压缩" +# bake.opt --cmd "build" --name "is_zip" --type bool --required --abbr z --default true --opt_help "is_zip, build项目时是否压缩" # 每个参数可以配置如下信息: # cmd: 参数作用的命令全名 # name: 参数长名,可以 ./bake build --is_zip 这样使用 @@ -501,7 +501,7 @@ Available Commands:" # required: 是否必须提供,不提供将报错 # abbr: 参数短名, 可以 ./bake build -z 这样使用 # default: 缺省值, 未指定参数时,使用此值 -# optHelp: 参数帮助,将显示在‘./bake build -h’命令帮助里 +# opt_help: 参数帮助,将显示在‘./bake build -h’命令帮助里 # 参考[bake.parse] bake._opt_internal_add bake.opt "cmd" "string" "true" "" "" "cmd name" bake._opt_internal_add bake.opt "name" "string" "true" "" "" "option name" @@ -509,7 +509,7 @@ bake._opt_internal_add bake.opt "type" "string" "true" "" "" "opt bake._opt_internal_add bake.opt "required" "bool" "false" "false" "false" "option required [true|false],default[false]" bake._opt_internal_add bake.opt "abbr" "string" "false" "" "" "option abbr" bake._opt_internal_add bake.opt "default" "string" "false" "" "" "option abbr" -bake._opt_internal_add bake.opt "optHelp" "string" "false" "" "" "option optHelp" +bake._opt_internal_add bake.opt "opt_help" "string" "false" "" "" "option opt_help" bake.opt() { eval "$(bake.parse ""${FUNCNAME[0]}"" "$@")" if [[ "$name" == "" ]]; then @@ -521,7 +521,7 @@ bake.opt() { if [[ "$type" != "bool" && "$type" != "string" && "$type" != "list" ]]; then echo "error: option [--type] must in [bool|string|list] " >&2 && return 1 fi - bake._opt_internal_add "$cmd" "$name" "$type" "${required:-false}" "$default" "$abbr" "$optHelp" + bake._opt_internal_add "$cmd" "$name" "$type" "${required:-false}" "$default" "$abbr" "$opt_help" } # bake.opt (public api) @@ -635,10 +635,10 @@ bake.parse() { # --summary "flutter-note cli." \ # --description ".... your root cmd help " # 这样就可以用'./your_script -h' 查看根帮助了 -bake.opt --cmd "bake.cmd" --name "cmd" --type string --optHelp "cmd, function name" -bake.opt --cmd "bake.cmd" --name "usage" --type string --optHelp "usage" -bake.opt --cmd "bake.cmd" --name "summary" --type string --optHelp "summary help, short, show on cmd list" -bake.opt --cmd "bake.cmd" --name "description" --type string --optHelp "description, long help ,show on cmd help page" +bake.opt --cmd "bake.cmd" --name "cmd" --type string --opt_help "cmd, function name" +bake.opt --cmd "bake.cmd" --name "usage" --type string --opt_help "usage" +bake.opt --cmd "bake.cmd" --name "summary" --type string --opt_help "summary help, short, show on cmd list" +bake.opt --cmd "bake.cmd" --name "description" --type string --opt_help "description, long help ,show on cmd help page" bake.cmd() { # 模版代码,放到每个需要使用option的函数中,然后就可以使用option了 eval "$(bake.parse "${FUNCNAME[0]}" "$@")" @@ -735,8 +735,8 @@ bake.go() { } # _root is special cmd(you can define it), bake add some common options to this cmd, you can add yourself options -bake.opt --cmd _root --name "help" --abbr h --type bool --default false --optHelp "print help, show all commands" -bake.opt --cmd _root --name "debug" --abbr d --type bool --default false --optHelp "debug mode, print more internal info" +bake.opt --cmd _root --name "help" --abbr h --type bool --default false --opt_help "print help, show all commands" +bake.opt --cmd _root --name "debug" --abbr d --type bool --default false --opt_help "debug mode, print more internal info" # BASH_SOURCE > 1 , means bake import from other script, it is lib mode # lib mod is not load app function, so we need to stop here diff --git a/test/bake2_test.bash b/test/bake2_test.bash index 655579e7..c8c106de 100755 --- a/test/bake2_test.bash +++ b/test/bake2_test.bash @@ -401,7 +401,7 @@ test.bake._cmd_register(){ @contains "test.bake._cmd_register" } test.data.children(){ - assert "$(bake._data_children "bake.opt/opts")" @is_escape "abbr\ncmd\ndefault\nname\noptHelp\nrequired\ntype" + assert "$(bake._data_children "bake.opt/opts")" @is_escape "abbr\ncmd\ndefault\nname\nopt_help\nrequired\ntype" } test.bake._opt_cmd_chain_opts(){ @@ -417,7 +417,7 @@ bake.opt/opts/abbr bake.opt/opts/cmd bake.opt/opts/default bake.opt/opts/name -bake.opt/opts/optHelp +bake.opt/opts/opt_help bake.opt/opts/required bake.opt/opts/type" }