22
33function detect_container_command() {
44
5+
56if [ -z " ${CONTAINER_CMD} " ]; then
67 CONTAINER_CMD=" $( podman version > /dev/null 2>&1 && echo podman) "
78fi
89
910if [ -z " ${CONTAINER_CMD} " ]; then
1011 CONTAINER_CMD=" $( docker version > /dev/null 2>&1 && echo docker) "
12+
1113fi
1214}
1315
1416
1517
16- # default values for command line options:
18+ # default values for ccommand line options:
1719 LANG=c
1820 REGISTRY=quay.io
1921 CONTAINER_CMD=" "
2325 DEPS=" "
2426 DEFAULT_ACTION=" help"
2527 ACTION=" $DEFAULT_ACTION "
26- # action that was explicitly set from the command line:
28+ # action that was explicxitly set from the command line:
2729 EXPLICIT_ACTION=" "
2830 # shell for interactive use:
2931 I_SHELL=" bash"
@@ -36,14 +38,6 @@ if [ -z "$CONTAINER_CMD" ]; then
3638 exit 1
3739fi
3840
39- # Detect whether GNU getopt is available.
40- # GNU getopt returns exit code 4 when called with --test; BSD getopt does not.
41- HAS_GNU_GETOPT=false
42- getopt --test > /dev/null 2>&1
43- if [ $? -eq 4 ]; then
44- HAS_GNU_GETOPT=true
45- fi
46-
4741function usage() {
4842>&2 cat << EOF
4943
@@ -56,27 +50,25 @@ These are the supported options:
5650[ -r | --registry (quay.io|... ] - container registry (default: $REGISTRY )
5751[ -c | --container-cmd (podman|docker) ] - default: $CONTAINER_CMD (auto-detected)
5852[ -o | --os (fedora|debian|ubuntu|rocky|suse) ] - linux distro (default: $OS )
59- [ -n | --registry-namespace (...) ] - default: $NAMESPACE
60- [ -s | --build-script (command|path) ] - command or local script (in the CWD) for building the project.
53+ [ -n | --registry-namespace (...) ] - default: $NAMESPACE
54+ [ -s | --build-script (command|path) ] - command or local script (in the CWD) for building the project.
6155[ -d | --deps (pkg,pkg,pkg,...) ] - additional packages to install (comma-separated)
56+ -i | --shell (bash) ] - sinteractive shell (default: $I_SHELL )
6257[ -h | --help ] - action help: print this usage information
63- [ -t | --test ] - action test: print some diagnostic info for testing this program
58+ [-t | --test ] - action test: print some diagnostic info for testing this program
6459[ -b | --build ] - action build: perform a build in the CWD
65- [ -e | --enter ] - action enter: enter container in interactive mode (shell)
60+ [ -e | --enter] - action enter: enter container in interactive mode (shell)
6661
67- Note: Long options (e.g. --build, --lang) require GNU getopt.
68- If GNU getopt is not available (e.g. on macOS without Homebrew's gnu-getopt),
69- only short options are supported and a warning will be shown for long options.
70- Install GNU getopt with: brew install gnu-getopt (macOS) or via your Linux package manager.
7162
72- The Options -l, -r, -o, and -n in combination determine the buildbox image (by tag) to be used.
63+ The Options -l, -r, -o, and -n in combination determine the buildbox image (by tag) to be used.
64+
7365
7466The options -h, -t, -e, and -b are the available actions (help, test, enter, and build) to be performed.
7567
76- Exactly one of the actions must be selected.
68+ Exactly one of the actions must be selected.
7769
7870The build action requires the additional option -s.
79- The enter action is triggered by the -e flag .
71+ The enter action makes use of the --shell option .
8072
8173EOF
8274
@@ -86,6 +78,8 @@ function set_action() {
8678
8779local action=" $1 "
8880
81+
82+
8983if [ -n " $EXPLICIT_ACTION " ]; then
9084 echo " Error: multiple actions specified on cmdline but only one is allowed."
9185 usage
@@ -97,94 +91,51 @@ if [ -n "$EXPLICIT_ACTION" ]; then
9791}
9892
9993
94+
95+
10096# parsing command line arguments:
10197optstring=" l:r:c:o:n:hbtes:d:"
102- optstring_long=" lang:,registry:,container-cmd:,os:,registry-namespace:, help,test,build,enter,build-script:,deps:"
98+ optstring_long=" lang:,registry:,container-cmd:,help,test,build,enter,build-script:,deps:"
10399
104- if $HAS_GNU_GETOPT ; then
105- # GNU getopt is available: support both short and long options.
106- parsed_args=$( getopt -n " builbo" -o " $optstring " -l " $optstring_long " -- " $@ " )
107- args_valid=$?
108100
109- if [ " $args_valid " != " 0" ]; then
110- echo " error: invalid args."
111- usage
112- exit 1
113- fi
101+ parsed_args=$( getopt -n " builbo" -o " $optstring " -l " $optstring_long " -- " $@ " )
102+ args_valid=$?
114103
115- # processing parsed args
116- eval set -- " $parsed_args "
117- while :
118- do
119- case " $1 " in
120- -l | --lang) LANG=" $2 " ; shift 2 ;;
121- -r | --registry) REGISTRY=" $2 " ; shift 2 ;;
122- -c | --container-cmd) CONTAINER_CMD=" $2 " ; shift 2 ;;
123- -o | --os) OS=" $2 " ; shift 2 ;;
124- -n | --registry-namespace) NAMESPACE=" $2 " ; shift 2 ;;
125- -s | --build-script) BUILD_CMD=" $2 " ; shift 2 ;;
126- -d | --deps) DEPS=" $2 " ; shift 2 ;;
127- -h | --help) set_action " help" ; shift ;;
128- -b | --build) set_action " build" ; shift ;;
129- -t | --test) set_action " test" ; shift ;;
130- -e | --enter) set_action " enter" ; shift ;;
131- # -- means end of args; stop processing.
132- --) shift ; break ;;
133- * ) echo " Unexpected option $1 - this should not happen."
134- usage
135- exit 1
136- ;;
137- esac
138- done
139-
140- else
141- # GNU getopt is not available; fall back to Bash built-in getopts (short options only).
142- echo " warning: GNU getopt is not available on this system. Only short options are supported." >&2
143- echo " Install GNU getopt for long option support (e.g. brew install gnu-getopt on macOS)." >&2
144-
145- # Error on any long options passed by the user.
146- # Track value-consuming short options so their values are not mistaken for long options.
147- skip_next=false
148- for arg in " $@ " ; do
149- if $skip_next ; then
150- skip_next=false
151- continue
152- fi
153- if [[ " $arg " == --?* ]]; then
154- echo " error: long option '$arg ' is not supported without GNU getopt." >&2
155- echo " Please use the equivalent short option, or install GNU getopt." >&2
156- usage
157- exit 1
158- fi
159- # Short options that consume the next argument (from optstring: l:r:c:o:n:s:d:)
160- if [[ " $arg " =~ ^-[lrconsd]$ ]]; then
161- skip_next=true
162- fi
163- done
164-
165- while getopts " $optstring " opt; do
166- case " $opt " in
167- l) LANG=" $OPTARG " ;;
168- r) REGISTRY=" $OPTARG " ;;
169- c) CONTAINER_CMD=" $OPTARG " ;;
170- o) OS=" $OPTARG " ;;
171- n) NAMESPACE=" $OPTARG " ;;
172- s) BUILD_CMD=" $OPTARG " ;;
173- d) DEPS=" $OPTARG " ;;
174- h) set_action " help" ;;
175- b) set_action " build" ;;
176- t) set_action " test" ;;
177- e) set_action " enter" ;;
178- * )
179- echo " error: invalid args."
180- usage
181- exit 1
182- ;;
183- esac
184- done
185- shift $(( OPTIND - 1 ))
104+ if [ " $args_valid " != " 0" ]; then
105+ echo " error:invalid args."
106+ usage
107+ exit 1
186108fi
187109
110+ # processing parsed args
111+
112+ eval set -- " $parsed_args "
113+ while :
114+ do
115+ case " $1 " in
116+ -l | --lang) LANG=" $2 " ; shift 2 ;;
117+ -r | --registry) REGISTRY=" $2 " ; shift 2 ;;
118+ -c | --container-cmd) CONTAINER_CMD=" $2 " ; shift 2 ;;
119+ -o | --os) OS=" $2 " ; shift 2 ;;
120+ -n | --registry-namespace) NAMESPACE=" $2 " ; shift 2 ;;
121+ -s | --build-script) BUILD_CMD=" $2 " ; shift 2 ;;
122+ -d | --deps) DEPS=" $2 " ; shift 2 ;;
123+ -h | --help) set_action " help" ; shift ;;
124+ -b| --build) set_action " build" ; shift ;;
125+ -t| --test) set_action " test" ; shift ;;
126+ -e|--enter) set_action " enter" ; shift ;;
127+
128+
129+ # -- means end of args. ignore and stop processing.
130+ --) shift ; break ;;
131+ * ) echo " Unexpected option $1 . - this should not happen."
132+ # this should have been caught above with args_valid
133+ usage
134+ exit 1
135+ ;;
136+ esac
137+ done
138+
188139# done processing args
189140
190141
0 commit comments