Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active October 30, 2023 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninmonkey/21457f2be9274600b71f21dd8c1c36a5 to your computer and use it in GitHub Desktop.
Save ninmonkey/21457f2be9274600b71f21dd8c1c36a5 to your computer and use it in GitHub Desktop.
Calls Native command rg with args
using namespace System.Collections.Generic
Function InvokeGrep {
<#
.SYNOPSIS
wraps the native command RipGrep
#>
param(
[string]$Pattern,
[ArgumentCompletions('Smart', 'IgnoreCase', 'CaseSensitive')]
[string]$CaseMode,
[switch]$AsJson,
[switch]$Version,
[switch]$WhatIf,
[switch]$ListTypes
)
if($Version) {
rg --version
return
}
if($ListTypes) {
rg --type-list
return
}
[List[Object]]$binArgs = @()
switch($CaseMode){
'CaseSensitive' {
$binArgs.Add('--case-sensitive')
}
'Smart' {
$binArgs.Add('--smart-case')
}
'IgnoreCase' {
$binArgs.Add('--ignore-case')
}
}
$binArgs.Add( $Pattern )
if( $AsJson ) {
$binArgs.Add('--json')
}
# PreviewArgs
$binArgs -join ' ' | write-host -back 'darkyellow'
if( $WhatIf ) { return }
# neither, call with args
& 'rg' @binArgs
}
## example use
InvokeGrep -Pattern 'Function' -CaseMode 'Smart' -AsJson
InvokeGrep -Pattern 'Function' -CaseMode 'Smart' -WhatIf
InvokeGrep -Version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment