Closed ninmonkey closed 2 years ago
Maybe your use-case is different than the example here, but I dont think you need to cast the color:
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
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:
I was running into errors when I type'd my functions as
[PoshCode.Pansies.RgbColor]
Cause
New-Text
from[PoshCode.Pansies.Commands.NewTextCommand]
can't coerce[Palettes.X11ColorName]
to[RgbColor]
-- even with valid namesFix
Wrapping
[string]
first, works.-fg ( $m.X11ColorName )
Works with-fg ( [string]$m.X11ColorName )
Test Code / How to Reproduce
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 ```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