title: Using Python to parse complex arguments to shell script
body: <p>When I'm writing shell scripts, I often find myself spending most of my time (especially when debugging) dealing with argument processing.  Many scripts I write or maintain are easily more than 80% input parsing and sanitization.  I compare that to my Python scripts, where <a href="http://docs.python.org/library/argparse.html" rel="nofollow noreferrer">argparse</a> handles most of the grunt work for me, and lets me easily construct complex option structures and sanitization / string parsing behavior.</p>
  
  <p>I'd love, therefore, to be able to have Python do this heavy lifting, and then get these simplified and sanitized values in my shell script, without needing to worry any further about the arguments the user specified.</p>
  
  <p>To give a specific example, many of the shell scripts where I work have been defined to accept their arguments in a specific order.  You can call <code>start_server.sh --server myserver --port 80</code> but <code>start_server.sh --port 80 --server myserver</code> fails with <code>You must specify a server to start.</code> - it makes the parsing code a lot simpler, but it's hardly intuitive.</p>
  
  <p>So a first pass solution could be something as simple as having Python take in the arguments, sort them (keeping their parameters next to them) and returning the sorted arguments.  So the shell script still does some parsing and sanitization, but the user can input much more arbitrary content than the shell script natively accepts, something like:</p>
  
  <p><code># script.sh -o -aR --dir /tmp/test --verbose</code></p>
  
  <pre><code>#!/bin/bash
  
  args=$(order.py "$@")
  # args is set to "-a --dir /tmp/test -o -R --verbose"
  
  # simpler processing now that we can guarantee the order of parameters
  </code></pre>
  
  <p>There's some obvious limitations here, notably that <code>parse.py</code> can't distinguish between a final option with an argument and the start of indexed arguments, but that doesn't seem that terrible.</p>
  
  <p>So here's my question: <strong>1)</strong> Is there any existing (Python preferably) utility to enable CLI parsing by something more powerful than bash, which can then be accessed by the rest of my bash script after sanitization, or <strong>2)</strong> Has anyone done this before?  Are there issues or pitfalls or better solutions I'm not aware of?  Care to share your implementation?</p>
  
  <hr>
  
  <p>One (very half-baked) idea:</p>
  
  <pre><code>#!/bin/bash
  
  # Some sort of simple syntax to describe to Python what arguments to accept
  opts='
  "a", "append", boolean, help="Append to existing file"
  "dir", str, help="Directory to run from"
  "o", "overwrite", boolean, help="Overwrite duplicates"
  "R", "recurse", boolean, help="Recurse into subdirectories"
  "v", "verbose", boolean, help="Print additional information"
  '
  
  # Takes in CLI arguments and outputs a sanitized structure (JSON?) or fails
  p=$(parse.py "Runs complex_function with nice argument parsing" "$opts" "$@")
  if [ $? -ne 0 ]; exit 1; fi # while parse outputs usage to stderr
  
  # Takes the sanitized structure and an argument to get
  append=$(arg.py "$p" append)
  overwrite=$(arg.py "$p" overwrite)
  recurse=$(arg.py "$p" recurse)
  verbose=$(arg.py "$p" verbose)
  
  cd $(python arg.py "$p" dir)
  
  complex_function $append $overwrite $recurse $verbose
  </code></pre>
  
  <p>Two lines of code, along with concise descriptions of the arguments to expect, and we're on to the actual script behavior.  Maybe I'm crazy, but that seems <em>way</em> nicer than what I feel like I have to do now.</p>
  
  <hr>
  
  <p>I've seen <a href="https://stackoverflow.com/questions/4882349/parsing-shell-script-arguments">Parsing shell script arguments</a> and things like this wiki page on <a href="http://mywiki.wooledge.org/BashFAQ/035" rel="nofollow noreferrer">easy CLI argument parsing</a>, but many of these patterns feel clunky and error prone, and I dislike having to re-implement them every time I write a shell script, especially when Python, Java, etc. have such nice argument processing libraries.</p>