Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a switch to Measure-Object to make it return the requested measurement only, as a single number / value #12141

Open
mklement0 opened this issue Mar 17, 2020 · 0 comments

Comments

@mklement0
Copy link
Contributor

@mklement0 mklement0 commented Mar 17, 2020

Summary of the new feature/enhancement

Following the model of the -Raw switch that was added to Select-String (see #7713) in order to return just the matching strings, without the wrapper object with metadata, it would be both convenient and more efficient to be able to have Measure-Object return just the measurement of interest; e.g.:

# WISHFUL THINKING

PS> 3, 7, 2 | Measure-Object -Maximum -Raw
7  # max. returned directly, as a single number rather than 
   # as part of a [GenericObjectMeasureInfo] instance

# -Count, which implies -Raw, would return just the count.
PS> 3, 7, 2 | Measure-Object -Count
3

Proposed technical implementation details (optional)

Implement a -Raw switch that makes Measure-Object return a single number that is the requested measurement only.

-Raw must be mutually exclusive with -AllStats and must cause a syntax error if more than one of the specific measurements are requested (-Sum, -Maximum, ...)

Additionally, a new -Count switch would imply -Raw and output the count only.

With respect to the (now variable) return type, improvements on the always-[double] properties of the default output type, Microsoft.PowerShell.Commands.GenericObjectMeasureInfo (except for -Minimum and -Maximum), could be implemented:

  • Note: The output-type logic for -Minimum and -Maximum is currently broken for numeric types - see #12103; the input type of the min/max value identified should always be preserved as-is on output, and this behavior therefore applies here too.

  • For the inherently non-integral -Average and -StandardDeviation measurements, [double] is an appropriate default, but with (at least one) [decimal] input [decimal] should also be used on output.

  • For the -Sum and -Count measurements, integer-only input should should also output an integral type, with automatic type widening, analogous to the widening (type promotion) that happens in PowerShell's number-literal parsing ([int] ->[long] -> [decimal], and possibly even to [bigint] rather than the [double] that is the widest type for number literals.

    • For -Count, numbers beyond [int] are unlikely to occur in practice, except perhaps if opt-in enumeration of array-valued properties is implemented via the proposed -Recurse switch - see #7244.

If the resulting discrepancy in output types between using and not using -Raw - which would have to be highlighted in the documentation - is considered problematic, an additional switch named, say,
-AutoNumber could be introduced that outputs a new wrapper type with Object / ValueType-typed properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.