using namespace System.Management.Automation using namespace System.Management.Automation.Language Register-ArgumentCompleter -Native -CommandName 'file_ops' -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) $commandElements = $commandAst.CommandElements $command = @( 'file_ops' for ($i = 1; $i -lt $commandElements.Count; $i++) { $element = $commandElements[$i] if ($element -isnot [StringConstantExpressionAst] -or $element.StringConstantType -ne [StringConstantType]::BareWord -or $element.Value.StartsWith('-') -or $element.Value -eq $wordToComplete) { break } $element.Value }) -join ';' $completions = @(switch ($command) { 'file_ops' { [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version information') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version information') [CompletionResult]::new('print-path', 'print-path', [CompletionResultType]::ParameterValue, 'Prints the path to the directory to walk') [CompletionResult]::new('move', 'move', [CompletionResultType]::ParameterValue, 'move the maching files to destination') [CompletionResult]::new('hlink', 'hlink', [CompletionResultType]::ParameterValue, 'hardlink') [CompletionResult]::new('copy', 'copy', [CompletionResultType]::ParameterValue, 'copy') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } 'file_ops;print-path' { [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 'the source dir to find the files') [CompletionResult]::new('--source-dir', 'source-dir', [CompletionResultType]::ParameterName, 'the source dir to find the files') [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'the target dir to put matched files') [CompletionResult]::new('--dest-dir', 'dest-dir', [CompletionResultType]::ParameterName, 'the target dir to put matched files') [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'the regex pattern to filter the files') [CompletionResult]::new('--pattern', 'pattern', [CompletionResultType]::ParameterName, 'the regex pattern to filter the files') [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'if it is present, the pattern will be case insensitive') [CompletionResult]::new('--case-insensitive', 'case-insensitive', [CompletionResultType]::ParameterName, 'if it is present, the pattern will be case insensitive') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') break } 'file_ops;move' { [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 'the source dir to find the files') [CompletionResult]::new('--source-dir', 'source-dir', [CompletionResultType]::ParameterName, 'the source dir to find the files') [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'the target dir to put matched files') [CompletionResult]::new('--dest-dir', 'dest-dir', [CompletionResultType]::ParameterName, 'the target dir to put matched files') [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'the regex pattern to filter the files') [CompletionResult]::new('--pattern', 'pattern', [CompletionResultType]::ParameterName, 'the regex pattern to filter the files') [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'if it is present, the pattern will be case insensitive') [CompletionResult]::new('--case-insensitive', 'case-insensitive', [CompletionResultType]::ParameterName, 'if it is present, the pattern will be case insensitive') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') break } 'file_ops;hlink' { [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 'the source dir to find the files') [CompletionResult]::new('--source-dir', 'source-dir', [CompletionResultType]::ParameterName, 'the source dir to find the files') [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'the target dir to put matched files') [CompletionResult]::new('--dest-dir', 'dest-dir', [CompletionResultType]::ParameterName, 'the target dir to put matched files') [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'the regex pattern to filter the files') [CompletionResult]::new('--pattern', 'pattern', [CompletionResultType]::ParameterName, 'the regex pattern to filter the files') [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'if it is present, the pattern will be case insensitive') [CompletionResult]::new('--case-insensitive', 'case-insensitive', [CompletionResultType]::ParameterName, 'if it is present, the pattern will be case insensitive') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') break } 'file_ops;copy' { [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 'the source dir to find the files') [CompletionResult]::new('--source-dir', 'source-dir', [CompletionResultType]::ParameterName, 'the source dir to find the files') [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'the target dir to put matched files') [CompletionResult]::new('--dest-dir', 'dest-dir', [CompletionResultType]::ParameterName, 'the target dir to put matched files') [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'the regex pattern to filter the files') [CompletionResult]::new('--pattern', 'pattern', [CompletionResultType]::ParameterName, 'the regex pattern to filter the files') [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'if it is present, the pattern will be case insensitive') [CompletionResult]::new('--case-insensitive', 'case-insensitive', [CompletionResultType]::ParameterName, 'if it is present, the pattern will be case insensitive') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') break } 'file_ops;help' { [CompletionResult]::new('print-path', 'print-path', [CompletionResultType]::ParameterValue, 'Prints the path to the directory to walk') [CompletionResult]::new('move', 'move', [CompletionResultType]::ParameterValue, 'move the maching files to destination') [CompletionResult]::new('hlink', 'hlink', [CompletionResultType]::ParameterValue, 'hardlink') [CompletionResult]::new('copy', 'copy', [CompletionResultType]::ParameterValue, 'copy') [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') break } 'file_ops;help;print-path' { break } 'file_ops;help;move' { break } 'file_ops;help;hlink' { break } 'file_ops;help;copy' { break } 'file_ops;help;help' { break } }) $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | Sort-Object -Property ListItemText }