barg-parser/example.sh

86 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Example: dev CLI tool using BARG framework
# Source the BARG framework
source ./barg.sh
# shellcheck source=cli.vars.generated.sh
[[ -f "${BASH_SOURCE%.*}.vars.generated.sh" ]] &&
source "${BASH_SOURCE%.*}.vars.generated.sh"
# Define your CLI specification
export CLI_SPEC=(
"command;dev;Dev Container Management Tool"
"argument;help,h;type:flag;help:Show help message"
"argument;global;type:flag;help:Enable global mode"
"command;build;Build a dev container"
"argument;from;type:option;dest:fromName;help:Name of the base container"
"argument;name;type:positional;required;help:Name of the container"
"argument;nametwo;help:Secondary name"
"argument;image,i;type:option;required;dest:imageName;help:Base image"
"argument;include,I;type:option;repeatable;dest:includePaths;help:Include paths"
"argument;verbose,v;type:flag;repeatable;default:0;help:Increase verbosity (-v, -vv, ...)"
"argument;quiet,q;type:flag;default:false;help:Suppress all output"
"argument;cmd;type:rest;help:Command and args to run inside the container"
"argument;name;dest:nameOpt;type:option;help:Building container name"
"command;container;Nested container configuration"
"argument;dev;type:flag;help:Development mode"
"argument;name;required;help:Container name"
"argument;cmd;type:rest;dest:cmdRest;help:Command to run"
"end"
"end"
"command;stop;Stop a dev container"
"argument;name;type:positional;required;help:Name of the container"
"argument;kill,k;type:flag;default:false;help:Force kill the container"
"end"
"end"
)
# Command handlers - implement your logic here!
# Root command handler
cmd() {
echo "Dev Container Tool"
echo "Use --help to see available commands"
}
cmd_build() {
# now available directly:
# $name, $imageName, $fromName, $quiet, $verbose, $cmd, $nameOpt, $includePaths ...
echo "Building container: $name"
echo " Base image: $imageName"
[[ -n "$fromName" ]] && echo " From: $fromName"
[[ "$verbose" == "true" ]] && echo " Verbose mode enabled"
[[ "$quiet" == "true" ]] && echo " Quiet mode enabled"
[[ -n "$cmd" ]] && echo " Command: $cmd"
echo "Container built successfully!"
}
cmd_build_container() {
# $name is the build-level name, container's name appears as $container_name due to collision resolution
echo "Building nested container configuration"
echo " Parent container: $name"
echo " Child container: $container_name"
echo " Image: $imageName"
[[ "$dev" == "true" ]] && echo " Development mode: ON"
[[ -n "$cmdRest" ]] && echo " Command: $cmdRest"
echo "Nested container configured!"
}
cmd_stop() {
echo "Stopping container: $name"
if [[ "$kill" == "true" ]]; then
echo " Force killing..."
else
echo " Graceful shutdown..."
fi
echo "Container stopped!"
}
# Run the CLI with all arguments
barg_run CLI_SPEC[@] "$@"