<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://mirmodynamics.com/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Mirmo Dynamics - Tag - cli</title>
  <link>http://mirmodynamics.com/</link>
  <atom:link href="http://mirmodynamics.com/feed/tag/cli/rss2" rel="self" type="application/rss+xml"/>
  <description>Rien de grand ne se fit jamais sans enthousiasme.</description>
  <language>en</language>
  <pubDate>Wed, 03 Dec 2008 17:01:33 +0100</pubDate>
  <copyright>2003-2008 &amp;copy; Geoffrey Bachelet</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Liste de fichiers avec grep</title>
    <link>http://mirmodynamics.com/post/2006/08/13/Liste-de-fichiers-avec-grep</link>
    <guid isPermaLink="false">urn:md5:100b006bfd5662b0134ea3dd12578c4c</guid>
    <pubDate>Sun, 13 Aug 2006 17:57:00 +0200</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Le saviez vous ?</category>
        <category>cli</category><category>unix</category>    
    <description>    &lt;p&gt;Quand on cherche à récupérer une liste de fichiers contenant un motif particulier sous UNIX, on peut, quand on est une grosse brute comme moi, utiliser un mix des commandes &lt;code&gt;grep&lt;/code&gt;, &lt;code&gt;cut&lt;/code&gt; et &lt;code&gt;uniq&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;grep -r motif /emplacement/des/fichiers/ | cut -d : -f 1 | uniq&lt;/pre&gt;


&lt;p&gt;Le truc, c'est qu'il existe une option de grep qui remplace avantageusement l'appel a &lt;code&gt;cut&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;grep -rl motif /emplacement/des/fichiers | uniq&lt;/pre&gt;


&lt;p&gt;Je ne sais pas si l'option &lt;code&gt;-l&lt;/code&gt; traite les doublons, a vérifier.&lt;/p&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2006/08/13/Liste-de-fichiers-avec-grep#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2006/08/13/Liste-de-fichiers-avec-grep#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/atom/comments/575</wfw:commentRss>
      </item>
    
  <item>
    <title>Entry has unexpectedly changed special status</title>
    <link>http://mirmodynamics.com/post/2006/07/25/Entry-has-unexpectedly-changed-special-status</link>
    <guid isPermaLink="false">urn:md5:b696c8bc55ed1370c4e117502ae2a817</guid>
    <pubDate>Tue, 25 Jul 2006 10:40:00 +0200</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Geekeries</category>
        <category>cli</category><category>svn</category>    
    <description>    &lt;p&gt;Aussi connue sous le nom de &lt;em&gt;L'entrée a changé de statut spécial de façon inattendue&lt;/em&gt; si vous avez le malheur d'avoir une machine en fr_FR, cette erreur subversion est pour le moins windowsesque, puisqu'elle n'explicite en rien (a première vue du moins) le problème rencontré. Je suppose qu'il peut intervenir dans plusieurs cas, mais dans le mien, j'avais tout simplement changé le status d'un fichier &lt;em&gt;symlink&lt;/em&gt; en &lt;em&gt;vrai fichier&lt;/em&gt; :-) Manip simple pour réparer (disons que le fichier s'appelle &lt;em&gt;index.php&lt;/em&gt; et qu'il était un lien vers &lt;em&gt;blog.php&lt;/em&gt;):&lt;/p&gt;


&lt;pre&gt;$ mv index.php index.php.new
$ ln -s blog.php index.php
$ svn rm index.php
$ mv index.php.new index.php
$ svn add index.php&lt;/pre&gt;


