first version.
[build-srcpkg.git] / bin / utilcmd-example
blob41931ab13cacd4addd14c07c7932520ae498932c
1 #!/bin/bash
2 ############################################################
3 # source: example
4 # author: @{SRCPKG_AUTHOR}(@{SRCPKG_AUTHOR_EMAIL})
5 # date: @{DATE}
6 ############################################################
7 # Copyright (C) 2022- Free Software Foundation, Inc.
8 # This configure script is free software; the Free Software
9 # Foundation gives unlimited permission to copy, distribute
10 # and modify it.
11 ############################################################
12 # note:
13 # this file is an demo or template for utility cmd
14 # shell script program.
15 ############################################################
18 # todoļ¼š
19 # @
20 # @
24 # external sourced cmd.
25 #source <extern-script> --load
27 . shlibinc
30 include stdio.shlib
31 include args.shlib
33 ##############################
34 # section: public comment info
35 ##############################
39 ##############################
40 # section: variable define
41 ##############################
46 # this comment is used for version id auto update.
48 # @{prog-ver-begin}
49 readonly EXAMPLE_V1=0
50 readonly EXAMPLE_V2=1
51 readonly EXAMPLE_V3=0
52 readonly EXAMPLE_VEXT=
53 readonly EXAMPLE_VER_DATE=20240201
54 # @{prog-ver-end}
56 readonly EXAMPLE_PROG=`basename $0`
58 readonly EXAMPLE_PROG_VERSION="v${EXAMPLE_V1}.${EXAMPLE_V2}\${EXAMPLE_V3:+\".$EXAMPLE_V3\"}\${EXAMPLE_VEXT:+\"$EXAMPLE_VEXT\"}\${EXAMPLE_VER_DATE:+\"-$EXAMPLE_VER_DATE\"}"
59 EXAMPLE_PROG_VERSION_STR="`eval echo $EXAMPLE_PROG_VERSION`
60 Copyright (C) @{SRCPKG_YEAR} @{SRCPKG_HOLDER}.
61 This configure script is free software; the Free Software
62 Foundation gives unlimited permission to copy, distribute
63 and modify it.
65 Writen by @{SRCPKG_AUTHOR}(@{SRCPKG_AUTHOR_EMAIL}).
68 EXAMPLE_PROG_BANNER="$EXAMPLE_PROG $EXAMPLE_PROG_VERSION_STR"
69 EXAMPLE_PROG_SYNTAX='Usage: $EXAMPLE_PROG -[${p_shortparam//\|/}]'
70 EXAMPLE_PROG_DESC=" this file is an demo or template for utility cmd
71 shell script program.
74 EXAMPLE_PROG_OTHER_DESC="
75 eg:
77 @ getting helper info:
79 ${} -h
80 helper info.
83 EXAMPLE_USAGE_FMT='$EXAMPLE_PROG_BANNER
84 ${EXAMPLE_PROG_SYNTAX}
85 $EXAMPLE_PROG_DESC
86 ${helper}
87 $EXAMPLE_PROG_OTHER_DESC'
91 # do not delete this comment, it is used for var define code intert.
93 # args-var-define-begin
94 # args-var-define-end
98 # desc-str for dev
99 # @ every paramter desc-str-line start with 'param'.
100 # @ every colum start with '|', and seperated by blanks.
101 # @ '|blank' means a blank line dispalyed in helper.
102 # @ follow with a '' quoated string is the option category string.
103 # @ comment '#' in desc-str is also supported, and can be used to
104 # disable some option have not been implemented.
105 # @ desc-str can be in multiple var. and some of them maybe re-used
106 # for other program by 'source xxx --loadshlib'.
108 dev_desc_hdr_str="
109 |prog $0 'this file is an demo or template for utility cmd shell script program.'
112 # @ PARAMETER DEFINE.
113 # this desc str would be referenced in scripttest, consider about opt name space.
114 # =, param name for opt.
115 # %, var name if this opt is enabled.
116 # &, proc func if opt trigged.
117 example_desc_str="
118 |prog $0 'project build util-program.'
120 |blank '[example-category]'
121 |param |- |-- |---subcmd |= |%<subcmd> |&<on_subcmd> |'subcmd demo.'
122 |param |- |-- |---longopt |=<file> |%<longopt> |&<on_longopt> |'long option demo.'
123 |param |- |-- |---shortopt |=<file> |%<shortopt> |&<on_shortopt> |'short option demo.'
124 dest.'
126 |blank
129 example_other_desc_str="
130 |blank 'Version & Helper & Debug:'
131 |param |- |-- |---version |= |%<version> |&<on_version> |'output version info of this program.'
132 |param |- |--help |---help |= |%<help> |&<on_help> |'this helper text doc.'
138 ##############################
139 # section: private function
140 ##############################
144 # fsyntax: on_subcmd
145 # fdesc: on subcmd sub-cmd.
147 on_subcmd ()
149 # TBD:
154 # fsyntax: on_longopt
155 # fdesc: on longopt sub-cmd.
157 on_longopt ()
159 # TBD:
164 # fsyntax: on_shortopt
165 # fdesc: on shortopt sub-cmd.
167 on_shortopt ()
169 # TBD:
174 # fsyntax: on_version
175 # fdesc: on version sub-cmd.
177 on_version ()
179 eval echo "$EXAMPLE_PROG_VERSION"
181 exit
185 # fsyntax: on_help
186 # fdesc: on help sub-cmd.
188 on_help ()
190 [[ -z $term_width ]] && term_width=80
192 helper="$(opt_helper)"
194 eval EXAMPLE_PROG_SYNTAX="\"$EXAMPLE_PROG_SYNTAX\""
195 eval echo -ne "\"$EXAMPLE_USAGE_FMT\""
197 exit
202 ##############################
203 # section: public function
204 ##############################
208 main_example ()
210 [[ "$@" =~ '--load' ]] && return
213 # args parsing
215 # prog_opt_dispatch "$@"
216 opt_desc_str_dispatch example_desc_hdr_str
217 opt_desc_str_dispatch example_desc_str
218 opt_desc_str_dispatch example_other_desc_str
219 prog_opt_proc "$@"
221 init_dbglogout 2 testing 20000
222 # set_output_prefix info ""
223 set_auto_newline ""
225 # SRCPKG_DIR="$(pwd)"
226 # TEST_DIR_STR_FMT="$(attr_get catalog::testdir::STR_FMT)"
227 # ALIGN=' '
230 # running action list function for options after init.
231 # the paramter for init will be effact, then execute the
232 # action function.
234 action_list_exec
237 main_example "$@"
239 if [[ "$@" =~ "--debug" ]]; then
240 declare -g DBGOUTD_OUTPUT=1
242 # does not need to shift args if the --debug option is not the
243 # first option. it will be ignore in process.
244 # shift
247 if [[ $1 != "--loadshlib" ]]; then
248 # main "$@"
249 main_example "$@"
250 else
251 shift
255 ##############################
256 # section: file tail
257 ##############################