public/Write-CmdletExample.ps1

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
function Write-CmdletExample() {
    Param(
        [Parameter(Position = 0)]
        [PsCustomObject] $Model,

        [Parameter(Position = 1)]
        [String] $HeaderTemplate,

        [Parameter(Position = 2)]
        [String] $ItemTemplate
    )

    $examples = $Model.Examples.examples
    if($examples -and $examples.Length) {
        if([String]::IsNullOrWhitespace($HeaderTemplate)) {
            $HeaderTemplate = "`n## Examples`n`n"

        }

        if([String]::IsNullOrWhitespace($ItemTemplate)) {
            $ItemTemplate = @"
### {0}
 
``````powershell
{1}
``````
 
{2}
"@


        }

        $out = ""
        $out += $HeaderTemplate;

        $examples.example | ForEach-Object {
            $model = Read-HelpExample -Model $_
            $code = $model.Code 
            $remarks = $model.Remarks
            $title = $model.Title 
            if($title) {
                $title = $model.Title.Trim("- ".ToCharArray())
            }
            

            $out += [String]::Format($ItemTemplate, $title, $code, $remarks)
        }

        return $out;
    }
   

    return "";
}