1
- Coding Style Guide
2
- ==================
1
+ = Coding Style Guide
2
+
3
+ == Introduction
3
4
4
- Introduction
5
- ------------
6
5
This document attempts to explain the basic styles and patterns that
7
6
are used in the bash completion. New code should try to conform to
8
7
these standards so that it is as easy to maintain as existing code.
@@ -14,43 +13,38 @@ codebase, who are in the process of getting their code reviewed.
14
13
Before getting a review, please read over this document and make
15
14
sure your code conforms to the recommendations here.
16
15
17
- Indentation
18
- -----------
16
+ == Indentation
17
+
19
18
Indent step should be 4 spaces, no tabs.
20
19
21
- Globbing in case labels
22
- -----------------------
20
+ == Globbing in case labels
23
21
24
22
Avoid "fancy" globbing in case labels, just use traditional style when
25
23
possible. For example, do "--foo|--bar)" instead of "--@(foo|bar))".
26
24
Rationale: the former is easier to read, often easier to grep, and
27
25
doesn't confuse editors as bad as the latter, and is concise enough.
28
26
29
- [[ ]] vs [ ]
30
- ----------------
27
+ == [[ ]] vs [ ]
31
28
32
29
Always use [[ ]] instead of [ ]. Rationale: the former is less error
33
30
prone, more featureful, and slightly faster.
34
31
35
- Line wrapping
36
- -------------
32
+ == Line wrapping
37
33
38
34
Try to wrap lines at 79 characters. Never go past this limit, unless
39
35
you absolutely need to (example: a long sed regular expression, or the
40
36
like). This also holds true for the documentation and the testsuite.
41
37
Other files, like ChangeLog, or COPYING, are exempt from this rule.
42
38
43
- $(...) vs \`...`
44
- ----------------
39
+ == $(...) vs \`...`
45
40
46
41
When you need to do some code substitution in your completion script,
47
42
you *MUST* use the $(...) construct, rather than the \`...`. The former
48
43
is preferable because anyone, with any keyboard layout, is able to
49
44
type it. Backticks aren't always available, without doing strange
50
45
key combinations.
51
46
52
- -o filenames
53
- ------------
47
+ == -o filenames
54
48
55
49
As a rule of thumb, do not use "complete -o filenames". Doing it makes
56
50
it take effect for all completions from the affected function, which
@@ -61,8 +55,7 @@ need that kind of processing (e.g. file and command names). The
61
55
_filedir and _filedir_xspec helpers do this automatically whenever
62
56
they return some completions.
63
57
64
- `[[ ${COMPREPLY-} == *= ]] && compopt -o nospace`
65
- -------------------------------------------------
58
+ == `[[ ${COMPREPLY-} == *= ]] && compopt -o nospace`
66
59
67
60
The above is functionally a shorthand for:
68
61
@@ -75,8 +68,7 @@ appended after the equal sign. Calling compopt -o nospace makes sense
75
68
in case completion actually occurs: when only one completion is
76
69
available in COMPREPLY.
77
70
78
- `$split && return`
79
- ------------------
71
+ == `$split && return`
80
72
81
73
Should be used in completions using the -s flag of _init_completion,
82
74
or other similar cases where _split_longopt has been invoked, after
@@ -95,31 +87,27 @@ explicitly handled (non-completed) in the $prev handling block because
95
87
--foo=bar options can often be written without the equals sign, and in
96
88
that case the long option splitting does not occur.
97
89
98
- Use arithmetic evaluation
99
- -------------------------
90
+ == Use arithmetic evaluation
100
91
101
92
When dealing with numeric data, take advantage of arithmetic evaluation.
102
93
In essence, use (( ... )) whenever it can replace [[ ... ]] because the
103
94
syntax is more readable; no need for $-prefixes, numeric comparison etc
104
95
operators are more familiar and easier on the eye.
105
96
106
- Array subscript access
107
- ----------------------
97
+ == Array subscript access
108
98
109
99
Array subscripts are arithmetic expressions, take advantage of that.
110
100
E.g. write ${foo[bar]}, not ${foo[$bar]}, and similarly ${foo[bar+1]}
111
101
vs ${foo[((bar+1))]} or ${foo[$((bar+1))]}, ${foo[--i]} vs ${foo[((--i))]}.
112
102
113
- Loop variable names
114
- -------------------
103
+ == Loop variable names
115
104
116
105
Use i, j, k for loop-local indices; n and m for lengths; some other descriptive
117
106
name typically based on array name but in singular when looping over actual
118
107
values. If an index or value is to be accessed later on instead of being just
119
108
locally for looping, use a more descriptive and specific name for it.
120
109
121
- Function names
122
- --------------
110
+ == Function names
123
111
124
112
Use the _comp_ prefix for all function names, and _comp_cmd_ for functions
125
113
defined in per command completion files and not anywhere else. Prefixing with
@@ -133,16 +121,13 @@ This is due to backwards compatibility reasons, but all functions introduced
133
121
after version 2.11 should follow this name prefix rule.
134
122
135
123
/////////////////////////////////////////
136
- case/esac vs if
137
- ---------------
138
124
139
- quoting
140
- -------
125
+ == case/esac vs if
126
+
127
+ == quoting
141
128
142
- awk vs cut for simple cases
143
- ---------------------------
129
+ == awk vs cut for simple cases
144
130
145
- variable naming
146
- ---------------
131
+ == variable naming
147
132
148
133
/////////////////////////////////////////
0 commit comments