Code Monkey home page Code Monkey logo

Comments (6)

we-mi avatar we-mi commented on July 21, 2024 1

Hey @regg00 ,

thanks for the update! I'll try this out as soon as i can, and get back in touch with you

from chocoman.

regg00 avatar regg00 commented on July 21, 2024

Thanks @we-mi for the kind words.
It's always nice to know something you made is appreciated by someone.

I'll take a look at this feature request and let you know if I need help.
In the meantime, you can always fork it an create a PR if you want to.

from chocoman.

regg00 avatar regg00 commented on July 21, 2024

Hey @we-mi, I just pushed a commit in the branch linked to that issue.
Let me know what you think.

from chocoman.

we-mi avatar we-mi commented on July 21, 2024

Hi @regg00 ,

i had some time looking at your implementation.

Although it is working, i want to point out some things.

Consider the following example:
Get-ChocoPackage "vlc" | Uninstall-ChocoPackage

You are using ValueFromPipeline as a Parameter-Attribute for $Name, which only tries to map the pipeline inputs by the variable-types.
You specified no explicit type for $Name, so my guess is it defaults to Object, then you have one Object-Parameter which accepts pipeline input.

Get-ChocoPackage -Name "vlc" outputs an PSCustomObject (which can be converted to Object), in my case:

> $packages = Get-ChocoPackage -Name "vlc"
> $packages.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSCustomObject                           System.Object

> $packages

Name Version
---- -------
vlc  3.0.18

This output is piped to Uninstall-ChocoPackage, but the $Name parameter contains the whole object, instead of only the desired Package-Name, because it was automatically mapped due to the corresponding parameter-types.

Don't get me wrong. It works.
But imagine you have a cmdlet with two attributes which accept pipeline input. Lets say:

[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
$Name,
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
$Version,

In the above example both $Name and $Version will contain the same object, if you use the pipeline-command from above. This works too, but is not the way it's intended to be.

Or if you specify the desired explicit type for the parameters:

[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[String]$Name,
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[Version]$Version,

it would even lead to an error.
The first String-Parameter contains the type-casted Object-Output from Get-ChocoPackage as a string (because Object-Variables can be converted to strings, but you can't access the attributes like with a normal Object).
The conversion from Object to Version-Type is not possible and therefore leads to an error.

Long story short: While this works, you have to workaround some things in order for it to work in the current implementation (what you did in lines 57 to 62 of Uninstall-ChocoPackage.ps1).

You can just use ValueFromPipelineByPropertyName instead of ValueFromPipeline and the parameter-mapping happens at the objects attribute-name level.

The input pipeline object for Uninstall-ChocoPackage does need an attribute Name in order to map it accordingly (which it does right now)
If you implement another parameter it needs to have this other name and so on.

With this you have more control how the parameters are mapped and you can just use them as defined in the param-block. If you pipe input to the cmdlet or not does not matter.

I'll create a PR with my changes :) The changes are marginal, that's why I wrote so much. I hope this clarified it a bit

from chocoman.

regg00 avatar regg00 commented on July 21, 2024

@we-mi Thanks for the clarification and the PR.
I already tried the ValueFromPipelineByPropertyName way a couple of days ago, but I kept getting the same error:

PS ~> get-chocopackage -Name rufus | uninstall-package

Uninstall-Package: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

This error is from your PR #10.
I had the exact same error before, that's why used ValueFromPipeline with the workaround you mentioned.
Does it work on your end?

from chocoman.

regg00 avatar regg00 commented on July 21, 2024

@we-mi Thanks for the clarification and the PR. I already tried the ValueFromPipelineByPropertyName way a couple of days ago, but I kept getting the same error:

PS ~> get-chocopackage -Name rufus | uninstall-package

Uninstall-Package: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

This error is from your PR #10. I had the exact same error before, that's why used ValueFromPipeline with the workaround you mentioned. Does it work on your end?

Just forget everything I said above 😥
I confused Uninstall-Package with Uninstall-ChocoPackage the first time I tested ValueFromPipelineByPropertyName.

It works fine with the changes you requested:

PS C:\Users\regg0\GitHub\ChocoMan> get-chocopackage -Name rufus | Uninstall-ChocoPackage

Name  Status
----  ------
rufus Uninstalled

Again, thanks for the help on this.
It's really appreciated.

from chocoman.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.