From 1b4365676f9bfc8d9e3d3c0e06611a618e2300cf Mon Sep 17 00:00:00 2001 From: Shamrock Lee <44064051+ShamrockLee@users.noreply.github.com> Date: Sat, 1 Jan 2022 21:40:13 +0800 Subject: [PATCH 1/4] optparse.bash: add function optparse.compose --- optparse.bash | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/optparse.bash b/optparse.bash index 8011213..f77f05a 100644 --- a/optparse.bash +++ b/optparse.bash @@ -95,13 +95,13 @@ function optparse.define(){ } # ----------------------------------------------------------------------------------------------------------------------------- -function optparse.build(){ - local build_file="$(mktemp -t "optparse-XXXXXX.tmp")" - - # Building getopts header here - - # Function usage - cat << EOF > $build_file +function optparse.compose(){ + local UNSET_GLOBAL=true + if [ "$#" -ge 0 ] && [ "$1" == "--no-unset" ]; then + UNSET_GLOBAL=false + fi + # Composing getopts header + cat << EOF | sed 's/#NL/\n/g' | sed 's/#TB/\t/g' function usage(){ cat << XXX usage: \$0 [OPTIONS] @@ -154,23 +154,27 @@ while getopts "$optparse_arguments_string" option; do esac done -# Clean up after self -rm $build_file - EOF + if $UNSET_GLOBAL; then + # Unset global variables + unset optparse_usage + unset optparse_process + unset optparse_arguments_string + unset optparse_defaults + unset optparse_contractions + fi +} - local -A o=( ['#NL']='\n' ['#TB']='\t' ) +# ----------------------------------------------------------------------------------------------------------------------------- +# Kept for backward compatibility (2022-01-01) +function optparse.build(){ + local build_file="$(mktemp -t "optparse-XXXXXX.tmp")" - for i in "${!o[@]}"; do - sed -i "s/${i}/${o[$i]}/g" $build_file - done + # Writing configurations into the getopts header + optparse.compose > "$build_file" - # Unset global variables - unset optparse_usage - unset optparse_process - unset optparse_arguments_string - unset optparse_defaults - unset optparse_contractions + # Clean up after self + echo 'rm $build_file' >> "$build_file" # Return file name to parent echo "$build_file" From b9754e215191157d6fb4b82c12a20eff1237521a Mon Sep 17 00:00:00 2001 From: Shamrock Lee <44064051+ShamrockLee@users.noreply.github.com> Date: Sat, 1 Jan 2022 23:31:37 +0800 Subject: [PATCH 2/4] optparse.bash: remove trailing whitespaces/tabs from generated header --- optparse.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optparse.bash b/optparse.bash index f77f05a..80044b6 100644 --- a/optparse.bash +++ b/optparse.bash @@ -101,7 +101,7 @@ function optparse.compose(){ UNSET_GLOBAL=false fi # Composing getopts header - cat << EOF | sed 's/#NL/\n/g' | sed 's/#TB/\t/g' + cat << EOF | sed 's/#NL/\n/g' | sed 's/#TB/\t/g' | sed 's/[ \t]+$//' function usage(){ cat << XXX usage: \$0 [OPTIONS] From 36b8a6ec0d28c5aaa7247a3e8fd3e250df8738c3 Mon Sep 17 00:00:00 2001 From: Shamrock Lee <44064051+ShamrockLee@users.noreply.github.com> Date: Sat, 1 Jan 2022 23:37:06 +0800 Subject: [PATCH 3/4] treewide: remove trailing whitespaces/tabs --- README.md | 22 +++++++++++----------- sample_head.sh | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 36c63ce..9f6a7e5 100644 --- a/README.md +++ b/README.md @@ -14,17 +14,17 @@ A wrapper that provides a clean and easy way to parse arguments to your BASH scr ##### See `sample_head.sh` for a demonstration of optparse ### 1. Define your arguments -Each argument to the script is defined with `optparse.define`, which specifies the option names, a short description, the variable it sets and the default value (if any). +Each argument to the script is defined with `optparse.define`, which specifies the option names, a short description, the variable it sets and the default value (if any). ```bash optparse.define short=f long=file desc="The input file" variable=filename ``` -Flags are defined in exactly the same way, but with an extra parameter `value` that is assigned to the variable. +Flags are defined in exactly the same way, but with an extra parameter `value` that is assigned to the variable. ```bash optparse.define short=v long=verbose desc="Set flag for verbose mode" variable=verbose_mode value=true default=false -``` +``` ### 2. Evaluate your arguments The `optparse.build` function creates a temporary header script based on the provided argument definitions. Simply source the file the function returns, to parse the arguments. @@ -35,16 +35,16 @@ source $( optparse.build ) #### That's it! The script can now make use of the variables. Running the script (without any arguments) should give you a neat usage description. - + usage: ./script.sh [OPTIONS] - + OPTIONS: - + -f --file : The input file -v --verbose : Set flag for verbose mode - + -? --help : usage - + ## Supported definition parameters All definition parameters for `optparse.define` are provided as `key=value` pairs, seperated by an `=` sign. #### `short` @@ -62,10 +62,10 @@ the default value to set the variable to if argument not specified ## Installation 1. Download/clone `optparse.bash` -2. Add +2. Add -```bash -`source /path/to/optparse.bash` +```bash +`source /path/to/optparse.bash` ``` to `~/.bashrc` diff --git a/sample_head.sh b/sample_head.sh index 03bf576..51d8729 100755 --- a/sample_head.sh +++ b/sample_head.sh @@ -17,7 +17,7 @@ fi # Display arguments if [ "$verbose_mode" = "true" ]; then - echo "Verbose mode ON" + echo "Verbose mode ON" echo "FILE : $file" echo "OUTPUT: $output" echo "LINES : $lines" @@ -36,4 +36,4 @@ cat $file | head -n $lines > $output if [ "$verbose_mode" = "true" ]; then echo "Done."; fi exit 0 - + From 5e72058b8c0518003789514f12cce5a93f158ab5 Mon Sep 17 00:00:00 2001 From: Shamrock Lee <44064051+ShamrockLee@users.noreply.github.com> Date: Sat, 1 Jan 2022 23:57:06 +0800 Subject: [PATCH 4/4] Use optparse.compose instead of optparse.build in README and sample_head.sh --- README.md | 16 +++++++++++++++- sample_head.sh | 2 +- sample_head_legacy.sh | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100755 sample_head_legacy.sh diff --git a/README.md b/README.md index 9f6a7e5..4459bc5 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,21 @@ optparse.define short=v long=verbose desc="Set flag for verbose mode" variable=v ``` ### 2. Evaluate your arguments -The `optparse.build` function creates a temporary header script based on the provided argument definitions. Simply source the file the function returns, to parse the arguments. +The `optparse.compose` function prints out the header content based on the provided argument definitions. Simply evaluate the result to parse the arguments. + +```bash +eval "$(optparse.compose)" +``` + +If you don't want to have `eval` in your script, source it like: + +```bash +source <<( optparse.compose ) +``` + +To keep backward compatibility, the `optparse.build` function creates a temporary header script containing the result printed by `optparse.compose`, plus a command to delete itself after sourcing. + +Previous scripts source the file the function returns to parse the arguments. ```bash source $( optparse.build ) diff --git a/sample_head.sh b/sample_head.sh index 51d8729..acb3d4c 100755 --- a/sample_head.sh +++ b/sample_head.sh @@ -8,7 +8,7 @@ optparse.define short=o long=output desc="The output file" variable=output defau optparse.define short=l long=lines desc="The number of lines to head (default:5)" variable=lines default=5 optparse.define short=v long=verbose desc="Flag to set verbose mode on" variable=verbose_mode value=true default=false # Source the output file ---------------------------------------------------------- -source $( optparse.build ) +eval "$(optparse.compose)" if [ "$file" == "" ]; then echo "ERROR: Please provide a file" diff --git a/sample_head_legacy.sh b/sample_head_legacy.sh new file mode 100755 index 0000000..51d8729 --- /dev/null +++ b/sample_head_legacy.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +# Source the optparse.bash file --------------------------------------------------- +source optparse.bash +# Define options +optparse.define short=f long=file desc="The file to process" variable=file +optparse.define short=o long=output desc="The output file" variable=output default=head_output.txt +optparse.define short=l long=lines desc="The number of lines to head (default:5)" variable=lines default=5 +optparse.define short=v long=verbose desc="Flag to set verbose mode on" variable=verbose_mode value=true default=false +# Source the output file ---------------------------------------------------------- +source $( optparse.build ) + +if [ "$file" == "" ]; then + echo "ERROR: Please provide a file" + exit 1 +fi + +# Display arguments +if [ "$verbose_mode" = "true" ]; then + echo "Verbose mode ON" + echo "FILE : $file" + echo "OUTPUT: $output" + echo "LINES : $lines" +fi + +# Check if input file exists +if [ "$verbose_mode" = "true" ]; then echo "Checking input file $file..." ; fi +if [ ! -f $file ]; then + echo "File does not exist" + exit 1 +fi + +if [ "$verbose_mode" = "true" ]; then echo "Heading first $lines lines into $output..." ; fi +cat $file | head -n $lines > $output + +if [ "$verbose_mode" = "true" ]; then echo "Done."; fi + +exit 0 +