Skip to content

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

Closed

Description

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

Activity

  1. added
    Issue-Question
    on Nov 16, 2019
  2. changed the title [-]Select-String -SimpleMatch is mutually exclusive with -AllMatches, but is quietly ignored[/-] [+]Select-String -SimpleMatch should be mutually exclusive with -AllMatches, but the combination is currently accepted and quietly ignored[/+] on Nov 16, 2019
  3. iSazonov commented on Nov 18, 2019

    @iSazonov
    Collaborator

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

  4. mklement0 commented on Nov 18, 2019

    @mklement0
    ContributorAuthor

    Thanks, Ilya (@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
  5. vexx32 commented on Nov 18, 2019

    @vexx32
    Collaborator

    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.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. added
    Resolution-No Activity
    on Nov 16, 2023
  10. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-QuestionResolution-No ActivityWG-Cmdlets

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions