Merge pull request #16 from shawalli/feature/expand_command

Adds a command to expand an alias to its value
This commit is contained in:
Lazarus Lazaridis 2018-03-12 09:10:04 +02:00 committed by GitHub
commit 17e6131f00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 2 deletions

View File

@ -120,6 +120,26 @@ or
goto --list
```
### Expand an alias
To expand an alias to its value, use:
```bash
goto -x <alias>
```
or
```bash
goto --expand <alias>
```
#### Example
```bash
goto -x last_release
```
or
```bash
goto --expand last_release
```
### Cleanup
To cleanup the aliases from directories that are no longer accessible in your filesystem, use:

31
goto.sh
View File

@ -50,6 +50,9 @@ goto()
-l|--list)
_goto_list_aliases
;;
-x|--expand) # Expand an alias
_goto_expand_alias "$@"
;;
-h|--help)
_goto_usage
;;
@ -83,6 +86,8 @@ OPTIONS:
goto -u|--unregister <alias>
-l, --list: lists aliases
goto -l|--list
-x, --expand: expands an alias
goto -x|--expand <alias>
-c, --cleanup: cleans up non existent directory aliases
goto -c|--cleanup
-h, --help: prints this help
@ -116,6 +121,25 @@ _goto_list_aliases()
fi
}
# Expands a registered alias.
_goto_expand_alias()
{
if [ "$#" -ne "1" ]; then
_goto_error "usage: goto -x|--expand <alias>"
return
fi
local resolved
resolved=$(_goto_find_alias_directory "$1")
if [ -z "$resolved" ]; then
_goto_error "alias '$1' does not exist"
return
fi
echo "$resolved"
}
# Lists duplicate directory aliases
_goto_find_duplicate()
{
@ -268,7 +292,7 @@ _complete_goto_commands()
local IFS=$' \t\n'
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "-r --register -u --unregister -l --list -c --cleanup -v --version" -- "$1"))
COMPREPLY=($(compgen -W "-r --register -u --unregister -l --list -x --expand -c --cleanup -v --version" -- "$1"))
}
# Completes the goto function with the available aliases
@ -321,7 +345,10 @@ _complete_goto_bash()
prev="${COMP_WORDS[1]}"
if [[ $prev = "-u" ]] || [[ $prev = "--unregister" ]]; then
# prompt with aliases only if user tries to unregister one
# prompt with aliases if user tries to unregister one
_complete_goto_aliases "$cur"
elif [[ $prev = "-x" ]] || [[ $prev = "--expand" ]]; then
# prompt with aliases if user tries to expand one
_complete_goto_aliases "$cur"
fi
elif [ "$COMP_CWORD" -eq "3" ]; then