How Old School C Programmers Process Arguments
Extract this into its own function and you basically have getopt() --- the POSIX version, not the rather more bloaty GNU version that has sometimes-unexpected behaviour (like reordering arguments).<p>If you like this sort of source code, I&#x27;d also recommend looking at the BSD standard utilities --- they have a similar simple and concise style.<p>Yet there&#x27;s still one small simplification that can be made: the condition in the inner loop doesn&#x27;t need &quot;!= &#x27;\0&#x27;&quot;.<p>If there are many pure-binary options, then putting them in a bitflag is also an easy extension to the present code:<p><pre><code>    char binopts[] = &quot;abcdefg&quot;;
    char *opt;
    if(opt = strchr(binopts, *s))
     options |= 1 &lt;&lt; (optidx - opt);
    else
     &#x2F;&#x2F; put the switch(*s) here
</code></pre>
<i>Nowadays they just haul in the software weenies with their fancy objects and methods. Lost is the subtle art of manipulating arrays of pointers to strings of characters.</i><p>Indeed, compare with the &quot;modern&quot; way of doing it:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;commandlineparser&#x2F;commandline&#x2F;tree&#x2F;master&#x2F;src" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;commandlineparser&#x2F;commandline&#x2F;tree&#x2F;master...</a><p>...or something slightly simpler (because it&#x27;s only one file!):<p><a href="https:&#x2F;&#x2F;github.com&#x2F;mono&#x2F;mono&#x2F;blob&#x2F;master&#x2F;mcs&#x2F;class&#x2F;Mono.Options&#x2F;Mono.Options&#x2F;Options.cs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mono&#x2F;mono&#x2F;blob&#x2F;master&#x2F;mcs&#x2F;class&#x2F;Mono.Opti...</a><p>It may seem like an exaggeration, but in my experience working with C# and Java, code like that is the norm --- the core functionality is obscured by being scattered amongst a large amount of &quot;fluff&quot; and it&#x27;s hard to get the whole picture of how it works because the flow jumps around so much. In contrast, the C code in this article can be understood by just staring at it for a little bit --- the entire functionality is contained within less than a dozen lines.