Heavyweight Help in V1
Again, in V1 PSH, this is what I use for nicely formatted help. If you're on V2, by all means, use the inline help, PLEASE!
function Break-Line {
# synopsis::
# [-help]
#
# Description::
# Inserts linefeeds into strings read from STDIN so they wrap nicely
# for a given PowerShell window's screen geometry
#
# Usage::
# $data | Break-Line
param (
[switch]$help
);
if ($help) { return (Get-FunctionHelp -name $MyInvocation.MyCommand.Name); }
$width = $Host.UI.RawUI.WindowSize.Width - 1;
$input | % {
$_ = $_ -replace "`t", " ";
foreach ($line in $_.split("`n")) {
$line = $line -replace "\s*$";
$indent = $line -replace "(^\s*).*", '$1';
$line = $line -replace "^\s*";
while ($line.length -gt ($width - $indent.length)) {
$splitPoint = $width - $indent.length;
$buffer = $line.SubString(0, $splitPoint);
$offset = $buffer.lastIndexOf(" ")
if (($offset -ne -1) -and ($offet -ne ($buffer.length -1))) {
$splitPoint = $offset;
$buffer = $line.SubString(0, $splitPoint);
}
$line = $line.SubString($splitPoint, ($line.length - $splitPoint)) -replace "^\s*";
"$indent$buffer";
}
"$indent$line";
}
}
}
function Get-FunctionHelp {
# Synopsis::
# <-name <string>> [-help]
#
# Description::
# Extracts help text from header comments in functions and displays it.
# Function help is the first comment block in the scriptblock.
#
# usage::
# Get-FunctionHelp -name Get-FunctionHelp
param (
[string]$name = $null,
[switch]$help
)
if ($help) { return (Get-FunctionHelp -name $MyInvocation.MyCommand.Name); }
if (!$name) {
Write-Warning "Get-FunctionHelp -name not specified.";
return;
}
if (!(Test-Path "function:$name")) {
Write-Warning "Get-FunctionHelp -name $name not found in function: PSProvider.";
}
(Get-Content "function:$name").ToString().Split("`n") | Write-Help -name $name;
}
function Write-Help {
# Synopsis::
# [-name <string>] [-indent <n>] [-full] [-help]
#
# Description::
# Format basic help text, treating any string that ends with :: (with optional
# whitespace) as a header. A header has an extra blank line inserted above
# it, the :: removed, and is displayed in allcaps.
#
# If -indent is specified, the help text is indented by the specified
# number of spaces. Default is 4 spaces.
#
# Usage::
# $helpText | Write-Help -indent 2;
param (
[string]$name,
[int]$indent = 4,
[switch]$full,
[switch]$help
)
if ($help) { return (Get-FunctionHelp -name $MyInvocation.MyCommand.Name); }
$data = @('');
$indents = " " * $indent;
foreach ($line in $input) {
if (!$line) { continue; }
if (($line -match "^\s*$") -or ($line -match "^#region ") -or ($line -match "^#endregion ")) { continue; }
if ($line -match "^\s*###") {
if ($full) {
continue;
} else {
break;
}
}
$line = $line -replace "<name>(\.ps1)?", $name;
if ($line -match "^\s*#") {
$line = $line -replace "^\s*#*" -replace "^ " -replace "\s*$", " ";
if ($line -match "^\s*$") {
$data += @("", $indents);
} else {
if ($line -match "^.*::\s*$") {
$line = $line.ToUpper()
$data += $line -replace "::\s*$";
if ($line -match "Synopsis::") {
$data += "$indents$name ";
} else {
$data += $indents;
}
} else {
$data[$data.Count - 1] += $line;
}
}
} else {
break;
}
}
"`nName`n$indents$name`n";
$data | Break-Line;
"NOTES`n${indents}Internal Use Only`n${indents}Copyright (c)$(Get-Date -format yyyy) Microsoft`n";
}
Here, the magic string is ending a line with "::". That signifies a section head.