diff options
author | johannst <johannst@users.noreply.github.com> | 2021-04-11 19:41:45 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2021-04-11 19:41:45 +0000 |
commit | faa61d5bfc0ab42ef94a75d5f392dfb9c91ce456 (patch) | |
tree | b7a7a37315857694662ebb58e94e66e7c13d544f /print.html | |
parent | c6ae65a3f9c9f1d393291dbb7c1c741676664825 (diff) | |
download | notes-faa61d5bfc0ab42ef94a75d5f392dfb9c91ce456.tar.gz notes-faa61d5bfc0ab42ef94a75d5f392dfb9c91ce456.zip |
deploy: 66e190555b3570da3f547efa33f3ab89d0388582
Diffstat (limited to 'print.html')
-rw-r--r-- | print.html | 41 |
1 files changed, 37 insertions, 4 deletions
@@ -295,6 +295,39 @@ echo $bar[2] # 2 echo ${(L)foo} # aabb echo ${(U)foo} # AABB </code></pre> +<h2><a class="header" href="#argument-parsing-with-zparseopts" id="argument-parsing-with-zparseopts">Argument parsing with <code>zparseopts</code></a></h2> +<pre><code class="language-zsh">zparseopts [-D] [-E] [-A assoc] specs +</code></pre> +<p>Arguments are copied into the associative array <code>assoc</code> according to <code>specs</code>. +Each spec is described by an entry as <code>opt[:][=array]</code>.</p> +<ul> +<li><code>opt</code> is the option without the <code>-</code> char. Passing <code>-f</code> is matched against <code>f</code> +opt, <code>--long</code> is matched against <code>-long</code>.</li> +<li>Using <code>:</code> means the option will take an argument.</li> +<li>The optional <code>=array</code> specifies an alternate storage container where this +option should be stored.</li> +</ul> +<blockquote> +<p>Documentation can be found in <code>man zshmodules</code>.</p> +</blockquote> +<h3><a class="header" href="#example" id="example">Example</a></h3> +<pre><code class="language-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 +</code></pre> <h2><a class="header" href="#regular-expressions" id="regular-expressions">Regular Expressions</a></h2> <p>Zsh supports regular expression matching with the binary operator <code>=~</code>. The match results can be accessed via the <code>$MATCH</code> variable and @@ -368,7 +401,7 @@ blu -- desc for blu FUNC call func to generate matches {STR} evaluate `STR` to generate matches </code></pre> -<h3><a class="header" href="#example" id="example">Example</a></h3> +<h3><a class="header" href="#example-1" id="example-1">Example</a></h3> <p>Skeleton to copy/paste for writing simple completions.</p> <p>Assume a program <code>foo</code> with the following interface:</p> <pre><code class="language-zsh">foo -c green|red|blue -s low|high -f <file> -d <dir> -h @@ -511,7 +544,7 @@ command 2>&1 >file <li><code><param></code> specifies a variable name which <code>getopts</code> fills with the last parsed option argument</li> <li><code><args></code> optionally specify argument string to parse, by default <code>getopts</code> parses <code>$@</code></li> </ul> -<h3><a class="header" href="#example-1" id="example-1">Example</a></h3> +<h3><a class="header" href="#example-2" id="example-2">Example</a></h3> <pre><code class="language-bash">#!/bin/bash function parse_args() { while getopts "f:c" PARAM; do @@ -582,7 +615,7 @@ compgen -W "foo foobar bar" "f" # compare "hom" against file/dir names and generate matches compgen -d -f "hom" </code></pre> -<h3><a class="header" href="#example-2" id="example-2">Example</a></h3> +<h3><a class="header" href="#example-3" id="example-3">Example</a></h3> <p>Skeleton to copy/paste for writing simple completions.</p> <p>Assume a program <code>foo</code> with the following interface:</p> <pre><code class="language-bash">foo -c green|red|blue -s low|high -f <file> -h @@ -1876,7 +1909,7 @@ major_pagefault: Happens when the page needed is NOT in memory, the kernel -l <filter> . show who calls into lib matched by <filter> -C .......... demangle </code></pre> -<h1><a class="header" href="#example-3" id="example-3">Example</a></h1> +<h1><a class="header" href="#example-4" id="example-4">Example</a></h1> <p>List which program/libs call into <code>libstdc++</code>:</p> <pre><code class="language-bash">ltrace -l '*libstdc++*' -C -o ltrace.log ./main </code></pre> |