Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fish alias pass multiple arguments, access specific argument

Tags:

fish

I would like to create an alias in script for creating pull-requests using hub.

I'm planning on creating a function like this.

# PR 
function pr --description 'hub pr'
    hub pull-request -b $argv[0] -m $argv[1];
end

The -m part can be optional though.

So I can all it like this pr 'branch here' 'message here'

But I get an error saying Array index out of bounds.

like image 686
Dev Avatar asked Oct 24 '25 14:10

Dev


2 Answers

function pr -d 'git pull-request' -a branch message
    set -q message[1]
    and set message -m $message
    hub pull-request -b $branch $message
end
like image 126
Kurtis Rader Avatar answered Oct 26 '25 17:10

Kurtis Rader


Fish arrays start at index 1, so your $argv[0] is wrong here. Use $argv[1] instead of that, and $argv[2] instead of the $argv[1] you currently have.

However, currently any element except 1 will result in an array-index-out-of-bounds error if you try to access it and it isn't set, so you need to check if it is with set -q argv[2].

# PR 
function pr --description 'hub pr'
    set -l message
    if set -q argv[2]
        set message -m $argv[2]
    end
    hub pull-request -b $argv[1] $message;
end

(Also, https://github.com/fish-shell/fish-shell/issues/826 discusses removing index-out-of-bounds errors)

like image 25
faho Avatar answered Oct 26 '25 16:10

faho