diff options
author | johannst <johannes.stoelp@gmail.com> | 2021-04-11 21:31:39 +0200 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2021-04-11 21:31:39 +0200 |
commit | 66e190555b3570da3f547efa33f3ab89d0388582 (patch) | |
tree | 293b57675ddd8e33c245192e0560e645df9f1083 | |
parent | 6e82f44415534b31b683006bbeca4379d43d9d0d (diff) | |
download | notes-66e190555b3570da3f547efa33f3ab89d0388582.tar.gz notes-66e190555b3570da3f547efa33f3ab89d0388582.zip |
zshrc: added zparseopts notes
-rw-r--r-- | src/tools/zsh.md | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/tools/zsh.md b/src/tools/zsh.md index e9aa8bf..7a07a31 100644 --- a/src/tools/zsh.md +++ b/src/tools/zsh.md @@ -162,6 +162,40 @@ echo ${(L)foo} # aabb echo ${(U)foo} # AABB ``` +## Argument parsing with `zparseopts` + +```zsh +zparseopts [-D] [-E] [-A assoc] specs +``` +Arguments are copied into the associative array `assoc` according to `specs`. +Each spec is described by an entry as `opt[:][=array]`. +- `opt` is the option without the `-` char. Passing `-f` is matched against `f` + opt, `--long` is matched against `-long`. +- Using `:` means the option will take an argument. +- The optional `=array` specifies an alternate storage container where this + option should be stored. +> Documentation can be found in `man zshmodules`. + +### Example +```zsh +#!/bin/zsh +function test() { + zparseopts -D -E -A opts f=flag o: -long: + echo "flag $flag" + echo "o $opts[-o]" + echo "long $opts[--long]" + echo "pos $1" +} + +test -f -o OPTION --long LONG_OPT POSITIONAL + +# Outputs: +# flag -f +# o OPTION +# long LONG_OPT +# pos POSITIONAL +``` + ## Regular Expressions Zsh supports regular expression matching with the binary operator `=~`. |