Python argparse does not parse images with wildcard
<p>I use this library <a href="https://github.com/cmusatyalab/openface" rel="nofollow">https://github.com/cmusatyalab/openface</a> for image comparison. </p>

<p>I have downloaded Docker container with preinstalled environment.  </p>

<p>I start the container and I'm accessing this file:
<a href="https://github.com/cmusatyalab/openface/blob/master/demos/compare.py" rel="nofollow">https://github.com/cmusatyalab/openface/blob/master/demos/compare.py</a></p>

<p>with this command:</p>

<blockquote>
  <p>./demos/compare.py images/examples/{lennon*,clapton*}</p>
</blockquote>

<p>(check <a href="http://cmusatyalab.github.io/openface/demo-2-comparison/" rel="nofollow">http://cmusatyalab.github.io/openface/demo-2-comparison/</a> if you want)</p>

<p>It works fine when I execute the command directly from Docker terminal. Parsed arguments look like this:</p>

<pre><code>Namespace(dlibFacePredictor='/root/openface/demos/../models/dlib/shape_predictor_68_face_landmarks.dat', imgDim=96, imgs=['images/examples/lennon-1.jpg', 'images/examples/lennon-2.jpg', 'images/examples/clapton-1.jpg', 'images/examples/clapton-2.jpg'], 
networkModel='/root/openface/demos/../models/openface/nn4.small2.v1.t7', verbose=False)
</code></pre>

<p>The problem is when I execute the same command from PHP file (I start simple PHP web server to access this script from host machine). PHP code:</p>

<blockquote>
  <p>echo shell_exec('./demos/compare.py
  images/examples/{lennon*,clapton*}')</p>
</blockquote>

<p>Parsed arguments look now like this:</p>

<pre><code>Namespace(dlibFacePredictor='/root/openface/demos/../models/dlib/shape_predictor_68_face_landmarks.dat', imgDim=96, imgs=['images/examples/{lennon*,clapton*}'], 
networkModel='/root/openface/demos/../models/openface/nn4.small2.v1.t7', verbose=False)
</code></pre>

<p><strong>BOTTOMLINE:</strong></p>

<p>When script is executed directly, imgs argument is parsed correctly</p>

<pre><code>imgs=['images/examples/lennon-1.jpg', 'images/examples/lennon-2.jpg', 'images/examples/clapton-1.jpg', 'images/examples/clapton-2.jpg']
</code></pre>

<p>When script is executed from PHP script, imgs argument is not correctly parsed:</p>

<pre><code>imgs=['images/examples/{lennon*,clapton*}']
</code></pre>

<p>Code:</p>

<pre><code>import time

start = time.time()

import argparse
import cv2
import itertools
import os

import numpy as np
np.set_printoptions(precision=2)

import openface

fileDir = os.path.dirname(os.path.realpath(__file__))
modelDir = os.path.join(fileDir, '..', 'models')
dlibModelDir = os.path.join(modelDir, 'dlib')
openfaceModelDir = os.path.join(modelDir, 'openface')

parser = argparse.ArgumentParser()

parser.add_argument('imgs', type=str, nargs='+', help="Input images.")
parser.add_argument('--dlibFacePredictor', type=str, help="Path to dlib's face predictor.",
                    default=os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))
parser.add_argument('--networkModel', type=str, help="Path to Torch network model.",
                    default=os.path.join(openfaceModelDir, 'nn4.small2.v1.t7'))
parser.add_argument('--imgDim', type=int,
                    help="Default image dimension.", default=96)
parser.add_argument('--verbose', action='store_true')

args = parser.parse_args()
</code></pre>

<p>Any idea why this happens? Thanks.</p>

<p>Neither python nor <code>argparse</code> do <em>any</em> wildcard expansion on the <code>argv</code> list passed in from the parent process. Instead, this is handled by the shell.</p>

<p>It'll depend on the shell wether or not your style of expansion is even supported. Evidently, the shell that <code>shell_exec()</code> spawns (usually <code>/bin/sh</code>) does not support <a href="https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html" rel="nofollow">bash-style brace expansion</a>. Instead, the wildcards are passed through to Python un-expanded.</p>

<p>Simplify the wildcard style by relying on <code>*</code> expansion only:</p>

<pre><code>echo shell_exec('./demos/compare.py images/examples/lennon* images/examples/clapton*')
</code></pre>

php python django python-2.7