Last active
October 30, 2023 18:05
-
-
Save ninmonkey/21457f2be9274600b71f21dd8c1c36a5 to your computer and use it in GitHub Desktop.
Calls Native command rg with args
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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