xref: /aosp_15_r20/external/shflags/CONTRIBUTING.md (revision 63d4e48fb639f6414be0db9d718e3be2667e4fed)
1# Coding Standards
2
3shFlags 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
5[TOC]
6
7## Function declaration
8
9Declare functions using the following form:
10
11```sh
12doSomething() {
13  echo 'done!'
14}
15```
16
17One-line functions are allowed if they are for a single command.
18
19```sh
20doSomething() { echo 'done!'; }
21```
22
23## Function documentation
24
25Each function should be preceded by a header that provides the following:
26
271.  A one-sentence summary of what the function does.
28
291.  (optional) A longer description of what the function does, and perhaps some special information that helps convey its usage better.
30
311.  Args: a one-line summary of each argument of the form:
32
33    `name: type: description`
34
351.  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
37    `type: description`
38
391.  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
41    `type: description`
42
43Here is a sample header:
44
45```
46# Return valid getopt options using currently defined list of long options.
47#
48# This function builds a proper getopt option string for short (and long)
49# options, using the current list of long options for reference.
50#
51# Args:
52#   _flags_optStr: integer: option string type (__FLAGS_OPTSTR_*)
53# Output:
54#   string: generated option string for getopt
55# Returns:
56#   boolean: success of operation (always returns True)
57```
58
59## Variable and function names
60
61All 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
63All non built-in constants and variables will be surrounded with squiggle brackets, e.g. `${flags_someVariable}` to improve code readability.
64
65Due 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
67Type                             | Sample
68-------------------------------- | ---------------------
69global public constant           | `FLAGS_TRUE`
70global private constant          | `__FLAGS_SHELL_FLAGS`
71global public variable           | `flags_variable`
72global private variable          | `__flags_variable`
73global macro                     | `_FLAGS_SOME_MACRO_`
74public function                  | `flags_function`
75public function, local variable  | `flags_variable_`
76private function                 | `_flags_function`
77private function, local variable | `_flags_variable_`
78
79Where 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
81There 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
83-   `flags_error`
84-   `flags_output`
85-   `flags_return`
86
87## Local variable cleanup
88
89As 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
91## Indentation
92
93Code block indentation is two (2) spaces, and tabs may not be used.
94
95```sh
96if [ -z 'some string' ]; then
97  someFunction
98fi
99```
100
101Lines 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
103```sh
104for x in some set of very long set of arguments that make for a very long \
105    that extends much too long for one line
106do
107  echo ${x}
108done
109```
110
111When 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
113```sh
114[ -n 'some really long expression' -a -n 'some other long expr' ] && \
115    echo 'that was actually true!'
116```
117