├── LICENSE ├── README.md ├── base.sh ├── examples ├── git.sh ├── helloworld.sh └── helloworldWith2StringOptions.sh ├── index.html └── screenshot └── demo.gif /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Vorachet Jaroensawas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Bash shell script template for readability CLI 3 | 4 | # Features 5 | 6 | * **bash-cli-template** allows you writing a readability CLI script 7 | * **bash-cli-template** provides built-in validation for mandatory and optional command parameters 8 | * **bash-cli-template** eases the script user can understand command usage with built-in readability CLI flow. 9 | * **bash-cli-template** provides built-in debug and help command 10 | * **bash-cli-template** provides easy steps to define commands and to implement the body of your command execution 11 | 12 | # Learning by example 13 | 14 | ## Running script example 15 | 16 | The script example is available in folder "examples". You can run "helloworld.sh" with the following steps. 17 | 18 | ``` 19 | $ git clone https://github.com/vorachet/bash-cli-template.git 20 | $ cd bash-cli-template/examples 21 | $ ./helloworld.sh 22 | ``` 23 | 24 | ### Your first CLI script 25 | 26 | Writing CLI script with **bash-cli-template** requires configuration of the following shell constants. 27 | 28 | You have to define value for all constants in making structure of the CLI. **bash-cli-template** will provide interface and basic flows of the CLI automatically based on the given constant values. 29 | 30 | 1. BASH_CLI_SCRIPT_NAME 31 | 2. BASH_CLI_OPT_NAME 32 | 3. BASH_CLI_OPT_ALT_NAME 33 | 4. BASH_CLI_OPT_DATA_TYPE 34 | 5. BASH_CLI_MANDATORY_PARAM 35 | 6. BASH_CLI_NON_MANDATORY_PARAM 36 | 7. BASH_CLI_OPT_DESC 37 | 38 | 39 | #### Requirements of helloworld.sh 40 | 41 | ![Logo](https://github.com/vorachet/bash-cli-template/raw/master/screenshot/demo.gif) 42 | 43 | 44 | #### Writing helloworld.sh 45 | 46 | File: helloworld.sh (The finished code is available in examples folder) 47 | 48 | ``` 49 | #!/bin/bash 50 | 51 | # Implementation of helloword script 52 | 53 | # Script name 54 | BASH_CLI_SCRIPT_NAME="helloworld.sh" 55 | 56 | # Option name 57 | BASH_CLI_OPT_NAME[0]="-t" 58 | BASH_CLI_OPT_NAME[1]="-u" 59 | BASH_CLI_OPT_NAME[2]="-l" 60 | BASH_CLI_OPT_NAME[3]="hello" 61 | 62 | # Alternative option name 63 | BASH_CLI_OPT_ALT_NAME[0]="--text" 64 | BASH_CLI_OPT_ALT_NAME[1]="--uppercase" 65 | BASH_CLI_OPT_ALT_NAME[2]="--lowercase" 66 | BASH_CLI_OPT_ALT_NAME[3]="helloworld" 67 | 68 | # Data type consists of string, boolean, and cmd. 69 | # - string does not allow you set empty option value 70 | # - object allows you flag the option without giving value 71 | # - cmd is the command used in various situations in your script. 72 | BASH_CLI_OPT_DATA_TYPE[0]="string" 73 | BASH_CLI_OPT_DATA_TYPE[1]="boolean" 74 | BASH_CLI_OPT_DATA_TYPE[2]="boolean" 75 | BASH_CLI_OPT_DATA_TYPE[3]="cmd" 76 | 77 | # Setting mandatory and optional parameters for cmd "hello" 78 | # A cmd data type may require one or more mandatory and optional parameters 79 | # 80 | # Requirements of hellworld.sh 81 | # 82 | # "-t | --text" is a mandatory parameter of the cmd "hello" 83 | # "-u | --uppercase" is an optional parameter of the cmd "hello" 84 | # "-l | --lowercase" is an optional parameter of the cmd "hello" 85 | # 86 | # Reference numbers for setting mandatory and non-mandatory parameters 87 | # 88 | # - The index reference number of BASH_CLI_OPT_NAME[3]="hello" is 3 89 | # - The array index of BASH_CLI_OPT_NAME[0]="-t" is 0 90 | # - The array index of BASH_CLI_OPT_NAME[1]="-u" is 1 91 | # - The array index of BASH_CLI_OPT_NAME[2]="-l" is 2 92 | # 93 | # Mandatory parameter: 94 | # - BASH_CLI_OPT_NAME[3]="hello" requires BASH_CLI_OPT_NAME[0]="-t" 95 | # 96 | # Non-Mandatory parameters: 97 | # - BASH_CLI_OPT_NAME[3]="hello" requires BASH_CLI_OPT_NAME[1]="-u" 98 | # - BASH_CLI_OPT_NAME[3]="hello" requires BASH_CLI_OPT_NAME[2]="-l" 99 | # 100 | BASH_CLI_MANDATORY_PARAM[3]="0" 101 | BASH_CLI_NON_MANDATORY_PARAM[3]="1,2" 102 | 103 | # Setting description of the option 104 | BASH_CLI_OPT_DESC[0]="text" 105 | BASH_CLI_OPT_DESC[1]="use uppercase" 106 | BASH_CLI_OPT_DESC[2]="use lowercase" 107 | BASH_CLI_OPT_DESC[3]="To print text from the value of -t" 108 | 109 | # Implementation of "hello" command 110 | # 111 | # Getting parameter values 112 | # 113 | # BASH_CLI_OPT_VALUE[] is an array variable that declared by the template base.sh 114 | # The value of BASH_CLI_OPT_VALUE[] will be managed by the template 115 | # 116 | hello() { 117 | # -t | --text 118 | local text=${BASH_CLI_OPT_VALUE[0]} 119 | # -u | --uppercase 120 | local enabled_uppercase=${BASH_CLI_OPT_VALUE[1]} 121 | # -l | --lowercase 122 | local enabled_lowercase=${BASH_CLI_OPT_VALUE[2]} 123 | 124 | if [ ${enabled_uppercase} == true ]; then 125 | echo "${text}" | tr '[:lower:]' '[:upper:]' 126 | elif [ ${enabled_lowercase} == true ]; then 127 | echo "${text}" | tr '[:upper:]' '[:lower:]' 128 | else 129 | echo "${text}" 130 | fi 131 | 132 | exit 133 | } 134 | 135 | source ../base.sh 136 | 137 | ``` 138 | 139 | # License 140 | 141 | The MIT License (MIT) 142 | 143 | Copyright (c) 2015 Vorachet Jaroensawas 144 | 145 | Permission is hereby granted, free of charge, to any person obtaining a copy 146 | of this software and associated documentation files (the "Software"), to deal 147 | in the Software without restriction, including without limitation the rights 148 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 149 | copies of the Software, and to permit persons to whom the Software is 150 | furnished to do so, subject to the following conditions: 151 | 152 | The above copyright notice and this permission notice shall be included in all 153 | copies or substantial portions of the Software. 154 | 155 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 156 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 157 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 158 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 159 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 160 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 161 | SOFTWARE. 162 | -------------------------------------------------------------------------------- /base.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | # Bash shell script template for readability CLI 3 | # https://github.com/vorachet/bash-cli-template 4 | # MIT License 5 | # Tested with GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15) 6 | 7 | BASH_CLI_ALL_ARGS=${@:2} 8 | BASH_CLI_CURRENT_CMD_INDEX=0 9 | 10 | for i in "${!BASH_CLI_OPT_NAME[@]}" 11 | do 12 | if [ ! "${BASH_CLI_OPT_DATA_TYPE[$i]}" ]; then 13 | echo "BASH_CLI_OPT_DATA_TYPE[$i]=\"\" empty string is not allowed." 14 | echo "The value must be one of these: \"string\", \"boolean\", or \"cmd\"" 15 | exit 16 | else 17 | if [ "${BASH_CLI_OPT_DATA_TYPE[$i]}" == "string" ] ; then 18 | BASH_CLI_OPT_VALUE[$i]="" 19 | elif [ "${BASH_CLI_OPT_DATA_TYPE[$i]}" == "boolean" ] ; then 20 | BASH_CLI_OPT_VALUE[$i]=false 21 | elif [ "${BASH_CLI_OPT_DATA_TYPE[$i]}" == "cmd" ] ; then 22 | BASH_CLI_OPT_VALUE[$i]="wait" 23 | else 24 | echo "BASH_CLI_OPT_DATA_TYPE[$i]=${BASH_CLI_OPT_DATA_TYPE[$i]} is not allowed." 25 | echo "The value must be one of these: \"string\", \"boolean\", or \"cmd\"" 26 | exit 27 | fi 28 | fi 29 | done 30 | 31 | SPECLINES="" 32 | for i in "${!BASH_CLI_OPT_NAME[@]}" 33 | do 34 | if [ "${BASH_CLI_OPT_DATA_TYPE[$i]}" != "cmd" ]; then 35 | SPECLINES+=" \t[${BASH_CLI_OPT_NAME[$i]}|${BASH_CLI_OPT_ALT_NAME[$i]} <${BASH_CLI_OPT_DESC[$i]}>]\n" 36 | fi 37 | done 38 | SPECLINES+=" \t[-h|--help]\n" 39 | 40 | SCRIPT_OPTIONS="" 41 | SCRIPT_CMDS="" 42 | for i in "${!BASH_CLI_OPT_DATA_TYPE[@]}" 43 | do 44 | if [ "${BASH_CLI_OPT_DATA_TYPE[$i]}" != "cmd" ]; then 45 | SCRIPT_OPTIONS="${SCRIPT_OPTIONS} 46 | \t ${BASH_CLI_OPT_NAME[$i]} \n" 47 | else 48 | 49 | SCRIPT_CMDS="${SCRIPT_CMDS} 50 | \t${BASH_CLI_OPT_NAME[$i]} | ${BASH_CLI_OPT_ALT_NAME[$i]} 51 | \t ${BASH_CLI_OPT_DESC[$i]} \n" 52 | fi 53 | done 54 | 55 | function help { 56 | echo -e "\n Usage: ./${BASH_CLI_SCRIPT_NAME} \n\n ${SPECLINES} 57 | \tThese are common ${BASH_CLI_SCRIPT_NAME} commands used in various situations: 58 | ${SCRIPT_CMDS} 59 | " 60 | } 61 | 62 | if [ $# -eq 0 ]; then 63 | help 64 | exit 65 | fi 66 | 67 | while [ "$1" != "" ]; do 68 | for i in ${!BASH_CLI_OPT_NAME[@]} 69 | do 70 | if [[ ( "${BASH_CLI_OPT_NAME[$i]}" == "$1" ) || 71 | ( "${BASH_CLI_OPT_ALT_NAME[$i]}" == "$1" ) ]] ; then 72 | if [ "${BASH_CLI_OPT_DATA_TYPE[$i]}" == "string" ] ; then 73 | if [[ ( ${2:0:1} == "-" ) || ( ${2:0:1} == "") ]] ; then 74 | BASH_CLI_OPT_VALUE[$i]='' 75 | else 76 | BASH_CLI_OPT_VALUE[$i]=$2 77 | shift 78 | fi 79 | fi 80 | 81 | if [ ${BASH_CLI_OPT_DATA_TYPE[$i]} == "boolean" ] ; then 82 | BASH_CLI_OPT_VALUE[$i]=true 83 | fi 84 | 85 | if [ ${BASH_CLI_OPT_DATA_TYPE[$i]} == "cmd" ] ; then 86 | BASH_CLI_OPT_VALUE[$i]="invoked" 87 | fi 88 | 89 | else 90 | if [[ ( "$1" == "-h" ) || ( "$1" == "--help" ) ]] ; then 91 | help 92 | exit 93 | fi 94 | 95 | fi 96 | done 97 | shift 98 | done 99 | 100 | validate_mandatory_options(){ 101 | local i 102 | for i in $(echo ${BASH_CLI_MANDATORY_PARAM[$BASH_CLI_CURRENT_CMD_INDEX]} | tr "," "\n") 103 | do 104 | if [ "${BASH_CLI_OPT_DATA_TYPE[$i]}" == "boolean" ]; then 105 | echo -e "\n Warning!! \n" 106 | echo -e "\t Please check your script implementation" 107 | echo -e "\t All mandatory options (BASH_CLI_MANDATORY_PARAM[]) must be configured with string datatype option" 108 | echo -e "\t BASH_CLI_OPT_NAME[$i]=\"${BASH_CLI_OPT_NAME[$i]}\" is currently using boolean data type and it does not allow to be a mandatory option" 109 | echo -e "\n" 110 | exit 111 | fi 112 | validate_string_parameter "${BASH_CLI_OPT_NAME[$i]}" "${BASH_CLI_OPT_VALUE[$i]}" "${BASH_CLI_OPT_DATA_TYPE[$i]}" "${BASH_CLI_OPT_DESC[$i]}" 113 | done 114 | } 115 | 116 | validate_string_parameter() { 117 | local pname=$1 118 | local pvalue=$2 119 | local pdatatype=$3 120 | local pdesc=$4 121 | if [ "${pvalue}" == "" ]; then 122 | echo -e "\t\tMissing mandatory option: \"${pname}\" (${pdesc}) is mandatory option" 123 | 124 | if [ "${pdatatype}" == "string" ]; then 125 | echo -e "\t\tExample: ./${BASH_CLI_SCRIPT_NAME} ${BASH_CLI_OPT_NAME[$BASH_CLI_CURRENT_CMD_INDEX]} ${pname} \"String Value\" " 126 | fi 127 | exit 128 | fi 129 | } 130 | 131 | show_optional_parameters() { 132 | local i 133 | local options 134 | for i in $(echo "${BASH_CLI_NON_MANDATORY_PARAM[$BASH_CLI_CURRENT_CMD_INDEX]}" | tr "," "\n") 135 | do 136 | options="${options} ${BASH_CLI_OPT_NAME[$i]}" 137 | done 138 | echo -e "\n\tAll optional parameters of this command: ${options}" 139 | 140 | echo -e "\tCurrent value:" 141 | for i in $(echo ${BASH_CLI_NON_MANDATORY_PARAM[$BASH_CLI_CURRENT_CMD_INDEX]} | tr "," "\n") 142 | do 143 | echo -e "\t\t${BASH_CLI_OPT_NAME[$i]} ${BASH_CLI_OPT_VALUE[$i]}" 144 | done 145 | } 146 | 147 | process() { 148 | local j 149 | 150 | for j in "${!BASH_CLI_OPT_NAME[@]}" 151 | do 152 | if [ "${BASH_CLI_OPT_DATA_TYPE[$j]}" == "cmd" ]; then 153 | if [ "${BASH_CLI_OPT_VALUE[$j]}" == "invoked" ]; then 154 | BASH_CLI_CURRENT_CMD_INDEX=$j 155 | validate_mandatory_options 156 | ${BASH_CLI_OPT_NAME[$BASH_CLI_CURRENT_CMD_INDEX]} 157 | # ${BASH_CLI_OPT_NAME[$BASH_CLI_CURRENT_CMD_INDEX]} "${BASH_CLI_ALL_ARGS}" 158 | break 159 | fi 160 | 161 | fi 162 | done 163 | } 164 | 165 | process $BASH_CLI_ALL_ARGS 166 | 167 | -------------------------------------------------------------------------------- /examples/git.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #!/bin/bash 4 | 5 | BASH_CLI_SCRIPT_NAME="git.sh" 6 | 7 | BASH_CLI_OPT_NAME[0]="-v" 8 | BASH_CLI_OPT_NAME[1]="-C" 9 | BASH_CLI_OPT_NAME[2]="-c" 10 | BASH_CLI_OPT_NAME[3]="-n" 11 | BASH_CLI_OPT_NAME[4]="-html" 12 | BASH_CLI_OPT_NAME[5]="-man" 13 | BASH_CLI_OPT_NAME[6]="-i" 14 | BASH_CLI_OPT_NAME[7]="-pg" 15 | BASH_CLI_OPT_NAME[8]="-npg" 16 | BASH_CLI_OPT_NAME[9]="-nrp" 17 | BASH_CLI_OPT_NAME[10]="-b" 18 | BASH_CLI_OPT_NAME[11]="-d" 19 | BASH_CLI_OPT_NAME[12]="-t" 20 | BASH_CLI_OPT_NAME[13]="-ns" 21 | BASH_CLI_OPT_NAME[14]="clone" 22 | BASH_CLI_OPT_NAME[15]="init" 23 | BASH_CLI_OPT_NAME[16]="add" 24 | BASH_CLI_OPT_NAME[17]="mv" 25 | BASH_CLI_OPT_NAME[18]="reset" 26 | BASH_CLI_OPT_NAME[19]="rm" 27 | BASH_CLI_OPT_NAME[20]="bisect" 28 | BASH_CLI_OPT_NAME[21]="grep" 29 | BASH_CLI_OPT_NAME[22]="log" 30 | BASH_CLI_OPT_NAME[23]="show" 31 | BASH_CLI_OPT_NAME[24]="status" 32 | BASH_CLI_OPT_NAME[25]="branch" 33 | BASH_CLI_OPT_NAME[26]="checkout" 34 | BASH_CLI_OPT_NAME[27]="commit" 35 | BASH_CLI_OPT_NAME[28]="diff" 36 | BASH_CLI_OPT_NAME[29]="merge" 37 | BASH_CLI_OPT_NAME[30]="rebase" 38 | BASH_CLI_OPT_NAME[31]="tag" 39 | BASH_CLI_OPT_NAME[32]="fetch" 40 | BASH_CLI_OPT_NAME[33]="pull" 41 | BASH_CLI_OPT_NAME[34]="push" 42 | 43 | BASH_CLI_OPT_ALT_NAME[0]="--version" 44 | BASH_CLI_OPT_ALT_NAME[1]="--path" 45 | BASH_CLI_OPT_ALT_NAME[2]="--name" 46 | BASH_CLI_OPT_ALT_NAME[3]="--exec-path" 47 | BASH_CLI_OPT_ALT_NAME[4]="--html-path" 48 | BASH_CLI_OPT_ALT_NAME[5]="--man-path" 49 | BASH_CLI_OPT_ALT_NAME[6]="--info-path" 50 | BASH_CLI_OPT_ALT_NAME[7]="--paginate" 51 | BASH_CLI_OPT_ALT_NAME[8]="--no-pager" 52 | BASH_CLI_OPT_ALT_NAME[9]="--no-replace-objects" 53 | BASH_CLI_OPT_ALT_NAME[10]="--bare" 54 | BASH_CLI_OPT_ALT_NAME[11]="--git-dir" 55 | BASH_CLI_OPT_ALT_NAME[12]="--work-tree" 56 | BASH_CLI_OPT_ALT_NAME[13]="--namespace" 57 | BASH_CLI_OPT_ALT_NAME[14]="cloneAsClone" 58 | BASH_CLI_OPT_ALT_NAME[15]="initAsClone" 59 | BASH_CLI_OPT_ALT_NAME[16]="addAsClone" 60 | BASH_CLI_OPT_ALT_NAME[17]="mvAsClone" 61 | BASH_CLI_OPT_ALT_NAME[18]="resetAsClone" 62 | BASH_CLI_OPT_ALT_NAME[19]="rmAsClone" 63 | BASH_CLI_OPT_ALT_NAME[20]="bisectAsClone" 64 | BASH_CLI_OPT_ALT_NAME[21]="grepAsClone" 65 | BASH_CLI_OPT_ALT_NAME[22]="logAsClone" 66 | BASH_CLI_OPT_ALT_NAME[23]="showAsClone" 67 | BASH_CLI_OPT_ALT_NAME[24]="statusAsClone" 68 | BASH_CLI_OPT_ALT_NAME[25]="branchAsClone" 69 | BASH_CLI_OPT_ALT_NAME[26]="checkoutAsClone" 70 | BASH_CLI_OPT_ALT_NAME[27]="commitAsClone" 71 | BASH_CLI_OPT_ALT_NAME[28]="diffAsClone" 72 | BASH_CLI_OPT_ALT_NAME[29]="mergeAsClone" 73 | BASH_CLI_OPT_ALT_NAME[30]="rebaseAsClone" 74 | BASH_CLI_OPT_ALT_NAME[31]="tagAsClone" 75 | BASH_CLI_OPT_ALT_NAME[32]="fetchAsClone" 76 | BASH_CLI_OPT_ALT_NAME[33]="pullAsClone" 77 | BASH_CLI_OPT_ALT_NAME[34]="pushAsClone" 78 | 79 | BASH_CLI_OPT_DATA_TYPE[0]="string" 80 | BASH_CLI_OPT_DATA_TYPE[1]="string" 81 | BASH_CLI_OPT_DATA_TYPE[2]="string" 82 | BASH_CLI_OPT_DATA_TYPE[3]="string" 83 | BASH_CLI_OPT_DATA_TYPE[4]="string" 84 | BASH_CLI_OPT_DATA_TYPE[5]="string" 85 | BASH_CLI_OPT_DATA_TYPE[6]="string" 86 | BASH_CLI_OPT_DATA_TYPE[7]="string" 87 | BASH_CLI_OPT_DATA_TYPE[8]="string" 88 | BASH_CLI_OPT_DATA_TYPE[9]="string" 89 | BASH_CLI_OPT_DATA_TYPE[10]="string" 90 | BASH_CLI_OPT_DATA_TYPE[11]="string" 91 | BASH_CLI_OPT_DATA_TYPE[12]="string" 92 | BASH_CLI_OPT_DATA_TYPE[13]="string" 93 | BASH_CLI_OPT_DATA_TYPE[14]="cmd" 94 | BASH_CLI_OPT_DATA_TYPE[15]="cmd" 95 | BASH_CLI_OPT_DATA_TYPE[16]="cmd" 96 | BASH_CLI_OPT_DATA_TYPE[17]="cmd" 97 | BASH_CLI_OPT_DATA_TYPE[18]="cmd" 98 | BASH_CLI_OPT_DATA_TYPE[19]="cmd" 99 | BASH_CLI_OPT_DATA_TYPE[20]="cmd" 100 | BASH_CLI_OPT_DATA_TYPE[21]="cmd" 101 | BASH_CLI_OPT_DATA_TYPE[22]="cmd" 102 | BASH_CLI_OPT_DATA_TYPE[23]="cmd" 103 | BASH_CLI_OPT_DATA_TYPE[24]="cmd" 104 | BASH_CLI_OPT_DATA_TYPE[25]="cmd" 105 | BASH_CLI_OPT_DATA_TYPE[26]="cmd" 106 | BASH_CLI_OPT_DATA_TYPE[27]="cmd" 107 | BASH_CLI_OPT_DATA_TYPE[28]="cmd" 108 | BASH_CLI_OPT_DATA_TYPE[29]="cmd" 109 | BASH_CLI_OPT_DATA_TYPE[30]="cmd" 110 | BASH_CLI_OPT_DATA_TYPE[31]="cmd" 111 | BASH_CLI_OPT_DATA_TYPE[32]="cmd" 112 | BASH_CLI_OPT_DATA_TYPE[33]="cmd" 113 | BASH_CLI_OPT_DATA_TYPE[34]="cmd" 114 | 115 | BASH_CLI_MANDATORY_PARAM[4]="0,1" 116 | BASH_CLI_NON_MANDATORY_PARAM[4]="2,3" 117 | 118 | BASH_CLI_OPT_DESC[0]="string" 119 | BASH_CLI_OPT_DESC[1]="string" 120 | BASH_CLI_OPT_DESC[2]="string" 121 | BASH_CLI_OPT_DESC[3]="string" 122 | BASH_CLI_OPT_DESC[4]="string" 123 | BASH_CLI_OPT_DESC[5]="string" 124 | BASH_CLI_OPT_DESC[6]="string" 125 | BASH_CLI_OPT_DESC[7]="string" 126 | BASH_CLI_OPT_DESC[8]="string" 127 | BASH_CLI_OPT_DESC[9]="string" 128 | BASH_CLI_OPT_DESC[10]="string" 129 | BASH_CLI_OPT_DESC[11]="string" 130 | BASH_CLI_OPT_DESC[12]="string" 131 | BASH_CLI_OPT_DESC[13]="string" 132 | BASH_CLI_OPT_DESC[14]="[start a working area] Clone a repository into a new directory" 133 | BASH_CLI_OPT_DESC[15]="[start a working area] Create an empty Git repository or reinitialize an existing one" 134 | BASH_CLI_OPT_DESC[16]="[work on the current change] Add file contents to the index" 135 | BASH_CLI_OPT_DESC[17]="[work on the current change] Move or rename a file, a directory, or a symlink" 136 | BASH_CLI_OPT_DESC[18]="[work on the current change] Reset current HEAD to the specified state" 137 | BASH_CLI_OPT_DESC[19]="[work on the current change] Remove files from the working tree and from the index" 138 | BASH_CLI_OPT_DESC[20]="[examine the history and state] Use binary search to find the commit that introduced a bug" 139 | BASH_CLI_OPT_DESC[21]="[examine the history and state] Print lines matching a pattern" 140 | BASH_CLI_OPT_DESC[22]="[examine the history and state] Show commit logs" 141 | BASH_CLI_OPT_DESC[23]="[examine the history and state] Show various types of objects" 142 | BASH_CLI_OPT_DESC[24]="[examine the history and state] Show the working tree status" 143 | BASH_CLI_OPT_DESC[25]="[grow, mark and tweak your common history] List, create, or delete branches" 144 | BASH_CLI_OPT_DESC[26]="[grow, mark and tweak your common history] Switch branches or restore working tree files" 145 | BASH_CLI_OPT_DESC[27]="[grow, mark and tweak your common history] Record changes to the repository" 146 | BASH_CLI_OPT_DESC[28]="[grow, mark and tweak your common history] Show changes between commits, commit and working tree, etc" 147 | BASH_CLI_OPT_DESC[29]="[grow, mark and tweak your common history] Join two or more development histories together" 148 | BASH_CLI_OPT_DESC[30]="[grow, mark and tweak your common history] Forward-port local commits to the updated upstream head" 149 | BASH_CLI_OPT_DESC[31]="[grow, mark and tweak your common history] Create, list, delete or verify a tag object signed with GPG" 150 | BASH_CLI_OPT_DESC[32]="[collaborate] Download objects and refs from another repository" 151 | BASH_CLI_OPT_DESC[33]="[collaborate] Fetch from and integrate with another repository or a local branch" 152 | BASH_CLI_OPT_DESC[34]="[collaborate] Update remote refs along with associated objects" 153 | 154 | clone() { 155 | echo "clone" 156 | } 157 | 158 | init() { 159 | echo "init" 160 | } 161 | 162 | add() { 163 | echo "add" 164 | } 165 | 166 | mv() { 167 | echo "mv" 168 | } 169 | 170 | reset() { 171 | echo "reset" 172 | } 173 | 174 | rm() { 175 | echo "rm" 176 | } 177 | 178 | bisect() { 179 | echo "bisect" 180 | } 181 | 182 | grep() { 183 | echo "grep" 184 | } 185 | 186 | log() { 187 | echo "log" 188 | } 189 | 190 | show() { 191 | echo "show" 192 | } 193 | 194 | status() { 195 | echo "status" 196 | } 197 | 198 | branch() { 199 | echo "branch" 200 | } 201 | 202 | checkout() { 203 | echo "checkout" 204 | } 205 | 206 | commit() { 207 | echo "commit" 208 | } 209 | 210 | diff() { 211 | echo "diff" 212 | } 213 | 214 | merge() { 215 | echo "merge" 216 | } 217 | 218 | rebase() { 219 | echo "rebase" 220 | } 221 | 222 | tag() { 223 | echo "tag" 224 | } 225 | 226 | fetch() { 227 | echo "fetch" 228 | } 229 | 230 | pull() { 231 | echo "pull" 232 | } 233 | 234 | push() { 235 | echo "push" 236 | } 237 | 238 | source ../base.sh -------------------------------------------------------------------------------- /examples/helloworld.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Implementation of helloword script 4 | 5 | # Script name 6 | BASH_CLI_SCRIPT_NAME="helloworld.sh" 7 | 8 | # Option name 9 | BASH_CLI_OPT_NAME[0]="-t" 10 | BASH_CLI_OPT_NAME[1]="-u" 11 | BASH_CLI_OPT_NAME[2]="-l" 12 | BASH_CLI_OPT_NAME[3]="hello" 13 | 14 | # Alternative option name 15 | BASH_CLI_OPT_ALT_NAME[0]="--text" 16 | BASH_CLI_OPT_ALT_NAME[1]="--uppercase" 17 | BASH_CLI_OPT_ALT_NAME[2]="--lowercase" 18 | BASH_CLI_OPT_ALT_NAME[3]="helloworld" 19 | 20 | # Data type consists of string, boolean, and cmd. 21 | # - string does not allow you set empty option value 22 | # - object allows you flag the option without giving value 23 | # - cmd is the command used in various situations in your script. 24 | BASH_CLI_OPT_DATA_TYPE[0]="string" 25 | BASH_CLI_OPT_DATA_TYPE[1]="boolean" 26 | BASH_CLI_OPT_DATA_TYPE[2]="boolean" 27 | BASH_CLI_OPT_DATA_TYPE[3]="cmd" 28 | 29 | # Setting mandatory and optional parameters for cmd "hello" 30 | # A cmd data type may require one or more mandatory and optional parameters 31 | # 32 | # Requirements of hellworld.sh 33 | # 34 | # "-t | --text" is a mandatory parameter of the cmd "hello" 35 | # "-u | --uppercase" is an optional parameter of the cmd "hello" 36 | # "-l | --lowercase" is an optional parameter of the cmd "hello" 37 | # 38 | # Reference numbers for setting mandatory and non-mandatory parameters 39 | # 40 | # - The index reference number of BASH_CLI_OPT_NAME[3]="hello" is 3 41 | # - The array index of BASH_CLI_OPT_NAME[0]="-t" is 0 42 | # - The array index of BASH_CLI_OPT_NAME[1]="-u" is 1 43 | # - The array index of BASH_CLI_OPT_NAME[2]="-l" is 2 44 | # 45 | # Mandatory parameter: 46 | # - BASH_CLI_OPT_NAME[3]="hello" requires BASH_CLI_OPT_NAME[0]="-t" 47 | # 48 | # Non-Mandatory parameters: 49 | # - BASH_CLI_OPT_NAME[3]="hello" requires BASH_CLI_OPT_NAME[1]="-u" 50 | # - BASH_CLI_OPT_NAME[3]="hello" requires BASH_CLI_OPT_NAME[2]="-l" 51 | # 52 | BASH_CLI_MANDATORY_PARAM[3]="0" 53 | BASH_CLI_NON_MANDATORY_PARAM[3]="1,2" 54 | 55 | # Setting description of the option 56 | BASH_CLI_OPT_DESC[0]="text" 57 | BASH_CLI_OPT_DESC[1]="use uppercase" 58 | BASH_CLI_OPT_DESC[2]="use lowercase" 59 | BASH_CLI_OPT_DESC[3]="To print text from the value of -t" 60 | 61 | # Implementation of "hello" command 62 | # 63 | # Getting parameter values 64 | # 65 | # BASH_CLI_OPT_VALUE[] is an array variable that declared by the template base.sh 66 | # The value of BASH_CLI_OPT_VALUE[] will be managed by the template 67 | # 68 | hello() { 69 | # -t | --text 70 | local text=${BASH_CLI_OPT_VALUE[0]} 71 | # -u | --uppercase 72 | local enabled_uppercase=${BASH_CLI_OPT_VALUE[1]} 73 | # -l | --lowercase 74 | local enabled_lowercase=${BASH_CLI_OPT_VALUE[2]} 75 | 76 | if [ ${enabled_uppercase} == true ]; then 77 | echo "${text}" | tr '[:lower:]' '[:upper:]' 78 | elif [ ${enabled_lowercase} == true ]; then 79 | echo "${text}" | tr '[:upper:]' '[:lower:]' 80 | else 81 | echo "${text}" 82 | fi 83 | 84 | exit 85 | } 86 | 87 | source ../base.sh -------------------------------------------------------------------------------- /examples/helloworldWith2StringOptions.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | BASH_CLI_SCRIPT_NAME="hellowordWith2StringOptions.sh" 4 | 5 | BASH_CLI_OPT_NAME[0]="-p" 6 | BASH_CLI_OPT_NAME[1]="-t" 7 | BASH_CLI_OPT_NAME[2]="-u" 8 | BASH_CLI_OPT_NAME[3]="-l" 9 | BASH_CLI_OPT_NAME[4]="hello" 10 | 11 | BASH_CLI_OPT_ALT_NAME[0]="--text" 12 | BASH_CLI_OPT_ALT_NAME[1]="--prefix" 13 | BASH_CLI_OPT_ALT_NAME[2]="--uppercase" 14 | BASH_CLI_OPT_ALT_NAME[3]="--lowercase" 15 | BASH_CLI_OPT_ALT_NAME[4]="helloworld" 16 | 17 | BASH_CLI_OPT_DATA_TYPE[0]="string" 18 | BASH_CLI_OPT_DATA_TYPE[1]="string" 19 | BASH_CLI_OPT_DATA_TYPE[2]="boolean" 20 | BASH_CLI_OPT_DATA_TYPE[3]="boolean" 21 | BASH_CLI_OPT_DATA_TYPE[4]="cmd" 22 | 23 | BASH_CLI_MANDATORY_PARAM[4]="0,1" 24 | BASH_CLI_NON_MANDATORY_PARAM[4]="2,3" 25 | 26 | BASH_CLI_OPT_DESC[0]="text" 27 | BASH_CLI_OPT_DESC[1]="prefix word to put infront of the given text" 28 | BASH_CLI_OPT_DESC[2]="use uppercase" 29 | BASH_CLI_OPT_DESC[3]="use lowercase" 30 | BASH_CLI_OPT_DESC[4]="To print text from the value of -t" 31 | 32 | hello() { 33 | local prefix=${BASH_CLI_OPT_VALUE[0]} 34 | local text=${BASH_CLI_OPT_VALUE[1]} 35 | local enabled_uppercase=${BASH_CLI_OPT_VALUE[2]} 36 | local enabled_lowercase=${BASH_CLI_OPT_VALUE[3]} 37 | 38 | if [ ${enabled_uppercase} == true ]; then 39 | echo "${prefix} ${text}" | tr '[:lower:]' '[:upper:]' 40 | elif [ ${enabled_lowercase} == true ]; then 41 | echo "${prefix} ${text}" | tr '[:upper:]' '[:lower:]' 42 | else 43 | echo "${prefix} ${text}" 44 | fi 45 | exit 46 | } 47 | 48 | source ../base.sh -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | SHCOV - / 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 54 | 55 | 56 |
SHCOV by Simon Kågström
18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 50 | 51 | 52 |
Current view:/
Date:2016-01-04Instrumented lines:1
Code covered:0.0 %Executed lines:0
Legend: 40 | 41 | Low: 0% to 15% 42 | 43 | 44 | Medium: 15% to 50% 45 | 46 | 47 | High: 50% to 100% 48 | 49 |
53 |
57 |
58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /screenshot/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vorachet/bash-cli-template/92c3bf9d8bd734a2bfaa432d02611d3ba9a87512/screenshot/demo.gif --------------------------------------------------------------------------------

Directory nameCoverage