&lt;p&gt;Et voilà, vous pouvez maintenant commettre (oh oh oh) votre &lt;em&gt;working copy&lt;/em&gt;.&lt;/p&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2006/07/25/Entry-has-unexpectedly-changed-special-status#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2006/07/25/Entry-has-unexpectedly-changed-special-status#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/atom/comments/568</wfw:commentRss>
      </item>
    
  <item>
    <title>php_sapi_name, pour des includes dynamiques ?</title>
    <link>http://mirmodynamics.com/post/2006/05/29/543-php_sapi_name-pour-des-includes-dynamiques</link>
    <guid isPermaLink="false">urn:md5:37a27ba1c5b720e0b368d3b81adf3f1a</guid>
    <pubDate>Mon, 29 May 2006 12:58:44 +0000</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Coding</category>
        <category>cli</category><category>php</category>    
    <description>    &lt;p&gt;Tout a l'heure je me suis dis que j'aurai bien voulu faire un include en fonction du &lt;acronym&gt;SAPI&lt;/acronym&gt; dans lequel un script s'execute. Comme &lt;acronym title=&quot;PHP: Hypertext Preprocessor&quot;&gt;PHP&lt;/acronym&gt; est (presque) bien fait, il existe la fonction &lt;code&gt;php_sapi_name&lt;/code&gt; qui retourne le nom du &lt;acronym&gt;SAPI&lt;/acronym&gt; courant. Ce qui nous permet de faire quelque chose comme:&lt;/p&gt;

&lt;pre&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$sapi_include&lt;/span&gt; = &lt;a href=&quot;http://www.php.net/dirname&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;dirname&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;__FILE__&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; . &lt;span style=&quot;color: #ff0000;&quot;&gt;'/sapi/'&lt;/span&gt;.php_sapi_name&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #ff0000;&quot;&gt;'.php'&lt;/span&gt;;
&lt;span style=&quot;color: #0000ff;&quot;&gt;$sapi_default&lt;/span&gt; = &lt;a href=&quot;http://www.php.net/dirname&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;dirname&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;__FILE__&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; . &lt;span style=&quot;color: #ff0000;&quot;&gt;'/sapi/default.php'&lt;/span&gt;;
&amp;nbsp;
&lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.php.net/file_exists&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;file_exists&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$sapi_include&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
	&lt;span style=&quot;color: #b1b100;&quot;&gt;require_once&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$sapi_include&lt;/span&gt;;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.php.net/file_exists&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;file_exists&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$sapi_default&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
	&lt;span style=&quot;color: #b1b100;&quot;&gt;require_once&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;$sapi_default&lt;/span&gt;;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;C'est pas très joli, mais ça fonctionne. Le gros avantage, c'est qu'on a des includes spécifiques au &lt;acronym&gt;SAPI&lt;/acronym&gt;, et donc on aura par exemple, dans &lt;code&gt;sapi/apache2handler.php&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;a href=&quot;http://www.php.net/header&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;header&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Content-Type: text/html; charset=ISO-8859-15'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #b1b100;&quot;&gt;require_once&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'libraries/Template.php'&lt;/span&gt;;
&lt;a href=&quot;http://www.php.net/session_start&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;session_start&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/pre&gt;


&lt;p&gt;et dans &lt;code&gt;sapi/cli.php&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;a href=&quot;http://www.php.net/var_dump&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;Var_Dump&lt;/span&gt;&lt;/a&gt;::&lt;span style=&quot;color: #006600;&quot;&gt;displayInit&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.php.net/array&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'display_mode'&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'text'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/pre&gt;


&lt;p&gt;Le problème, résolu par l'utilisation de &lt;code&gt;sapi/default.php&lt;/code&gt;, était que faire un fichier par &lt;acronym&gt;SAPI&lt;/acronym&gt; possible serait fastidieux (il faut connaitre le nom de tous les &lt;acronym&gt;SAPI&lt;/acronym&gt; disponibles (bon ok c'est &lt;a href=&quot;http://fr.php.net/manual/en/function.php-sapi-name.php#34002&quot;&gt;disponible ici&lt;/a&gt; mais cette liste n'a rien d'officiel) puis faire un fichier pour chaque.&lt;/p&gt;


