title: What is the best practice for creating a unix/linux command-line tool in C/C++?
body: <p>I currently am tasked with creating some command-line helper utilities for our internal development team to use. However, I want to know the best practice for creating unix command-line tools. I have tried viewing <a href="https://github.com/git/git/blob/master/git.c">git source code</a> for an example of how to read parameters and display messages accordingly. However, I'm looking for a clear template for creating a tool, reading parameters safely, and displaying the standard "help" messages if a user types in an incorrect parameter or <code>--help</code> I want to show the help message. Is there a standard library for reading <code>-abcFGH</code> and <code>--parameter</code> and switching which process starts based upon the passed parameter?</p>
  
  <p><strong>Command-Line:</strong></p>
  
  <pre><code>git
  </code></pre>
  
  <p>or</p>
  
  <pre><code>git --help
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>usage: git [--version] [--exec-path[=&lt;path&gt;]] [--html-path] [--man-path] [--info-path]
         [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
         [--git-dir=&lt;path&gt;] [--work-tree=&lt;path&gt;] [--namespace=&lt;name&gt;]
         [-c name=value] [--help]
         &lt;command&gt; [&lt;args&gt;]
  ...
  </code></pre>
  
  <p><strong>Command-Line:</strong></p>
  
  <pre><code>MyTool CommandName --CommandArgs
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <p>Whatever that specific command does.</p>
  
  <hr>
  
  <p>What I have working so far:</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>int main(int argc, char **argv)
  {
      if(argc &lt; 2)
      helpMessage();
      char* commandParameter = argv[1];
      if (strncmp(argv [1],"help", strlen(commandParameter)) == 0)
          helpMessage();
      else if (strncmp(argv [1],"pull", strlen(commandParameter)) == 0)
          pull();
      else
          helpMessage();
  }
  </code></pre>
  
  <p>What would be ideal would look like this:</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>int main(int argc, char **argv)
  {
      MagicParameters magicParameters = new MagicParameters(argv);
      switch(magicParameters[1])
      {
          case command1:
              Command1();
              break;
          case ...
  
          case help:
          default:
              HelpMessage();
              break;
      }
  }
  </code></pre>