using namespace System.Management.Automation using namespace System.Management.Automation.Language Register-ArgumentCompleter -Native -CommandName 'downtools' -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) $commandElements = $commandAst.CommandElements $command = @( 'downtools' 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) { 'downtools' { [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 } 'downtools;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 } 'downtools;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 } 'downtools;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 } 'downtools;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 } 'downtools;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 } 'downtools;help;print-path' { break } 'downtools;help;move' { break } 'downtools;help;hlink' { break } 'downtools;help;copy' { break } 'downtools;help;help' { break } }) $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | Sort-Object -Property ListItemText }