&lt;p&gt;Maintenant la question que je me (et vous) pose, c'est: y'a-t'il des effets secondaires que je n'aurai pas encore remarqué&amp;nbsp;?&lt;/p&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2006/05/29/543-php_sapi_name-pour-des-includes-dynamiques#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2006/05/29/543-php_sapi_name-pour-des-includes-dynamiques#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/atom/comments/543</wfw:commentRss>
      </item>
    
  <item>
    <title>Faire du CLI en PHP</title>
    <link>http://mirmodynamics.com/post/2005/03/05/325-faire-du-cli-en-php</link>
    <guid isPermaLink="false">urn:md5:1325b5a09c1e84802f8f787b0c0c867f</guid>
    <pubDate>Sat, 05 Mar 2005 13:02:47 +0000</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Geekeries</category>
        <category>cli</category><category>howto</category><category>php</category>    
    <description>&lt;p&gt;Souvent les gens ne voient &lt;acronym&gt;PHP&lt;/acronym&gt; que comme un langage de script orienté web. Même si il est vrai que c'est son but premier, ce n'est pas la seule &lt;a href=&quot;http://fr.php.net/manual/en/intro-whatcando.php&quot; hreflang=&quot;en&quot;&gt;chose que &lt;acronym&gt;PHP&lt;/acronym&gt; peut faire&lt;/a&gt;. Penchons nous un peu sur l'utilisation de PHP en &lt;acronym title=&quot;Command Line Interface&quot;&gt;CLI&lt;/acronym&gt;, qui permet d'utiliser &lt;acronym&gt;PHP&lt;/acronym&gt; comme un langage de script &lt;em&gt;classique&lt;/em&gt;. Nous utiliserons pour se faire un système linux (ubuntu pour être précis) avec un binaire php-cli déjà installé.&lt;/p&gt;    &lt;h5&gt;Au commencement...&lt;/h5&gt;


&lt;p&gt;Au commencement est le &lt;em&gt;shebang&lt;/em&gt;. Qu'est-ce que c'est donc que ça&amp;nbsp;? C'est simplement la première ligne d'un script shell, celle qui indique au shell quel interpréteur utiliser pour executer le script. Elle début obligatoirement par les caractères &lt;code&gt;#!&lt;/code&gt; (qu'on appelle &lt;em&gt;shebang&lt;/em&gt;), suivis du chemin absolu de l'interpréteur. Dans la plupart des cas, l'intérpréteur &lt;acronym&gt;PHP&lt;/acronym&gt; se situe dans &lt;code&gt;/usr/bin&lt;/code&gt; et s'appelle &lt;code&gt;php&lt;/code&gt;, tout simplement. Ce qui nous donne donc:&lt;/p&gt;


&lt;pre&gt;#!/usr/bin/php&lt;/pre&gt;


&lt;p&gt;Bien sur, il est possible que votre interpréteur se situe ailleurs, &lt;code&gt;/usr/local/bin/php&lt;/code&gt; par exemple. Pour le trouve, vous pouvez utiliser la commande &lt;code&gt;which&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;ash@sushi:~$ which php
/usr/bin/php&lt;/pre&gt;


&lt;p&gt;Si cette commande ne retourne rien, c'est que php-cli n'est pas installé, et là ça dépasse le cadre de cet article :)&lt;/p&gt;


&lt;h5&gt;Un petit détour par la doc&lt;/h5&gt;


&lt;p&gt;A ce stade, il est bon d'aller voir ce que la doc a a nous dire sur &lt;a href=&quot;http://fr.php.net/manual/en/features.commandline.php&quot; hreflang=&quot;en&quot;&gt;l'utilisation de &lt;acronym&gt;PHP&lt;/acronym&gt; en ligne de commande&lt;/a&gt;. On y apprend toutes sortes de choses, a commencer par la compilation de &lt;acronym&gt;PHP&lt;/acronym&gt; en &lt;acronym&gt;CLI&lt;/acronym&gt;. On y apprend également que certaines valeurs du &lt;code&gt;php.ini&lt;/code&gt; sont différentes de celles de &lt;acronym&gt;PHP&lt;/acronym&gt; utilisé avec un server web, et nottement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;html_errors&lt;/code&gt;, pour ne pas afficher de &lt;acronym&gt;HTML&lt;/acronym&gt; dans les erreurs&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_execution_time&lt;/code&gt;, pour autoriser un temps d'execution infini&lt;/li&gt;
&lt;li&gt;&lt;code&gt;register_argc_argv&lt;/code&gt;, pour peupler les variables $argc et $argv, sur lesquelles nous reviendrons plus tard&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On y apprend aussi que php-cli enregistre automatiquement de nouvelles constantes telles que:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;STDIN&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;STDOUT&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;STDERR&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Qui représentent respectivement l'entrée standard, la sortie standard, et la sortie d'erreur standard. Nous reviendronts sur ces constantes et leur utilisation plus tard.&lt;/p&gt;


