为函数添加了函数参数

Added function parameters to function

我有一个名为 auth 的函数,我想要函数参数,但我不知道该怎么做。函数参数为 usernamerepo。我试着用 bash 风格来做,但是没有用,在线搜索也没有太大帮助。这就是我目前拥有的。

function auth 
    username = 
    repo = 
    string = "git@github.com:${username}/${repo}"
    git remote set-url $string
end

我也试过了

function auth 
    $username = 
    $repo = 
    $string = "git@github.com:$username/$repo"
    git remote set-url {$string}
end

但是也没用。错误发生在我设置变量 usernamestring,repo

的地方
~/.config/fish/functions/auth.fish (line 2): The expanded command was empty.
    $username = 
    ^
in function 'auth' with arguments '1 2'
~/.config/fish/functions/auth.fish (line 3): The expanded command was empty.
    $repo = 
    ^
in function 'auth' with arguments '1 2'
~/.config/fish/functions/auth.fish (line 5): The expanded command was empty.
    $string = "git@github.com:$username/$repo"
    ^

Fish 将其参数存储在名为“$argv”的列表中,因此您想使用它。

此外,$var = value 在 fish 和 bash 中都是错误的语法。在 bash 中是

var=value

(没有 $ 并且 = 周围没有空格)。

在鱼中是

set var value

(也没有 $)。

所以你想要的是

function auth 
    set username $argv[1]
    set repo $argv[2]
    set string "git@github.com:$username/$repo"
    git remote set-url $string
end

但说真的,您想阅读 the documentation, specifically the section on $argv and the tutorial。这也应该可以通过 fish 中的 运行 help 访问,它应该在浏览器中打开本地副本。