1*63d4e48fSSadaf Ebrahimi# Coding Standards 2*63d4e48fSSadaf Ebrahimi 3*63d4e48fSSadaf EbrahimishFlags is more than just a simple 20 line shell script. It is a significant library of shell code that at first glance might not seam easy to understand. To improve code readability and usability, these guidelines are followed to help make the code more understandable for anyone who wants to read or modify it. 4*63d4e48fSSadaf Ebrahimi 5*63d4e48fSSadaf Ebrahimi[TOC] 6*63d4e48fSSadaf Ebrahimi 7*63d4e48fSSadaf Ebrahimi## Function declaration 8*63d4e48fSSadaf Ebrahimi 9*63d4e48fSSadaf EbrahimiDeclare functions using the following form: 10*63d4e48fSSadaf Ebrahimi 11*63d4e48fSSadaf Ebrahimi```sh 12*63d4e48fSSadaf EbrahimidoSomething() { 13*63d4e48fSSadaf Ebrahimi echo 'done!' 14*63d4e48fSSadaf Ebrahimi} 15*63d4e48fSSadaf Ebrahimi``` 16*63d4e48fSSadaf Ebrahimi 17*63d4e48fSSadaf EbrahimiOne-line functions are allowed if they are for a single command. 18*63d4e48fSSadaf Ebrahimi 19*63d4e48fSSadaf Ebrahimi```sh 20*63d4e48fSSadaf EbrahimidoSomething() { echo 'done!'; } 21*63d4e48fSSadaf Ebrahimi``` 22*63d4e48fSSadaf Ebrahimi 23*63d4e48fSSadaf Ebrahimi## Function documentation 24*63d4e48fSSadaf Ebrahimi 25*63d4e48fSSadaf EbrahimiEach function should be preceded by a header that provides the following: 26*63d4e48fSSadaf Ebrahimi 27*63d4e48fSSadaf Ebrahimi1. A one-sentence summary of what the function does. 28*63d4e48fSSadaf Ebrahimi 29*63d4e48fSSadaf Ebrahimi1. (optional) A longer description of what the function does, and perhaps some special information that helps convey its usage better. 30*63d4e48fSSadaf Ebrahimi 31*63d4e48fSSadaf Ebrahimi1. Args: a one-line summary of each argument of the form: 32*63d4e48fSSadaf Ebrahimi 33*63d4e48fSSadaf Ebrahimi `name: type: description` 34*63d4e48fSSadaf Ebrahimi 35*63d4e48fSSadaf Ebrahimi1. Output: a one-line summary of the output provided. Only output to STDOUT must be documented, unless the output to STDERR is of significance (i.e. not just an error message). The output should be of the form: 36*63d4e48fSSadaf Ebrahimi 37*63d4e48fSSadaf Ebrahimi `type: description` 38*63d4e48fSSadaf Ebrahimi 39*63d4e48fSSadaf Ebrahimi1. Returns: a one-line summary of the value returned. Returns in shell are always integers, but if the output is a true/false for success (i.e. a boolean), it should be noted. The output should be of the form: 40*63d4e48fSSadaf Ebrahimi 41*63d4e48fSSadaf Ebrahimi `type: description` 42*63d4e48fSSadaf Ebrahimi 43*63d4e48fSSadaf EbrahimiHere is a sample header: 44*63d4e48fSSadaf Ebrahimi 45*63d4e48fSSadaf Ebrahimi``` 46*63d4e48fSSadaf Ebrahimi# Return valid getopt options using currently defined list of long options. 47*63d4e48fSSadaf Ebrahimi# 48*63d4e48fSSadaf Ebrahimi# This function builds a proper getopt option string for short (and long) 49*63d4e48fSSadaf Ebrahimi# options, using the current list of long options for reference. 50*63d4e48fSSadaf Ebrahimi# 51*63d4e48fSSadaf Ebrahimi# Args: 52*63d4e48fSSadaf Ebrahimi# _flags_optStr: integer: option string type (__FLAGS_OPTSTR_*) 53*63d4e48fSSadaf Ebrahimi# Output: 54*63d4e48fSSadaf Ebrahimi# string: generated option string for getopt 55*63d4e48fSSadaf Ebrahimi# Returns: 56*63d4e48fSSadaf Ebrahimi# boolean: success of operation (always returns True) 57*63d4e48fSSadaf Ebrahimi``` 58*63d4e48fSSadaf Ebrahimi 59*63d4e48fSSadaf Ebrahimi## Variable and function names 60*63d4e48fSSadaf Ebrahimi 61*63d4e48fSSadaf EbrahimiAll shFlags specific constants, variables, and functions will be prefixed appropriately with `flags`. This is to distinguish usage in the shFlags code from users own scripts so that the shell name space remains predictable to users. The exceptions here are the standard `assertEquals`, etc., functions. 62*63d4e48fSSadaf Ebrahimi 63*63d4e48fSSadaf EbrahimiAll non built-in constants and variables will be surrounded with squiggle brackets, e.g. `${flags_someVariable}` to improve code readability. 64*63d4e48fSSadaf Ebrahimi 65*63d4e48fSSadaf EbrahimiDue to some shells not supporting local variables in functions, care in the naming and use of variables, both public and private, is very important. Accidental overriding of the variables can occur easily if care is not taken as all variables are technically global variables in some shells. 66*63d4e48fSSadaf Ebrahimi 67*63d4e48fSSadaf EbrahimiType | Sample 68*63d4e48fSSadaf Ebrahimi-------------------------------- | --------------------- 69*63d4e48fSSadaf Ebrahimiglobal public constant | `FLAGS_TRUE` 70*63d4e48fSSadaf Ebrahimiglobal private constant | `__FLAGS_SHELL_FLAGS` 71*63d4e48fSSadaf Ebrahimiglobal public variable | `flags_variable` 72*63d4e48fSSadaf Ebrahimiglobal private variable | `__flags_variable` 73*63d4e48fSSadaf Ebrahimiglobal macro | `_FLAGS_SOME_MACRO_` 74*63d4e48fSSadaf Ebrahimipublic function | `flags_function` 75*63d4e48fSSadaf Ebrahimipublic function, local variable | `flags_variable_` 76*63d4e48fSSadaf Ebrahimiprivate function | `_flags_function` 77*63d4e48fSSadaf Ebrahimiprivate function, local variable | `_flags_variable_` 78*63d4e48fSSadaf Ebrahimi 79*63d4e48fSSadaf EbrahimiWhere it makes sense to improve readability, variables can have the first letter of the second and later words capitalized. For example, the local variable name for the help string length is `flags_helpStrLen_`. 80*63d4e48fSSadaf Ebrahimi 81*63d4e48fSSadaf EbrahimiThere are three special-case global public variables used. They are used due to overcome the limitations of shell scoping or to prevent forking. The three variables are: 82*63d4e48fSSadaf Ebrahimi 83*63d4e48fSSadaf Ebrahimi- `flags_error` 84*63d4e48fSSadaf Ebrahimi- `flags_output` 85*63d4e48fSSadaf Ebrahimi- `flags_return` 86*63d4e48fSSadaf Ebrahimi 87*63d4e48fSSadaf Ebrahimi## Local variable cleanup 88*63d4e48fSSadaf Ebrahimi 89*63d4e48fSSadaf EbrahimiAs many shells do not support local variables, no support for cleanup of variables is present either. As such, all variables local to a function must be cleared up with the `unset` built-in command at the end of each function. 90*63d4e48fSSadaf Ebrahimi 91*63d4e48fSSadaf Ebrahimi## Indentation 92*63d4e48fSSadaf Ebrahimi 93*63d4e48fSSadaf EbrahimiCode block indentation is two (2) spaces, and tabs may not be used. 94*63d4e48fSSadaf Ebrahimi 95*63d4e48fSSadaf Ebrahimi```sh 96*63d4e48fSSadaf Ebrahimiif [ -z 'some string' ]; then 97*63d4e48fSSadaf Ebrahimi someFunction 98*63d4e48fSSadaf Ebrahimifi 99*63d4e48fSSadaf Ebrahimi``` 100*63d4e48fSSadaf Ebrahimi 101*63d4e48fSSadaf EbrahimiLines of code have no line limit, although the general preference is to wrap lines at reasonable boundaries (e.g., between if/then/else clauses). When long lines are wrapped using the backslash character '\', subsequent lines should be indented with four (4) spaces so as to differentiate from the standard spacing of two characters, and tabs may not be used. 102*63d4e48fSSadaf Ebrahimi 103*63d4e48fSSadaf Ebrahimi```sh 104*63d4e48fSSadaf Ebrahimifor x in some set of very long set of arguments that make for a very long \ 105*63d4e48fSSadaf Ebrahimi that extends much too long for one line 106*63d4e48fSSadaf Ebrahimido 107*63d4e48fSSadaf Ebrahimi echo ${x} 108*63d4e48fSSadaf Ebrahimidone 109*63d4e48fSSadaf Ebrahimi``` 110*63d4e48fSSadaf Ebrahimi 111*63d4e48fSSadaf EbrahimiWhen a conditional expression is written using the built-in `[` command, and that line must be wrapped, place the control `||` or `&&` operators on the same line as the expression where possible, with the list to be executed on its own line. 112*63d4e48fSSadaf Ebrahimi 113*63d4e48fSSadaf Ebrahimi```sh 114*63d4e48fSSadaf Ebrahimi[ -n 'some really long expression' -a -n 'some other long expr' ] && \ 115*63d4e48fSSadaf Ebrahimi echo 'that was actually true!' 116*63d4e48fSSadaf Ebrahimi``` 117