PoshCode / Pansies

Powershell ANSI Escape Sequences, functions for colored output, etc.
MIT License
146 stars 14 forks source link

Functions with Parameter type `RgbColor` Error on `X11ColorNames` #24

Closed ninmonkey closed 2 years ago

ninmonkey commented 2 years ago

I was running into errors when I type'd my functions as [PoshCode.Pansies.RgbColor]

image

$p = [PoshCode.Pansies.Palettes.X11ColorName]'maroon'
New-Text 'test' -fg $p

New-Text 'test' -fg ([string]$p)

Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."

Cause

New-Text from[PoshCode.Pansies.Commands.NewTextCommand] can't coerce [Palettes.X11ColorName] to [RgbColor] -- even with valid names

Fix

Wrapping [string] first, works.

-fg ( $m.X11ColorName ) Works with -fg ( [string]$m.X11ColorName )

Test Code / How to Reproduce

$m = [PoshCode.Pansies.RgbColor]'maroon'
$x11 = [RgbColor]'maroon' |  % X11ColorName

# ok
[RgbColor]'maroon'

# ok
New-Text 'test' -fg 'maroon'

# ok
New-Text -fg (  [string]$m.X11ColorName) 'test'

# error
New-Text 'test' -fg $x11
New-Text: Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."
Stack Trace (Click to Expand) ## Parameter Binding Trace ```powershell BIND NAMED cmd line args [New-Text] BIND arg [Maroon] to parameter [ForegroundColor] COERCE arg to [PoshCode.Pansies.RgbColor] Trying to convert argument value from PoshCode.Pansies.Palettes.X11ColorName to PoshCode.Pansies.RgbColor CONVERT arg type to param type using LanguagePrimitives.ConvertTo ERROR: ERROR: COERCE FAILED: arg [Maroon] could not be converted to the parameter type [PoshCode.Pansies.RgbColor] BIND PIPELINE object to parameters: [Out-Default] PIPELINE object TYPE = [System.Management.Automation.ErrorRecord] RESTORING pipeline parameter's original values Parameter [InputObject] PIPELINE INPUT ValueFromPipeline NO COERCION BIND arg [Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."] to parameter [InputObject] BIND arg [Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."] to param [InputObject] SUCCESSFUL ``` ```powershell BIND NAMED cmd line args [New-Text] BIND arg [Maroon] to parameter [ForegroundColor] COERCE arg to [PoshCode.Pansies.RgbColor] Trying to convert argument value from PoshCode.Pansies.Palettes.X11ColorName to PoshCode.Pansies.RgbColor CONVERT arg type to param type using LanguagePrimitives.ConvertTo ERROR: ERROR: COERCE FAILED: arg [Maroon] could not be converted to the parameter type [PoshCode.Pansies.RgbColor] BIND PIPELINE object to parameters: [Out-Default] PIPELINE object TYPE = [System.Management.Automation.ErrorRecord] RESTORING pipeline parameter's original values Parameter [InputObject] PIPELINE INPUT ValueFromPipeline NO COERCION BIND arg [Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."] to parameter [InputObject] BIND arg [Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."] to param [InputObject] SUCCESSFUL MANDATORY PARAMETER CHECK on cmdlet [Out-Default] BIND NAMED cmd line args [Out-LineOutput] BIND arg [Microsoft.PowerShell.Commands.Internal.Format.ConsoleLineOutput] to parameter [LineOutput] COERCE arg to [System.Object] Parameter and arg types the same, no coercion is needed. BIND arg [Microsoft.PowerShell.Commands.Internal.Format.ConsoleLineOutput] to param [LineOutput] SUCCESSFUL BIND POSITIONAL cmd line args [Out-LineOutput] MANDATORY PARAMETER CHECK on cmdlet [Out-LineOutput] CALLING BeginProcessing BIND PIPELINE object to parameters: [Out-LineOutput] PIPELINE object TYPE = [System.Management.Automation.ErrorRecord] RESTORING pipeline parameter's original values Parameter [InputObject] PIPELINE INPUT ValueFromPipeline NO COERCION BIND arg [Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."] to parameter [InputObject] BIND arg [Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."] to param [InputObject] SUCCESSFUL MANDATORY PARAMETER CHECK on cmdlet [out-lineoutput] BIND NAMED cmd line args [Format-Default] BIND POSITIONAL cmd line args [Format-Default] MANDATORY PARAMETER CHECK on cmdlet [Format-Default] CALLING BeginProcessing BIND PIPELINE object to parameters: [Format-Default] PIPELINE object TYPE = [System.Management.Automation.ErrorRecord] RESTORING pipeline parameter's original values Parameter [InputObject] PIPELINE INPUT ValueFromPipeline NO COERCION BIND arg [Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."] to parameter [InputObject] BIND arg [Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."] to param [InputObject] SUCCESSFUL MANDATORY PARAMETER CHECK on cmdlet [format-default] BIND NAMED cmd line args [Set-StrictMode] BIND arg [True] to parameter [Off] COERCE arg to [System.Management.Automation.SwitchParameter] Parameter and arg types the same, no coercion is needed. BIND arg [True] to param [Off] SUCCESSFUL BIND POSITIONAL cmd line args [Set-StrictMode] MANDATORY PARAMETER CHECK on cmdlet [Set-StrictMode] CALLING BeginProcessing CALLING EndProcessing BIND arg [Red] to parameter [color] Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute] result returned from DATA GENERATION: Red BIND arg [Red] to param [color] SUCCESSFUL BIND arg [Cyan] to parameter [color] Executing DATA GENERATION metadata: [System.Management.Automation.ArgumentTypeConverterAttribute] result returned from DATA GENERATION: Cyan BIND arg [Cyan] to param [color] SUCCESSFUL BIND NAMED cmd line args [Get-Command] BIND arg [New-Text] to parameter [Name] COERCE arg to [System.String[]] Trying to convert argument value from System.Management.Automation.CmdletInfo to System.String[] ENCODING arg into collection Binding collection parameter Name: argument type [CmdletInfo], parameter type [System.String[]], collection type Array, element type [System.String], coerceElementType Creating array with element type [System.String] and 1 elements Argument type CmdletInfo is not IList, treating this as scalar Coercing scalar arg value to type System.String COERCE arg to [System.String] Trying to convert argument value from System.Management.Automation.CmdletInfo to System.String CONVERT arg type to param type using LanguagePrimitives.ConvertTo CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [New-Text] Adding scalar element of type String to array position 0 Executing VALIDATION metadata: [System.Management.Automation.ValidateNotNullOrEmptyAttribute] BIND arg [System.String[]] to param [Name] SUCCESSFUL BIND arg [Ignore] to parameter [ErrorAction] COERCE arg to [System.Management.Automation.ActionPreference] Trying to convert argument value from System.String to System.Management.Automation.ActionPreference CONVERT arg type to param type using LanguagePrimitives.ConvertTo CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [Ignore] BIND arg [Ignore] to param [ErrorAction] SUCCESSFUL BIND POSITIONAL cmd line args [Get-Command] MANDATORY PARAMETER CHECK on cmdlet [Get-Command] CALLING BeginProcessing CALLING EndProcessing BIND NAMED cmd line args [Set-StrictMode] BIND arg [1] to parameter [Version] Executing DATA GENERATION metadata: [Microsoft.PowerShell.Commands.SetStrictModeCommand+ArgumentToVersionTransformationAttribute] result returned from DATA GENERATION: 1.0 COERCE arg to [System.Version] Parameter and arg types the same, no coercion is needed. Executing VALIDATION metadata: [Microsoft.PowerShell.Commands.SetStrictModeCommand+ValidateVersionAttribute] BIND arg [1.0] to param [Version] SUCCESSFUL BIND POSITIONAL cmd line args [Set-StrictMode] MANDATORY PARAMETER CHECK on cmdlet [Set-StrictMode] CALLING BeginProcessing CALLING EndProcessing ```
New-Text: Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'."
Stack Trace (Click to Expand) ## Error ```powershell Type : System.Management.Automation.ParameterBindingException Message : Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'." ParameterName : ForegroundColor ParameterType : PoshCode.Pansies.RgbColor TypeSpecified : PoshCode.Pansies.Palettes.X11ColorName ErrorId : CannotConvertArgumentNoMessage ``` ```powershell Exception : Type : System.Management.Automation.ParameterBindingException Message : Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'." ParameterName : ForegroundColor ParameterType : PoshCode.Pansies.RgbColor TypeSpecified : PoshCode.Pansies.Palettes.X11ColorName ErrorId : CannotConvertArgumentNoMessage Line : 1 Offset : 14 CommandInvocation : MyCommand : New-Text ScriptLineNumber : 1 OffsetInLine : 1 HistoryId : 79 Line : New-Text -fg $m.X11ColorName 'test' PositionMessage : At line:1 char:1 + New-Text -fg $m.X11ColorName 'test' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ InvocationName : New-Text PipelineLength : 1 PipelinePosition : 1 ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException Message : Cannot bind parameter 'ForegroundColor'. Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'." HResult : -2146233087 CategoryInfo : InvalidArgument: (:) [New-Text], ParentContainsErrorRecordException FullyQualifiedErrorId : CannotConvertArgumentNoMessage,PoshCode.Pansies.Commands.NewTextCommand InvocationInfo : MyCommand : New-Text ScriptLineNumber : 1 OffsetInLine : 14 HistoryId : 79 Line : New-Text -fg $m.X11ColorName 'test' PositionMessage : At line:1 char:14 + New-Text -fg $m.X11ColorName 'test' + ~~~~~~~~~~~~~~~ CommandOrigin : Internal ScriptStackTrace : at , : line 1 TargetSite : Name : CoerceTypeAsNeeded DeclaringType : System.Management.Automation.ParameterBinderBase, System.Management.Automation, Version=7.2.0.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35 MemberType : Method Module : System.Management.Automation.dll Data : System.Collections.ListDictionaryInternal InnerException : Type : System.Management.Automation.PSInvalidCastException ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException Message : Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'." HResult : -2146233087 CategoryInfo : InvalidArgument: (:) [], ParentContainsErrorRecordException FullyQualifiedErrorId : InvalidCastIConvertible TargetSite : Name : ConvertIConvertible DeclaringType : System.Management.Automation.LanguagePrimitives MemberType : Method Module : System.Management.Automation.dll Message : Cannot convert value "Maroon" to type "PoshCode.Pansies.RgbColor". Error: "Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'." InnerException : Type : System.InvalidCastException TargetSite : Name : DefaultToType DeclaringType : System.Convert MemberType : Method Module : System.Private.CoreLib.dll Message : Invalid cast from 'PoshCode.Pansies.Palettes.X11ColorName' to 'PoshCode.Pansies.RgbColor'. Source : System.Private.CoreLib HResult : -2147467262 StackTrace : at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) at System.Management.Automation.LanguagePrimitives.ConvertIConvertible(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) Source : System.Management.Automation HResult : -2147467262 StackTrace : at System.Management.Automation.LanguagePrimitives.ConvertIConvertible(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) at System.Management.Automation.LanguagePrimitives.ConversionData`1.Invoke(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable) at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable backupTypeTable) at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) Source : System.Management.Automation HResult : -2146233087 StackTrace : at System.Management.Automation.ParameterBinderBase.CoerceTypeAsNeeded(CommandParameterInternal argument, String parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, Object currentValue) at System.Management.Automation.ParameterBinderBase.BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags) at System.Management.Automation.CmdletParameterBinderController.BindParameter(CommandParameterInternal argument, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags) at System.Management.Automation.CmdletParameterBinderController.BindParameter(UInt32 parameterSets, CommandParameterInternal argument, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags) at System.Management.Automation.CmdletParameterBinderController.BindNamedParameter(UInt32 parameterSets, CommandParameterInternal argument, MergedCompiledCommandParameter parameter) at System.Management.Automation.ParameterBinderController.BindNamedParameters(UInt32 parameterSets, Collection`1 arguments) at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection`1 arguments) at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1 arguments) at System.Management.Automation.CommandProcessor.BindCommandLineParameters() at System.Management.Automation.CommandProcessor.Prepare(IDictionary psDefaultParameterValues) at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues) at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream) at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input) --- End of stack trace from previous location --- at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input) at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext) at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) CategoryInfo : InvalidArgument: (:) [New-Text], ParameterBindingException FullyQualifiedErrorId : CannotConvertArgumentNoMessage,PoshCode.Pansies.Commands.NewTextCommand InvocationInfo : MyCommand : New-Text ScriptLineNumber : 1 OffsetInLine : 14 HistoryId : 79 Line : New-Text -fg $m.X11ColorName 'test' PositionMessage : At line:1 char:14 + New-Text -fg $m.X11ColorName 'test' + ~~~~~~~~~~~~~~~ CommandOrigin : Internal ScriptStackTrace : at , : line 1 ```
aldrichtr commented 2 years ago

Maybe your use-case is different than the example here, but I dont think you need to cast the color: image

Jaykul commented 2 years ago

I'm sure we can add an explicit cast constructor. I definitely would like to be able to add tabcompletion for X11ColorName to all the RgbColor parameters ... (hmmmm).

The goal is that this should work:

[PoshCode.Pansies.RgbColor][PoshCode.Pansies.Palettes.X11ColorName]::Maroon2
Jaykul commented 2 years ago

Ok, I pushed and released (2.5.0 and) 2.6.0 with this fixed, so you can cast X11ColorName enum values to RgbColor. In 2.6.0 I also added an ArgumentCompleter implementation to X11Palette, so you can get tab-completion of color names:

image