diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/bash.html | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/tools/bash.html b/tools/bash.html index 7d74cc9..f7278b6 100644 --- a/tools/bash.html +++ b/tools/bash.html @@ -233,6 +233,42 @@ command 2>&1 >file <li>duplicate <code>fd 1</code> to <code>fd 2</code>, effectively redirecting <code>stderr</code> to <code>stdout</code></li> <li>redirect <code>stdout</code> to <code>file</code></li> </ol> +<h2><a class="header" href="#argument-parsing-with-getopts" id="argument-parsing-with-getopts">Argument parsing with <code>getopts</code></a></h2> +<p>The <code>getopts</code> builtin uses following global variables:</p> +<ul> +<li><code>OPTARG</code>, value of last option argument</li> +<li><code>OPTIND</code>, index of the next argument to process (user must reset)</li> +<li><code>OPTERR</code>, display errors if set to <code>1</code> </li> +</ul> +<pre><code class="language-bash">getopts <optstring> <param> [<args>] +</code></pre> +<ul> +<li><code><optstring></code> specifies the names of supported options, eg <code>f:c</code> +<ul> +<li><code>f:</code> means <code>-f</code> option with an argument</li> +<li><code>c</code> means <code>-c</code> option without an argument</li> +</ul> +</li> +<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" id="example">Example</a></h3> +<pre><code class="language-bash">#!/bin/bash +function parse_args() { + while getopts "f:c" PARAM; do + case $PARAM in + f) echo "GOT -f $OPTARG";; + c) echo "GOT -c";; + *) echo "ERR: print usage"; exit 1;; + esac + done + # users responsibility to reset OPTIND + OPTIND=0 +} + +parse_args -f xxx -c +parse_args -f yyy +</code></pre> <h2><a class="header" href="#completion" id="completion">Completion</a></h2> <p>The <code>complete</code> builtin is used to interact with the completion system.</p> <pre><code class="language-bash">complete # print currently installed completion handler @@ -253,7 +289,7 @@ COMPREPLY # array with possible completions </code></pre> <p>The <code>compgen</code> builtin is used to generate possible matches by comparing <code>word</code> against words generated by <code>option</code>.</p> -<pre><code class="language-bash">compgen [option] [word] +<pre><code class="language-bash">compgen <option> <word> # usefule options: # -W <list> specify list of possible completions @@ -268,7 +304,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" 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-bash">foo -c green|red|blue -s low|high -f <file> -h |