Powershell: Select-String -SimpleMatch should be mutually exclusive with -AllMatches, but the combination is currently accepted and quietly ignored

Created on 16 Nov 2019  路  3Comments  路  Source: PowerShell/PowerShell

When you specify -SimpleMatch, the output objects' .Matches property isn't populated at all - the only information about the input string's _content_ is the .Line property.

Since all matches per input object would only be available via .Matches, specifying -AllMatches in combination with -SimpleMatch is pointless and _should be prevented_.

My guess is that the -rare - instances of this pointless combination in existing code stem from a misconception of what -AllMatches does; users may mistakenly think that -AllMatches is necessary to find all matches in a _file_ (which happens by default), whereas in reality it is used to find all matches on each _line_.

As an aside: The alternative would be to also capture literal matches with -SimpleMatch (both one by default, and all with -AllMatches), but (a) that would be a nontrivial enhancement and (b) is of less interest than with regexes; that said, it would still be of interest in two respects:

  • if you match literally, but case-INsensitively with -SimpleMatch, and are interested in the specific case variation of the matching parts of the input line.

  • Even with literal matching you may be interested in _how many_ matches occurred per line.

Steps to reproduce

# Without `-SimpleMatch`: OK, because .Matches is populated
('foo,foo' | Select-String -Pattern foo -AllMatches).Matches.Count | Should -Be 2

# With -SimpleMatch:
# The matching parts of the input lines aren't captured at all, so -AllMatches makes no sense.
# An error should be thrown, but currently isn't:
{ 'foo,foo' | Select-String -SimpleMatch foo -AllMatches } | Should -Throw

Expected behavior

Both tests should pass.

Actual behavior

The 2nd test fails:

 Expected an exception, to be thrown, but no exception was thrown

Environment data

PowerShell Core 7.0.0-preview.5
Area-Cmdlets-Utility Issue-Question

All 3 comments

I prepared the fix but it is a breaking change if now we will throw in the case.

Thanks, @iSazonov.

Good point - since specifying the inapplicable switch has currently no effect whatsoever, current code that uses it - meaninglessly - could conceivably break.

My _hunch_ is that we're talking about a Bucket 3: Unlikely Grey Area](https://github.com/PowerShell/PowerShell/blob/master/docs/dev-process/breaking-change-contract.md#bucket-3-unlikely-grey-area) change, however, given that -SimpleMatch itself appears not to be used all that frequently.

Searching for https://github.com/search?q=select-string++simplematch+allmatches&type=Code
brings up only 79 code matches (most of them duplicates, and most of them PowerShell documentation / source code), but I did find 4 instances of combining -AllMatches with -SimpleMatch, across 3 repos:

Given this, my vote is to make the change, not least because users should know that the combination doesn't make sense, so that the code doesn't end up not doing what they think it does.

GitHub
PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.
GitHub
Brewmaster template for ElasticSearch on Windows Azure IaaS - AditiTechnologies/Brewmaster.ElasticSearch
GitHub
Brewmaster template for ElasticSearch on Windows Azure IaaS - AditiTechnologies/Brewmaster.ElasticSearch
GitHub
Brewmaster template for ElasticSearch on Windows Azure IaaS - AditiTechnologies/Brewmaster.ElasticSearch
GitHub
Various PowerShell Scripts . Contribute to bmmcclint/ps_scripts development by creating an account on GitHub.
GitHub
Various PowerShell Scripts . Contribute to bmmcclint/ps_scripts development by creating an account on GitHub.
GitHub
Code examples for Autodesk Vault Programmers Cookbook on http://zippybytes.blogspot.com - miwaniza/Autodesk_Vault_Programmers_Cookbook
GitHub
Code examples for Autodesk Vault Programmers Cookbook on http://zippybytes.blogspot.com - miwaniza/Autodesk_Vault_Programmers_Cookbook

Seems like a fairly sound analysis to me. If users are using switches in tandem that simply don't work that way, they should be notified of that fact.

Was this page helpful?
0 / 5 - 0 ratings