&lt;p&gt;La dernière chose que nous retiendrons de cette page est que php-cli ne modifie pas le repertoire courant a l'execution d'un script. C'est à dire que si vous vous trouvez dans le répertoire &lt;code&gt;/tmp&lt;/code&gt;, et que vous executez &lt;code&gt;php /home/ash/foo.php&lt;/code&gt;, la fonction &lt;code&gt;&lt;a href=&quot;http://fr.php.net/getcwd&quot; hreflang=&quot;en&quot;&gt;getcwd&lt;/a&gt;&lt;/code&gt; (GET Current Working Directory) retournera &lt;code&gt;/tmp&lt;/code&gt;, et pas &lt;code&gt;/home/ash&lt;/code&gt;.&lt;/p&gt;


&lt;h5&gt;Notre premier script en &lt;acronym&gt;CLI&lt;/acronym&gt;&lt;/h5&gt;


&lt;p&gt;Il est grand temps d'entrer dans le vif du sujet et de faire notre premier script. Soyons originaux, créons un &lt;code&gt;hello_world.php&lt;/code&gt;, avec le code suivant:&lt;/p&gt;

&lt;pre&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/php&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt; &lt;a href=&quot;http://www.php.net/print&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;print&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;hello world !&quot;&lt;/span&gt;; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Une fois le fichier créé, nous avons deux possibilités pour l'executer. Soit lancer directement l'interpréteur &lt;acronym&gt;PHP&lt;/acronym&gt; en lui précisant le script a executer:&lt;/p&gt;


&lt;pre&gt;$ /usr/bin/php ./hello_world.php&lt;/pre&gt;


&lt;p&gt;Soit rendre le script executable et l'executer directement:&lt;/p&gt;


&lt;pre&gt;$ chmod +x ./hello_world.php
$ ./hello_world.php&lt;/pre&gt;


&lt;p&gt;Dans tous les cas, le résultat est le même:&lt;/p&gt;


&lt;pre&gt;hello world !&lt;/pre&gt;


&lt;p&gt;Facile non&amp;nbsp;? Notez bien que si vous décidez de rendre le script executable, vous n'aurez a executer la commande &lt;code&gt;chmod&lt;/code&gt; qu'une seule fois.&lt;/p&gt;


&lt;p&gt;Vous remarquerez surement que votre &lt;em&gt;prompt&lt;/em&gt; s'affiche directement après le &lt;em&gt;!&lt;/em&gt;, c'est normal, nous n'avons pas inclus de caractère de nouvelle ligne (&lt;code&gt;
&lt;/code&gt;) dans notre &lt;code&gt;print&lt;/code&gt;. Vous pouvez donc modifier le script ainsi:&lt;/p&gt;

&lt;pre&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/php&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt; &lt;a href=&quot;http://www.php.net/print&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;print&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;hello world !&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;
&lt;/span&gt;&quot;&lt;/span&gt;; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Et le tour est joué.&lt;/p&gt;


&lt;h5&gt;Utilisation de $argc et $argv&lt;/h5&gt;


&lt;p&gt;Tout ça c'est bien beau, mais un script c'est quand même mieux quand on peut lui passer des arguments. Et &lt;strong&gt;non&lt;/strong&gt;, executer &lt;code&gt;./hello_world.php?arg=value&lt;/code&gt; ne fonctionne pas :-) Comment faire alors&amp;nbsp;? Les variables &lt;code&gt;$argc&lt;/code&gt; et &lt;code&gt;$argv&lt;/code&gt; sont là pour ça.&lt;/p&gt;


