Skip to content

Commit

Permalink
bake.bash: optHelp > opt_help
Browse files Browse the repository at this point in the history
  • Loading branch information
chen56 committed Mar 28, 2024
1 parent d946adb commit 1078a0d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions bake.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}


Expand Down Expand Up @@ -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
Expand All @@ -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 "
Expand Down Expand Up @@ -493,23 +493,23 @@ 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 这样使用
# type: 类型,目前支持 bool|string|list
# 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"
bake._opt_internal_add bake.opt "type" "string" "true" "" "" "option type [bool|string|list]"
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
Expand All @@ -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)
Expand Down Expand Up @@ -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]}" "$@")"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/bake2_test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand All @@ -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"
}
Expand Down

0 comments on commit 1078a0d

Please sign in to comment.