<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.info.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.getopt.php',
    1 => 'getopt',
    2 => 'Lit des options pass&eacute;es dans la ligne de commande',
  ),
  'up' => 
  array (
    0 => 'ref.info.php',
    1 => 'Fonctions sur les options et les informations de PHP',
  ),
  'prev' => 
  array (
    0 => 'function.getmyuid.php',
    1 => 'getmyuid',
  ),
  'next' => 
  array (
    0 => 'function.getrusage.php',
    1 => 'getrusage',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/info/functions/getopt.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.getopt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">getopt</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">getopt</span> &mdash; <span class="dc-title">Lit des options passées dans la ligne de commande</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.getopt-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>getopt</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$short_options</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$long_options</code><span class="initializer"> = []</span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$rest_index</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>getopt()</strong></span> lit les options passées dans la ligne de commande.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.getopt-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">short_options</code></dt>
     <dd>
      <span class="simpara">
       Chaque caractère dans cette chaîne sera utilisé en tant que caractères
       optionnels et devra correspondre aux options passées, commençant
       par un tiret simple (<code class="literal">-</code>).
      </span>
      <span class="simpara">
       Par exemple, une chaîne optionnelle <code class="literal">&quot;x&quot;</code> correspondra
       à l&#039;option <code class="literal">-x</code>.
      </span>
      <span class="simpara">
       Seuls a-z, A-Z et 0-9 sont autorisés.
      </span>
     </dd>
    
    
     <dt><code class="parameter">long_options</code></dt>
     <dd>
      <span class="simpara">
       Un tableau d&#039;options. Chaque élément de ce tableau sera utilisé comme
       option et devra correspondre aux options passées, commençant par un
       tiret double (<code class="literal">--</code>).
      </span>
      <span class="simpara">
       Par exemple, un élément <code class="literal">longopts</code> <code class="literal">&quot;opt&quot;</code>
       correspondra à l&#039;option <code class="literal">--opt</code>.
      </span>
     </dd>
    
    
     <dt><code class="parameter">rest_index</code></dt>
     <dd>
      <span class="simpara">
       Si le paramètre <code class="parameter">rest_index</code> est présent, alors l&#039;index
       où l&#039;analyse des arguments s&#039;est arrêtée sera écrit dans cette variable.
      </span>
     </dd>
    
   </dl>
  </p>
  <p class="para">
   Le paramètre <code class="parameter">short_options</code> peut contenir les éléments suivants :
   <ul class="simplelist">
    <li>Caractères individuels (n'accepte pas de valeur)</li>
    <li>Caractères suivis par un deux-points (le paramètre nécessite une valeur)</li>
    <li>Caractères suivis par deux deux-points (valeur optionnelle)</li>
   </ul>
   Les valeurs optionnelles sont les premiers arguments après la chaîne. Si
   une valeur est requise, peu importe que la valeur soit suivie d&#039;un espace ou
   non. Voir la note.
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     Les valeurs optionnelles n&#039;acceptent pas l&#039;espace comme séparateur.
    </span>
   </p></blockquote>
  </p>
  <p class="para">
   Le tableau de valeurs <code class="parameter">long_options</code> peut contenir :
   <ul class="simplelist">
    <li>Chaîne (le paramètre n'accepte aucune valeur)</li>
    <li>Chaîne suivie d'un deux-points (le paramètre nécessite une valeur)</li>
    <li>Chaîne suivie de deux deux-points (valeur optionnelle)</li>
   </ul>
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Le format des paramètres <code class="parameter">short_options</code> et
    <code class="parameter">long_options</code> est identique ; la seule différence est
    que <code class="parameter">long_options</code> prend un tableau en option (où chaque élément
    est une option) alors que <code class="parameter">short_options</code> prend une chaîne
    (où chaque caractère est une option).
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.getopt-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Cette fonction retourne un tableau d&#039;options/arguments,  ou <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> si une erreur survient.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    L&#039;analyse des options s&#039;arrêtera lorsque la première mauvaise
    option sera trouvée, et tout ce qui suivra sera ignoré.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.getopt-changelog">
  <h3 class="title">Historique</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.1.0</td>
       <td>
        Ajout du paramètre <code class="parameter">rest_index</code>.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.getopt-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="getopt.examples-1">
    <p><strong>Exemple #1 Exemple avec <span class="function"><strong>getopt()</strong></span> : les bases</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Script example.php<br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= </span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #DD0000">"f:hp:"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -fvalue -h</pre>
</div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(2) {
  [&quot;f&quot;]=&gt;
  string(5) &quot;value&quot;
  [&quot;h&quot;]=&gt;
  bool(false)
}</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="getopt.examples-2">
    <p><strong>Exemple #2 Second exemple avec <span class="function"><strong>getopt()</strong></span> : Introduction aux options longues</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Script example.php<br /></span><span style="color: #0000BB">$shortopts  </span><span style="color: #007700">= </span><span style="color: #DD0000">""</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$shortopts </span><span style="color: #007700">.= </span><span style="color: #DD0000">"f:"</span><span style="color: #007700">;  </span><span style="color: #FF8000">// Valeur requise<br /></span><span style="color: #0000BB">$shortopts </span><span style="color: #007700">.= </span><span style="color: #DD0000">"v::"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Valeur optionnelle<br /></span><span style="color: #0000BB">$shortopts </span><span style="color: #007700">.= </span><span style="color: #DD0000">"abc"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Ces options n'acceptent pas de valeur<br /><br /></span><span style="color: #0000BB">$longopts  </span><span style="color: #007700">= array(<br />    </span><span style="color: #DD0000">"required:"</span><span style="color: #007700">,     </span><span style="color: #FF8000">// Valeur requise<br />    </span><span style="color: #DD0000">"optional::"</span><span style="color: #007700">,    </span><span style="color: #FF8000">// Valeur optionnelle<br />    </span><span style="color: #DD0000">"option"</span><span style="color: #007700">,        </span><span style="color: #FF8000">// Aucune valeur<br />    </span><span style="color: #DD0000">"opt"</span><span style="color: #007700">,           </span><span style="color: #FF8000">// Aucune valeur<br /></span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= </span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #0000BB">$shortopts</span><span style="color: #007700">, </span><span style="color: #0000BB">$longopts</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -f &quot;value for f&quot; -v -a --required value --optional=&quot;optional value&quot; --option</pre>
</div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(6) {
  [&quot;f&quot;]=&gt;
  string(11) &quot;value for f&quot;
  [&quot;v&quot;]=&gt;
  bool(false)
  [&quot;a&quot;]=&gt;
  bool(false)
  [&quot;required&quot;]=&gt;
  string(5) &quot;value&quot;
  [&quot;optional&quot;]=&gt;
  string(14) &quot;optional value&quot;
  [&quot;option&quot;]=&gt;
  bool(false)
}</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="getopt.examples-3">
    <p><strong>Exemple #3 Troisième exemple avec <span class="function"><strong>getopt()</strong></span> : Passage de plusieurs options</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Script example.php<br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= </span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #DD0000">"abc"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -aaac</pre>
</div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(2) {
  [&quot;a&quot;]=&gt;
  array(3) {
    [0]=&gt;
    bool(false)
    [1]=&gt;
    bool(false)
    [2]=&gt;
    bool(false)
  }
  [&quot;c&quot;]=&gt;
  bool(false)
}</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="getopt.examples-4">
    <p><strong>Exemple #4 Exemple de <span class="function"><strong>getopt()</strong></span> : Utilisant <code class="parameter">rest_index</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Script example.php<br /></span><span style="color: #0000BB">$rest_index </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= </span><span style="color: #0000BB">getopt</span><span style="color: #007700">(</span><span style="color: #DD0000">'a:b:'</span><span style="color: #007700">, [], </span><span style="color: #0000BB">$rest_index</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pos_args </span><span style="color: #007700">= </span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$argv</span><span style="color: #007700">, </span><span style="color: #0000BB">$rest_index</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$pos_args</span><span style="color: #007700">);</span></span></code></div>
    </div>

    <div class="example-contents">
<div class="shellcode"><pre class="shellcode">shell&gt; php example.php -a 1 -b 2 -- test</pre>
</div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(1) {
  [0]=&gt;
  string(4) &quot;test&quot;
}</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.getopt-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="reserved.variables.argv.php" class="link"><var class="varname"><a href="reserved.variables.argv.php" class="classname">$argv</a></var></a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>