fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerPrepare to not localize private function descriptionsd835c5252

master
2.0 KiB61 linesraw
1# localization: skip(uses-apropos)
2
3# Provide a minimalist realpath implementation to help deal with platforms that may not provide it
4# as a command. If an external realpath or grealpath command is available simply pass all arguments
5# thru to it. If not fallback to our builtin.
6
7# The following is slightly subtle. We have to define a realpath function even if there is an
8# external command by that name. That's because if we don't the parser will select our builtin.
9# However, we only want our builtin if there is no external realpath command.
10
11if command -sq realpath
12    function realpath
13        command realpath $argv
14    end
15    exit 0
16end
17
18# If there is a HomeBrew installed version of GNU realpath named grealpath use that.
19if command -sq grealpath
20    function realpath
21        command grealpath $argv
22    end
23    exit 0
24end
25
26function realpath -d "return an absolute path without symlinks"
27    set -l options h/help q/quiet V-version s/no-symlinks N-strip z/zero
28    set -a options e/canonicalize-existing m/canonicalize-missing L/logical P/physical
29    set -a options 'R-relative-to=' 'B-relative-base='
30    argparse -n realpath $options -- $argv
31    or return
32
33    if set -q _flag_help
34        __fish_print_help realpath
35        return 0
36    end
37
38    set -l supported_flags
39    if set -el _flag_no_symlinks
40        set -el _flag_s
41        set -a supported_flags --no-symlinks
42    end
43
44    # We don't implement any of the other flags so if any are set it's an error.
45    if string match -q '_flag_*' -- (set -l)
46        set -l flags (set -l | string replace --filter --regex '_flag_\w+\s*' '' | sort -u)
47        printf "realpath: These flags are not allowed by fish realpath: '%s'" "$flags" >&2
48        echo >&2
49        __fish_print_help realpath
50        return 1
51    end
52
53    if not set -q argv[1]
54        printf "realpath: Expected at least %d args, got only %d\n" 1 0 >&2
55        return 1
56    end
57
58    for path in $argv
59        builtin realpath $supported_flags $path
60    end
61end