Trying to wrap my head around PowerShell - functions, aliases etc

放肆的年华 提交于 2021-02-05 09:16:25

问题


I'm a seasoned C# programmer, but so far, I have only barely dabbled in PowerShell (having used another alternate Windows command line product so far).

I'm trying to automate some Git stuff, and I've run into some difficulties and can't seem to find any solution to these... (and no video tutorial or blog post seems to have helped much so far, either).

What I'm trying to do is define functions and aliases that make my work with Git in PowerShell more comfortable - and yes, I know about Posh-Git, and have checked it out, too - but that seems to be dealing mostly with presenting a nice UI in PowerShell.

I would like to define "shortcuts" for common Git command that I use all the time - and have successfully defined some aliases so far.

What I'm struggling with right now is this: I'd like to have an alias for git pull (and also git push), that can either run just "as is" - e.g. running just git pull, or that can run the most frequent command I need to use - git pull origin master.

I tried to define one function:

function invoke-gitpull { git pull $args }

and then define two aliases for this - one just calling this function "as is", one providing two parameters - like this:

Set-Alias gtp invoke-gitpull 
Set-Alias gtpom invoke-gitpull origin master

But somehow, PS doesn't like this :-(

Set-Alias : A positional parameter cannot be found that accepts argument 'origin'.
+ Set-Alias gtpom invoke-gitpull origin master
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Alias], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetAliasCommand

I then also tried to define parameters for the invoke-gitpull function - like this:

function invoke-gitpull ([String] $remote, [String] $branch) { git pull $remote $branch }

thinking that if I provide no parameter values, then git pull will be issued - and if I provide two param values - invoke-gitpull -remote origin -branch master, then git pull origin master would be called - but again, PS disagrees with me:

Set-Alias : A parameter cannot be found that matches parameter name 'remote'.
+ Set-Alias gtpom invoke-gitpull -remote origin -branch master
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Alias], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetAliasCommand

Also I then also tried with parameters inside the function:

but I got the same error again.

So HOW do I have to define those parameters for my invoke-gitpull function for this to work? I seem to be going in circles, not exactly understanding what I'm doing wrong..... can anyone enlighten me?


回答1:


Aliases (using the *-Alias cmdlets) are limited to point at command names only, without arguments. If you wish to have arguments to your alias as another alias (such as gtp origin master), you need to define them as functions:

function invoke-gitpull { git pull $args }
Set-Alias -Name gtp -Value invoke-gitpull
function gtpom { gtp origin master }

but if this is just a private-use thing, I'd skip aliases and just include them in your $Profile how you want them:

function gtp() { & GIT.exe pull @args }
function gtpom() { gtp origin master }


来源:https://stackoverflow.com/questions/52466868/trying-to-wrap-my-head-around-powershell-functions-aliases-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!