<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Koichi Nakashima</title>
    <description>The latest articles on DEV Community by Koichi Nakashima (@ko1nksm).</description>
    <link>https://dev.to/ko1nksm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F603433%2Fd3b3b384-9f93-4b1d-9450-3705e4cebb3e.png</url>
      <title>DEV Community: Koichi Nakashima</title>
      <link>https://dev.to/ko1nksm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ko1nksm"/>
    <language>en</language>
    <item>
      <title>The easiest way to parse arguments using getoptions for bash and shell scripts</title>
      <dc:creator>Koichi Nakashima</dc:creator>
      <pubDate>Fri, 30 Apr 2021 00:20:39 +0000</pubDate>
      <link>https://dev.to/ko1nksm/the-easiest-way-to-parse-arguments-using-getoptions-for-bash-and-shell-scripts-47p5</link>
      <guid>https://dev.to/ko1nksm/the-easiest-way-to-parse-arguments-using-getoptions-for-bash-and-shell-scripts-47p5</guid>
      <description>&lt;p&gt;A lot of people have been looking for a best practice to use &lt;code&gt;getopt&lt;/code&gt; and &lt;code&gt;getopts&lt;/code&gt; for long years. A &lt;a href="https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/14203146"&gt;number of methods&lt;/a&gt; to parse arguments were thought up, but no perfect solution was found. However, with the advent of the &lt;a href="https://github.com/ko1nksm/getoptions"&gt;getoptions&lt;/a&gt;, that long journey comes to an end.&lt;/p&gt;

&lt;h2&gt;
  
  
  The getoptions
&lt;/h2&gt;

&lt;p&gt;It is designed to be a ready-to-use replacement for &lt;code&gt;getopt&lt;/code&gt; and &lt;code&gt;getopts&lt;/code&gt;. No special embedded comments, configuration files, or pre-build is required, so the learning curve is low and maintenance is easy. In other words, just install a &lt;a href="https://github.com/ko1nksm/getoptions/releases"&gt;getoptions&lt;/a&gt; and the following shell script will work like a charm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/sh&lt;/span&gt;

&lt;span class="nv"&gt;VERSION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"0.1"&lt;/span&gt;

parser_definition&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  setup   REST &lt;span class="nb"&gt;help&lt;/span&gt;:usage &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="s2"&gt;"Usage: example.sh [options]... [arguments]..."&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;
  msg &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="s1"&gt;'Options:'&lt;/span&gt;
  flag    FLAG    &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="nt"&gt;--flag&lt;/span&gt;                 &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="s2"&gt;"takes no arguments"&lt;/span&gt;
  param   PARAM   &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nt"&gt;--param&lt;/span&gt;                &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="s2"&gt;"takes one argument"&lt;/span&gt;
  option  OPTION  &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="nt"&gt;--option&lt;/span&gt; on:&lt;span class="s2"&gt;"default"&lt;/span&gt;  &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="s2"&gt;"takes one optional argument"&lt;/span&gt;
  disp    :usage  &lt;span class="nt"&gt;-h&lt;/span&gt; &lt;span class="nt"&gt;--help&lt;/span&gt;
  disp    VERSION    &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;getoptions parser_definition&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt; exit 1"&lt;/span&gt; &lt;span class="c"&gt;# argument parsing&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"FLAG: &lt;/span&gt;&lt;span class="nv"&gt;$FLAG&lt;/span&gt;&lt;span class="s2"&gt;, PARAM: &lt;/span&gt;&lt;span class="nv"&gt;$PARAM&lt;/span&gt;&lt;span class="s2"&gt;, OPTION: &lt;/span&gt;&lt;span class="nv"&gt;$OPTION&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'%s\n'&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="c"&gt;# output rest arguments&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;./example.sh &lt;span class="nt"&gt;-h&lt;/span&gt;
&lt;span class="go"&gt;Usage: example.sh [options]... [arguments]...

Options:
  -f, --flag                  takes no arguments
  -p, --param PARAM           takes one argument
  -o, --option[=OPTION]       takes one optional argument
  -h, --help
      --version
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  It is an option parser generator
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;getoptions&lt;/code&gt; is essentially a generator that generates an option parser, but claims to be an option parser. Because it is very fast (minimum &amp;lt;= 10ms), can dynamically generate an option parser at runtime and parse arguments.&lt;/p&gt;

&lt;h2&gt;
  
  
  I don't want to install the getoptions!
&lt;/h2&gt;

&lt;p&gt;If you want to run your script without &lt;code&gt;getoptions&lt;/code&gt; installed, you can include its core library in your script and distribute it. The license is &lt;a href="https://creativecommons.org/publicdomain/zero/1.0/"&gt;CC0&lt;/a&gt;, so there are no restrictions at all. Its size is 200-300 lines (5KB-8KB), depending on the configuration, is extremely compact compared to other implementations.&lt;/p&gt;

&lt;h2&gt;
  
  
  I don't want to include the library!
&lt;/h2&gt;

&lt;p&gt;If you do not want to include the library in your scripts, you can use it as an option parser generator. If you generate the parser beforehand, you don't need to include the library, and execution speed will be even faster. The generated parser is simple, short, and only one function. Its size will be about the same as what you would write by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  No requirements and POSIX-compliant
&lt;/h2&gt;

&lt;p&gt;All you need to use &lt;code&gt;getoptions&lt;/code&gt; is only POSIX shell (i.e. dash 0.5.4+, bash 2.03+, ksh88+, zsh 3.1.9+). It is strongly standards-aware and supports both POSIX and GNU option syntax and supports long options as well as long option abbreviations and subcommands.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;getoptions&lt;/code&gt; is very easy to use. But in contrast to its small size, is has a lot of features, extensibility and flexibility. You can customize error messages, validations, automatically generate help, and call functions when specified options.&lt;/p&gt;

&lt;p&gt;If you want to know more about the advanced features, please visit the project site. There are a variety of examples available. However, for basic usage, there is nothing more to explain.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>shellscript</category>
      <category>devops</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
