Bart Schaefer schrieb am Sa 19. Jun, 00:23 (-0700): > On Fri, Jun 18, 2021 at 11:14 PM Jörg Sommer <joerg@xxxxxxxx> wrote: > > > > would it be possible to have aliases for subcommands? > > It's not really possible to have aliases for multiple-word constructs, > but you can create a function that "knows about" subcommands and then > alias the base command to pass through that function. > > Very roughly (on the fly, certainly needs work, likely better naming): > > use_subcommand_aliases() { > local -a subcommand_expansion > zstyle -a subcommand_alias:$1 $2 subcommand_expansion && > argv[2]=($subcommand_expansion) > "$@" > } > alias restic="use_subcommand_aliases restic" > zstyle subcommand_alias:restic mount mount --allow-other > --no-default-permissions Thanks! This was a pretty good start. But some of the cases I couldn't complete. Maybe you could help. ``` #!/bin/zsh -fuC _subalias_expand() { local -a expansion if zstyle -a subalias:$1 $2 expansion then shift argv[1]=( $expansion ) fi "$@" } subalias() { local full=false case $1 in -f) full=true; shift;; esac local cmd=$1 shift if [[ $1 == *=* ]] then local subcmd=${1%%=*} argv[1]=${1#*=} if $full then zstyle subalias:$cmd $subcmd $@ else zstyle subalias:$cmd $subcmd $cmd $@ fi alias $cmd="_subalias_expand $cmd" else local -a expansion if zstyle -a subalias:$cmd $1 expansion then echo "$cmd $1=$expansion" else return 1 fi fi } test_helper() { if [[ "$expect" != "$*" ]] then echo "failed: $*" exit 1 fi } # simple test expect=(mount) subalias test_helper mnt=mount if [[ "test_helper mnt=mount" != "$(subalias test_helper mnt)" ]] then echo "subalias not defined: $(subalias test_helper mnt)" fi test_helper mnt # call with args expect=(mount -a --verbose ccc) subalias test_helper mnt=mount test_helper mnt -a --verbose ccc # with same name expect=(mount) subalias test_helper mount=mount if [[ "test_helper mount=mount" != "$(subalias test_helper mount)" ]] then echo "subalias not defined: $(subalias test_helper mount)" fi test_helper mount # alias with args expect=(mount -a --verbose ccc -x) subalias test_helper mnt=mount -a --verbose ccc test_helper mnt -x # alias with empty args expect=(commit -m '') subalias test_helper cc=commit -m '' if [[ "test_helper cc=commit -m " != "$(subalias test_helper cc)" ]] then echo "subalias not defined: $(subalias test_helper cc)" fi test_helper cc # check run-help still works hit=false run-help-test_helper() { hit=true } echo 'TODO: How to test this???' # full replace test_helper_full() { if [[ "cmd --arg1 arg2" != "$*" ]] then echo "failed: $*" fi if [[ $EDITOR != /bin/true ]] then echo "failed EDITOR: $EDITOR" fi } subalias -f test_helper_full al=EDITOR=/bin/true test_helper_full cmd --arg1 arg2 if [[ "test_helper_full al=EDITOR=/bin/true test_helper_full cmd --arg1 arg2" != "$(subalias test_helper_full al)" ]] then echo "subalias not defined: $(subalias test_helper_full al)" fi test_helper_full al ``` Regards Jörg -- Die beste Tarnung ist die Wahrheit. Die glaubt einem keiner! (Max Frisch: „Biedermann und die Brandstifter“)
Attachment:
signature.asc
Description: PGP signature