我想为NuGet 包管理器控制台编写几个命令来插入来自 GitHub 的 Gists。我有 4 个基本命令
- List-Gists '用户'
- 要点信息'gistId'
- Gist-Contents 'gistId' 'fileName'
- Gist-Insert 'gistId' 'fileName'
我所有的命令都依赖于几个实用函数,我正在为它们是否需要全局而苦苦挣扎。
# Json Parser
function parseJson([string]$json, [bool]$throwError = $true) {
try {
$result = $serializer.DeserializeObject( $json );
return $result;
} catch {
if($throwError) { throw "ERROR: Parsing Error"}
else { return $null }
}
}
function downloadString([string]$stringUrl) {
try {
return $webClient.DownloadString($stringUrl)
} catch {
throw "ERROR: Problem downloading from $stringUrl"
}
}
function parseUrl([string]$url) {
return parseJson(downloadString($url));
}
我可以只在我的全局函数之外拥有这些实用函数,还是需要以某种方式将它们包含在每个全局函数定义范围中?