2020-03-24 11:08:37 -07:00
// Copyright (c) Microsoft Corporation.
2018-02-13 09:23:53 -08:00
// Licensed under the MIT License.
2016-03-30 14:20:52 -07:00
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 )
{
2020-07-30 16:06:38 -07:00
if ( commandRuntime == null )
2016-03-30 14:20:52 -07:00
{
2020-05-19 19:57:52 +01:00
throw PSTraceSource . NewArgumentNullException ( nameof ( commandRuntime ));
2016-03-30 14:20:52 -07:00
}
2018-12-23 22:20:06 -08:00
2016-03-30 14:20:52 -07:00
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]
2016-08-02 13:29:09 -07:00
public UInt64 First { get ; set ; } = UInt64 . MaxValue ;
2016-03-30 14:20:52 -07:00
#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>
2018-12-29 08:25:49 +05:00
/// <param name="totalCount">A total count of objects that the cmdlet would return without paging.</param>
2016-03-30 14:20:52 -07:00
/// <param name="accuracy">
2017-01-16 13:31:14 -08:00
/// accuracy of the <paramref name="totalCount"/> parameter.
2016-03-30 14:20:52 -07:00
/// <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>
2018-12-25 21:43:03 +05:00
/// <returns>An object that represents a total count of objects that the cmdlet would return without paging.</returns>
2016-03-30 14:20:52 -07:00
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>
2019-01-06 04:40:24 +05:00
/// Constructs an instance with the specified command instance.
2016-03-30 14:20:52 -07:00
/// </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>
2016-07-29 13:02:49 -07:00
internal ShouldProcessParameters ( MshCommandRuntime commandRuntime )
2016-03-30 14:20:52 -07:00
{
2020-07-30 16:06:38 -07:00
if ( commandRuntime == null )
2016-03-30 14:20:52 -07:00
{
2020-05-19 19:57:52 +01:00
throw PSTraceSource . NewArgumentNullException ( nameof ( commandRuntime ));
2016-03-30 14:20:52 -07:00
}
2018-12-23 22:20:06 -08:00
2016-07-29 13:02:49 -07:00
_commandRuntime = commandRuntime ;
2018-12-08 18:53:30 +02:00
}
2016-03-30 14:20:52 -07:00
#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
{
2016-07-29 13:02:49 -07:00
return _commandRuntime . WhatIf ;
2016-03-30 14:20:52 -07:00
}
2018-12-23 22:20:06 -08:00
2016-03-30 14:20:52 -07:00
set
{
2016-07-29 13:02:49 -07:00
_commandRuntime . WhatIf = value ;
2016-03-30 14:20:52 -07:00
}
}
/// <summary>
/// Gets or sets the value of the -Confirm parameter for all cmdlets.
/// </summary>
[Parameter]
[Alias("cf")]
public SwitchParameter Confirm
{
get
{
2016-07-29 13:02:49 -07:00
return _commandRuntime . Confirm ;
2016-03-30 14:20:52 -07:00
}
2018-12-23 22:20:06 -08:00
2016-03-30 14:20:52 -07:00
set
{
2016-07-29 13:02:49 -07:00
_commandRuntime . Confirm = value ;
2016-03-30 14:20:52 -07:00
}
}
#endregion parameters
2020-11-05 03:36:23 +00:00
private readonly MshCommandRuntime _commandRuntime ;
2016-03-30 14:20:52 -07:00
}
/// <summary>
/// The declaration of parameters for the Transactions mechanisms. -UseTransaction, and -BypassTransaction.
/// </summary>
2019-05-10 15:34:27 -07:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes", Justification = "These are only exposed by way of the PowerShell cmdlets that surface them.")]
2016-03-30 14:20:52 -07:00
public sealed class TransactionParameters
{
#region ctor
/// <summary>
2019-01-06 04:40:24 +05:00
/// Constructs an instance with the specified command instance.
2016-03-30 14:20:52 -07:00
/// </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>
2016-07-29 13:02:49 -07:00
internal TransactionParameters ( MshCommandRuntime commandRuntime )
2016-03-30 14:20:52 -07:00
{
2016-07-29 13:02:49 -07:00
_commandRuntime = commandRuntime ;
2018-12-08 18:53:30 +02:00
}
2016-03-30 14:20:52 -07:00
#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
{
2016-07-29 13:02:49 -07:00
return _commandRuntime . UseTransaction ;
2016-03-30 14:20:52 -07:00
}
2018-12-23 22:20:06 -08:00
2016-03-30 14:20:52 -07:00
set
{
2016-07-29 13:02:49 -07:00
_commandRuntime . UseTransaction = value ;
2016-03-30 14:20:52 -07:00
}
}
#endregion parameters
2020-11-05 03:36:23 +00:00
private readonly MshCommandRuntime _commandRuntime ;
2016-07-29 13:02:49 -07:00
}
2016-03-30 14:20:52 -07:00
}