&lt;p&gt;Pour passer des paramètres à un shell script, on utilise usuellement la syntaxe suivante:&lt;/p&gt;


&lt;pre&gt;$ ./script [arg [arg...]]&lt;/pre&gt;


&lt;p&gt;C'est comme cela que nous procéderons en PHP-CLI. La variable &lt;code&gt;$argc&lt;/code&gt; (ARGuments Count) contient le nombre de paramètres passés au script, tandis que la variable &lt;code&gt;$argv&lt;/code&gt; (ARGuments Values) est un &lt;code&gt;array&lt;/code&gt; contient les valeurs de ces arguments. Modifions donc notre script de tout à l'heure:&lt;/p&gt;

&lt;pre&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/php&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;a href=&quot;http://www.php.net/var_dump&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;var_dump&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$argc&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;a href=&quot;http://www.php.net/print_r&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;print_r&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$argv&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Et executons le en lui passant quelques paramètres:&lt;/p&gt;


&lt;pre&gt;$ ./hello_world.php foo bar&lt;/pre&gt;


&lt;p&gt;Voyons le résultat:&lt;/p&gt;


&lt;pre&gt;ash@sushi:~$ ./hello_world.php foo bar
int(3)
Array
(
    [0] =&amp;gt; ./hello_world.php
    [1] =&amp;gt; foo
    [2] =&amp;gt; bar
)&lt;/pre&gt;


&lt;p&gt;On remarque que le nom du script compte comme un argument.&lt;/p&gt;


&lt;h5&gt;Utilisation de STDIN&lt;/h5&gt;


&lt;p&gt;Ces constantes dont nous avons parlé plus tôt sont très importantes. Elles définissent les flux standards à votre disposition, mais nous ne nous interresserons qu'a &lt;code&gt;STDIN&lt;/code&gt; dans cet article.&lt;/p&gt;


&lt;p&gt;&lt;code&gt;STDIN&lt;/code&gt;  (STanDard INput) est l'entrée standard. C'est elle que vous devez utiliser que vous voulez interragir avec l'utilisateur. Par exemple pour lui faire entrer des données. Modifions encore une fois notre &lt;code&gt;hello_world.php&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/php&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;$name&lt;/span&gt; = &lt;a href=&quot;http://www.php.net/trim&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;trim&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.php.net/fgets&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;fgets&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;STDIN&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;
&lt;a href=&quot;http://www.php.net/print&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;print&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;hello $name !&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;
&lt;/span&gt;&quot;&lt;/span&gt;;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Et executons le:&lt;/p&gt;


&lt;pre&gt;ash@sushi:~$ ./hello_world.php 
geoffrey
hello geoffrey !&lt;/pre&gt;


&lt;p&gt;Le script a donc attendu que je tape quelque chose en terminant par un &lt;acronym&gt;CRLF&lt;/acronym&gt; (Cariage Return Line Feed, la touche Entrée quoi ;). J'ai donc tapé &lt;em&gt;geoffrey&lt;/em&gt;, et voilà. Notons l'utilisation de &lt;code&gt;&lt;a href=&quot;http://fr.php.net/trim&quot; hreflang=&quot;en&quot;&gt;trim&lt;/a&gt;&lt;/code&gt; pour supprimer le &lt;acronym&gt;CRLF&lt;/acronym&gt; en question.&lt;/p&gt;


&lt;h5&gt;Le mot de la fin&lt;/h5&gt;


&lt;p&gt;Voilà, avec ça, vous devriez avoir les bases nécessaire pour commencer a faire du PHP-CLI. Cela dit, cet article n'explore pas toutes les subtilités du &lt;acronym&gt;CLI&lt;/acronym&gt;, et une bonne lecture de documentation est fortement conseillée :)&lt;/p&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2005/03/05/325-faire-du-cli-en-php#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2005/03/05/325-faire-du-cli-en-php#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/atom/comments/323</wfw:commentRss>
      </item>
    
</channel>
</rss>