using namespace System.Management.Automation using namespace System.Management.Automation.Language Register-ArgumentCompleter -Native -CommandName 'tools' -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) $commandElements = $commandAst.CommandElements $command = @( 'tools' 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) { 'tools' { [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'The path to the directory to walk') [CompletionResult]::new('--path', 'path', [CompletionResultType]::ParameterName, 'The path to the directory to walk') [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 } 'tools;print-path' { [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') break } 'tools;move' { [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 's') [CompletionResult]::new('--source-dir', 'source-dir', [CompletionResultType]::ParameterName, 'source-dir') [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'd') [CompletionResult]::new('--dest-dir', 'dest-dir', [CompletionResultType]::ParameterName, 'dest-dir') [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'p') [CompletionResult]::new('--pattern', 'pattern', [CompletionResultType]::ParameterName, 'pattern') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') break } 'tools;hlink' { [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 's') [CompletionResult]::new('--source-dir', 'source-dir', [CompletionResultType]::ParameterName, 'source-dir') [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'd') [CompletionResult]::new('--dest-dir', 'dest-dir', [CompletionResultType]::ParameterName, 'dest-dir') [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'p') [CompletionResult]::new('--pattern', 'pattern', [CompletionResultType]::ParameterName, 'pattern') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') break } 'tools;copy' { [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 's') [CompletionResult]::new('--source-dir', 'source-dir', [CompletionResultType]::ParameterName, 'source-dir') [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'd') [CompletionResult]::new('--dest-dir', 'dest-dir', [CompletionResultType]::ParameterName, 'dest-dir') [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'p') [CompletionResult]::new('--pattern', 'pattern', [CompletionResultType]::ParameterName, 'pattern') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help information') break } 'tools;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 } 'tools;help;print-path' { break } 'tools;help;move' { break } 'tools;help;hlink' { break } 'tools;help;copy' { break } 'tools;help;help' { break } }) $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | Sort-Object -Property ListItemText }