Skip to content

Commit

Permalink
Added advanced.bash and compact.bash
Browse files Browse the repository at this point in the history
  • Loading branch information
liudng committed Jan 1, 2021
1 parent 40ae983 commit 5daf6db
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.session.vim
.session.viminfo

102 changes: 102 additions & 0 deletions advanced.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash
# Copyright 2020 Liu Dng. All rights reserved.
# Use of this source code is governed by Apache License
# that can be found in the LICENSE file.

# trace ERR through pipes
set -o pipefail

# set -e : exit the script if any statement returns a non-true return value
set -o errexit

# The dev package version
declare -gr dev_global_version="1.0.0"

# The dev execution file path
declare -gr dev_global_self="$(realpath $0)"

# The dev execution base path
declare -gr dev_global_base="$(dirname $(dirname $dev_global_self))"

declare -g dev_arg_key

declare -g dev_arg_val

#
# Declare custom global variables
#


cmd_body() {
#
# Write your own source code here.
#
echo "This is a sample bash script."
echo "$dev_arg_key = $dev_arg_val"
echo "Custom arguments: $@"
}

dev_kernel_help_usage() {
echo "Usage: $(basename $dev_global_self) [--trace] [--verbose] [custom-arguments...]"
echo " $(basename $dev_global_self) [--help] [--version]"
echo ""
echo "Optional arguments:"
echo " --help Help topic and usage information"
echo " --trace Print command traces before executing command"
echo " --verbose Produce more output about what the program does"
echo " --version Output version information and exit"

#
# Append your custom help here.
#
echo " --key=value Example key and value"
}

dev_kernel_optional_arguments() {
# %: most right
# %%: most left
# #: most left
# ##: most right
dev_arg_key=${1%%=*}
dev_arg_val="${1#*=}"

case "$dev_arg_key" in
--help)
dev_kernel_help_usage
exit 0
;;
--version)
echo "$dev_global_version" >&2
exit 0
;;
--trace)
declare -gr dev_global_trace="1"
;;
--verbose)
declare -gr dev_global_verbose="1"
;;

#
# Add custom optional arguments here.
#
--key)
declare -gr demo_key="$dev_arg_val"
;;

*)
echo "Optional argument not found: $1" >&2
;;
esac
}

dev_main() {
while [[ $# -gt 0 && "${1:0:1}" == "-" ]]; do
dev_kernel_optional_arguments "$1"
shift
done

[[ "$dev_global_trace" -eq "1" ]] && set -o xtrace
cmd_body $@
}

dev_main $@
50 changes: 50 additions & 0 deletions compact.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
# Copyright 2020 Liu Dng. All rights reserved.
# Use of this source code is governed by Apache License
# that can be found in the LICENSE file.

. lib/framework.bash

#
# Declare custom global variables
#


cmd_body() {
#
# Write your own source code here.
#
echo "This is a sample bash script."
echo "--key = $demo_key"
echo "Custom arguments: $@"
}

dev_custom_help_usage() {
#
# Append your custom help here.
#
echo " --key=value Example key and value"
}

dev_custom_optional_arguments() {
case "$dev_arg_key" in
#
# Add custom optional arguments here.
#
--key)
declare -gr demo_key="$dev_arg_val"
;;
*)
echo "Optional argument not found: $1" >&2
;;
esac
}

dev_custom_validation() {
if [[ -z $demo_key ]]; then
echo "Error: --key is required." >&2
return 1
fi
}

dev_main $@
77 changes: 77 additions & 0 deletions lib/framework.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash
# Copyright 2020 Liu Dng. All rights reserved.
# Use of this source code is governed by Apache License
# that can be found in the LICENSE file.

# trace ERR through pipes
set -o pipefail

# set -e : exit the script if any statement returns a non-true return value
set -o errexit

# The dev package version
declare -gr dev_global_version="1.0.0"

# The dev execution file path
declare -gr dev_global_self="$(realpath $0)"

# The dev execution base path
declare -gr dev_global_base="$(dirname $(dirname $dev_global_self))"

declare -g dev_arg_key

declare -g dev_arg_val

dev_kernel_help_usage() {
echo "Usage: $(basename $dev_global_self) [--trace] [--verbose] [custom-arguments...]"
echo " $(basename $dev_global_self) [--help] [--version]"
echo ""
echo "Optional arguments:"
echo " --help Help topic and usage information"
echo " --trace Print command traces before executing command"
echo " --verbose Produce more output about what the program does"
echo " --version Output version information and exit"

dev_custom_help_usage
}

dev_kernel_optional_arguments() {
# %: most right
# %%: most left
# #: most left
# ##: most right
dev_arg_key=${1%%=*}
dev_arg_val="${1#*=}"

case "$dev_arg_key" in
--help)
dev_kernel_help_usage
exit 0
;;
--version)
echo "$dev_global_version" >&2
exit 0
;;
--trace)
declare -gr dev_global_trace="1"
;;
--verbose)
declare -gr dev_global_verbose="1"
;;
*)
dev_custom_optional_arguments $1
;;
esac
}

dev_main() {
while [[ $# -gt 0 && "${1:0:1}" == "-" ]]; do
dev_kernel_optional_arguments "$1"
shift
done

dev_custom_validation

[[ "$dev_global_trace" -eq "1" ]] && set -o xtrace
cmd_body $@
}
63 changes: 8 additions & 55 deletions simple.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Copyright 2020 Liudng. All rights reserved.
# Copyright 2020 Liu Dng. All rights reserved.
# Use of this source code is governed by Apache License
# that can be found in the LICENSE file.

Expand All @@ -18,64 +18,17 @@ declare -gr dev_global_self="$(realpath $0)"
# The dev execution base path
declare -gr dev_global_base="$(dirname $(dirname $dev_global_self))"

#
# Declare custom global variables
#


cmd_body() {
#
# Write your code here.
# Write your own source code here.
#
echo "This is a sample bash script."
echo "Custom arguments: $@"
}

dev_kernel_help_usage() {
echo "Usage: $(basename $dev_global_self) [--trace] [--verbose] [custom-arguments...]"
echo " $(basename $dev_global_self) [--help] [--version]"
echo ""
echo "Optional arguments:"
echo " --help Help topic and usage information"
echo " --trace Print command traces before executing command"
echo " --verbose Produce more output about what the program does"
echo " --version Output version information and exit"

#
# Append your custom help here.
#
}

dev_kernel_optional_arguments() {
case "$1" in
--help)
dev_kernel_help_usage
exit 0
;;
--version)
echo "$dev_global_version" >&2
exit 0
;;
--trace)
declare -gr dev_global_trace="1"
;;
--verbose)
declare -gr dev_global_verbose="1"
;;

#
# Add your custom optional arguments here.
#

*)
echo "Optional argument not found: $1" >&2
;;
esac
}

dev_main() {
while [[ $# -gt 0 && "${1:0:1}" == "-" ]]; do
dev_kernel_optional_arguments "$1"
shift
done

[[ "$dev_global_trace" -eq "1" ]] && set -o xtrace
cmd_body $@
}

dev_main $@
cmd_body $@

0 comments on commit 5daf6db

Please sign in to comment.