-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpowershell_stylingguide.json
More file actions
185 lines (185 loc) · 5.21 KB
/
powershell_stylingguide.json
File metadata and controls
185 lines (185 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{
"stylingGuide": {
"keywordsAndSyntax": {
"helpKeywords": {
"rules": "Keywords in comment-based help must be uppercase",
"examples": [
".SYNOPSIS",
".DESCRIPTION",
".PARAMETER",
".EXAMPLE",
".INPUTS",
".OUTPUTS",
".NOTES"
]
}
},
"namingConventions": {
"publicIdentifiers": {
"rules": "Use PascalCase for all public identifiers",
"examples": [
"function Get-Process {}",
"$Global:ConfigurationValue",
"$Script:LoggingPreference"
]
},
"functionNaming": {
"rules": "Use Verb-Noun format with PascalCase for both parts",
"examples": [
"function Get-UserProfile {}",
"function Start-ProcessManager {}",
"function Update-ServiceConfiguration {}"
]
},
"twoLetterAcronyms": {
"rules": "Capitalize both letters in two-letter acronyms",
"examples": [
"$PSDefaultParameterValues",
"Get-PSHost",
"$VMId"
]
}
},
"codeStructure": {
"braceStyle": {
"rules": {
"openingBrace": "Place at end of line",
"closingBrace": "Place at beginning of line",
"exception": "Small scriptblocks passed as parameters OR begin, process, end blocks"
},
"examples": [
"if ($value) {",
" # code",
"}",
"Get-Process | Where-Object { $_.CPU -gt 10 }"
]
},
"functionTemplate": {
"rules": "All functions must include CmdletBinding and standard blocks",
"example": [
"function Verb-Noun {",
" [CmdletBinding()]",
" param ()",
" begin {",
" }",
" process {",
" }",
" end {",
" }",
"}"
]
}
},
"formatting": {
"lineLength": {
"rules": "Maximum 115 characters per line",
"examples": [
"Get-ChildItem -Path $LongPath |",
" Where-Object { $_.Length -gt 100MB } |",
" Select-Object Name, Length"
]
},
"spacing": {
"rules": {
"parameters": "Single space around parameter names and operators",
"exceptions": ["Switch parameters", "Unary operators"]
},
"examples": [
"$result = Get-Content -Path $file -Wait:$true",
"$count++",
"$date = (Get-Date).AddDays(-1)"
]
},
"whitespace": {
"rules": {
"noTrailing": "No trailing whitespace",
"subexpressions": "Single space inside braces/parentheses",
"avoidUnnecessary": "No spaces inside parentheses/brackets"
},
"examples": [
"$( Get-Process ).Count",
"${variable}",
"function Get-Example { $value }"
]
}
},
"bestPractices": {
"returnValues": {
"rules": {
"noReturnKeyword": "Don't use return keyword",
"processBlockOutput": "Output objects in process block without the return keyword"
},
"example": [
"process {",
" $result",
"}"
]
},
"outputType": {
"rules": "Specify OutputType for advanced functions",
"examples": [
"[OutputType([System.String])]",
"[OutputType('System.IO.FileInfo', ParameterSetName = 'Path')]"
]
},
"parameterSets": {
"rules": "Always provide DefaultParameterSetName when using parameter sets",
"example": [
"[CmdletBinding(DefaultParameterSetName = 'Path')]",
"param (",
" [Parameter(ParameterSetName = 'Path')]",
" [string]$Path,",
" [Parameter(ParameterSetName = 'LiteralPath')]",
" [string]$LiteralPath",
")"
]
},
"parameterValidation": {
"rules": "Use parameter validation attributes instead of body validation",
"examples": {
"allowNull": [
"param (",
" [AllowNull()]",
" [string]$ComputerName",
")"
],
"validateRange": [
"param (",
" [ValidateRange(0, 100)]",
" [int]$Percentage",
")"
],
"validateSet": [
"param (",
" [ValidateSet('Low', 'Medium', 'High')]",
" [string]$Priority",
")"
]
}
}
},
"documentation": {
"commentBasedHelp": {
"rules": "Place comment-based help inside function at the beginning. All functions must have comment-based help",
"example": [
"function Get-Example {",
" <#",
" .SYNOPSIS",
" Brief description",
" .DESCRIPTION",
" Detailed description",
" .PARAMETER Name",
" Parameter description",
" .EXAMPLE",
" Example usage",
" #>",
" [CmdletBinding()]",
" param ()",
" process {",
" }",
"}"
]
}
}
}
}