Files
PowerShell/src/System.Management.Automation/engine/UserFeedbackParameters.cs
T
xtqqczze 9212aac0fa Use nameof operator (#12716)
# PR Summary

Using *Roslynator Command Line Tool version 0.1.0.4*

* Fix RCS1015:
  * `"argument"` → `nameof(argument)`
  * `enum.ToString()` → `nameof(enum)`

[RCS1015.log](https://github.com/PowerShell/PowerShell/files/4646102/RCS1015.log)


## PR Context

<!-- Provide a little reasoning as to why this Pull Request helps and why you have opened it. -->

## PR Checklist

- [x] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
    - Use the present tense and imperative mood when describing your changes
- [x] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] This PR is ready to merge and is not [Work in Progress](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---work-in-progress).
    - If the PR is work in progress, please add the prefix `WIP:` or `[ WIP ]` to the beginning of the title (the `WIP` bot will keep its status check at `Pending` while the prefix is present) and remove the prefix when the PR is ready.
- **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)**
    - [x] None
    - **OR**
    - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Experimental_Features.md)
        - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here -->
- **User-facing changes**
    - [x] Not Applicable
    - **OR**
    - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
        - [ ] Issue filed: <!-- Number/link of that issue here -->
- **Testing - New and feature**
    - [x] N/A or can only be tested interactively
    - **OR**
    - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting)
- **Tooling**
    - [x] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted.
    - **OR**
    - [ ] I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include:
        - Impact on [PowerShell Editor Services](https://github.com/PowerShell/PowerShellEditorServices) which is used in the [PowerShell extension](https://github.com/PowerShell/vscode-powershell) for VSCode (which runs in a different PS Host).
        - Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features.
        - Impact on [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) (which provides linting & formatting in the editor extensions).
        - Impact on [EditorSyntax](https://github.com/PowerShell/EditorSyntax) (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
2020-05-19 18:57:52 +00:00

215 lines
7.2 KiB
C#

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Globalization;
using System.Management.Automation.Language;
namespace System.Management.Automation
{
/// <summary>
/// The parameters for the paging support enabled by <see cref="CmdletCommonMetadataAttribute.SupportsPaging"/>.
/// Includes: -IncludeTotalCount, -Skip [int], -First [int]
/// </summary>
public sealed class PagingParameters
{
#region ctor
internal PagingParameters(MshCommandRuntime commandRuntime)
{
if (commandRuntime == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(commandRuntime));
}
commandRuntime.PagingParameters = this;
}
#endregion ctor
#region parameters
/// <summary>
/// Gets or sets the value of the -IncludeTotalCount parameter for all cmdlets that support paging.
/// </summary>
[Parameter]
public SwitchParameter IncludeTotalCount { get; set; }
/// <summary>
/// Gets or sets the value of the -Skip parameter for all cmdlets that support paging.
/// If the user doesn't specify anything, the default is <c>0</c>.
/// </summary>
[Parameter]
public UInt64 Skip { get; set; }
/// <summary>
/// Gets or sets the value of the -First parameter for all cmdlets that support paging.
/// If the user doesn't specify anything, the default is <see cref="System.UInt64.MaxValue"/>.
/// </summary>
[Parameter]
public UInt64 First { get; set; } = UInt64.MaxValue;
#endregion parameters
#region emitting total count
/// <summary>
/// A helper method for creating an object that represents a total count
/// of objects that the cmdlet would return without paging
/// (this can be more than the size of the page specified in the <see cref="First"/> cmdlet parameter).
/// </summary>
/// <param name="totalCount">A total count of objects that the cmdlet would return without paging.</param>
/// <param name="accuracy">
/// accuracy of the <paramref name="totalCount"/> parameter.
/// <c>1.0</c> means 100% accurate;
/// <c>0.0</c> means that total count is unknown;
/// anything in-between means that total count is estimated
/// </param>
/// <returns>An object that represents a total count of objects that the cmdlet would return without paging.</returns>
public PSObject NewTotalCount(UInt64 totalCount, double accuracy)
{
PSObject result = new PSObject(totalCount);
string toStringMethodBody = string.Format(
CultureInfo.CurrentCulture,
@"
$totalCount = $this.PSObject.BaseObject
switch ($this.Accuracy) {{
{{ $_ -ge 1.0 }} {{ '{0}' -f $totalCount }}
{{ $_ -le 0.0 }} {{ '{1}' -f $totalCount }}
default {{ '{2}' -f $totalCount }}
}}
",
CodeGeneration.EscapeSingleQuotedStringContent(CommandBaseStrings.PagingSupportAccurateTotalCountTemplate),
CodeGeneration.EscapeSingleQuotedStringContent(CommandBaseStrings.PagingSupportUnknownTotalCountTemplate),
CodeGeneration.EscapeSingleQuotedStringContent(CommandBaseStrings.PagingSupportEstimatedTotalCountTemplate));
PSScriptMethod toStringMethod = new PSScriptMethod("ToString", ScriptBlock.Create(toStringMethodBody));
result.Members.Add(toStringMethod);
accuracy = Math.Max(0.0, Math.Min(1.0, accuracy));
PSNoteProperty statusProperty = new PSNoteProperty("Accuracy", accuracy);
result.Members.Add(statusProperty);
return result;
}
#endregion emitting total count
}
}
namespace System.Management.Automation.Internal
{
/// <summary>
/// The declaration of parameters for the ShouldProcess mechanisms. -Whatif, and -Confirm.
/// </summary>
public sealed class ShouldProcessParameters
{
#region ctor
/// <summary>
/// Constructs an instance with the specified command instance.
/// </summary>
/// <param name="commandRuntime">
/// The instance of the command that the parameters should set the
/// user feedback properties on when the parameters get bound.
/// </param>
internal ShouldProcessParameters(MshCommandRuntime commandRuntime)
{
if (commandRuntime == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(commandRuntime));
}
_commandRuntime = commandRuntime;
}
#endregion ctor
#region parameters
/// <summary>
/// Gets or sets the value of the -Whatif parameter for all cmdlets.
/// </summary>
[Parameter]
[Alias("wi")]
public SwitchParameter WhatIf
{
get
{
return _commandRuntime.WhatIf;
}
set
{
_commandRuntime.WhatIf = value;
}
}
/// <summary>
/// Gets or sets the value of the -Confirm parameter for all cmdlets.
/// </summary>
[Parameter]
[Alias("cf")]
public SwitchParameter Confirm
{
get
{
return _commandRuntime.Confirm;
}
set
{
_commandRuntime.Confirm = value;
}
}
#endregion parameters
private MshCommandRuntime _commandRuntime;
}
/// <summary>
/// The declaration of parameters for the Transactions mechanisms. -UseTransaction, and -BypassTransaction.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes", Justification = "These are only exposed by way of the PowerShell cmdlets that surface them.")]
public sealed class TransactionParameters
{
#region ctor
/// <summary>
/// Constructs an instance with the specified command instance.
/// </summary>
/// <param name="commandRuntime">
/// The instance of the command that the parameters should set the
/// user feedback properties on when the parameters get bound.
/// </param>
internal TransactionParameters(MshCommandRuntime commandRuntime)
{
_commandRuntime = commandRuntime;
}
#endregion ctor
#region parameters
/// <summary>
/// Gets or sets the value of the -UseTransaction parameter for all cmdlets.
/// </summary>
[Parameter]
[Alias("usetx")]
public SwitchParameter UseTransaction
{
get
{
return _commandRuntime.UseTransaction;
}
set
{
_commandRuntime.UseTransaction = value;
}
}
#endregion parameters
private MshCommandRuntime _commandRuntime;
}
}