pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

styledna private pastebin - collaborative debugging tool What's a private pastebin?


Posted by CLI PHP on Tue 7 Oct 12:49 (modification of post by CLI PHP view diff)
report abuse | download | new post

  1. <?php
  2.  
  3. /**
  4.  * PHP Command Line library
  5.  *
  6.  * @author Philip Sturgeon
  7.  * @created 7 Oct 2008
  8.  *
  9.  */
  10.  
  11. class CLI {
  12.  
  13.     var $wait_msg = 'Press any key to continue...';
  14.    
  15.    
  16.     function CLI (){
  17.         // get CI!     
  18.     }
  19.    
  20.     // Output a line (or lines) to the command line
  21.     function write($output = '') {
  22.        
  23.         // If there are multiple lines, seperate them by newlines
  24.         if(is_array($output)) {
  25.                 $output = implode("\n", $output);
  26.         }
  27.        
  28.         // Output the lot
  29.         fwrite(STDOUT, $output."\n");
  30.     }
  31.  
  32.     // Read in a variable from the command line
  33.     function read() {
  34.        
  35.         // Work out whats what based on what params are given
  36.         $args = func_get_args();
  37.        
  38.         // Ask question with options
  39.         if(count($args) == 2) {
  40.             list($output, $options)=$args;
  41.        
  42.         // No question (probably been asked already) so just show options
  43.         } elseif(count($args) == 1 && is_array($args[0])) {
  44.             $output = '';
  45.             $options = $args[0];
  46.        
  47.         // Question without options
  48.         } elseif(count($args) == 1 && is_string($args[0])) {
  49.             $output = $args[0];
  50.             $options = array();
  51.        
  52.         // Nothing or too many, forget trying to be clever and just get what they asked for
  53.         } else {
  54.             $output = '';
  55.             $options = array();
  56.         }
  57.        
  58.         // If a question has been asked with the read
  59.         if(!empty($output)) {
  60.            
  61.             $options_output = '';
  62.             if(!empty($options)) {
  63.                 $options_output = ' [ '.implode(', ', $options).' ]';
  64.             }           
  65.  
  66.             fwrite(STDOUT, $output.$options_output.': ');
  67.         }
  68.        
  69.         // Read the input from keyboard.
  70.         $input = trim(fgets(STDIN));
  71.        
  72.         // If options are provided and the choice is not in the array, tell them to try again
  73.         if(!empty($options) && !in_array($input, $options)) {
  74.                 $this->write('This is not a valid option. Please try again.');
  75.            
  76.             $input = $this->read($output, $options);
  77.         }
  78.        
  79.         // Read the input
  80.         return $input;
  81.     }
  82.    
  83.     function new_line($lines = 1) {
  84.         // Do it once or more, write with empty string gives us a new line
  85.         for($i = 0; $i < $lines; $i++) $this->write();
  86.     }
  87.    
  88.     function wait($seconds = 0, $countdown = FALSE) {
  89.        
  90.         // Diplay the countdown
  91.         if($countdown == TRUE) {
  92.                 $i = $seconds;
  93.             while ( $i > 0 ) {
  94.                fwrite(STDOUT, $i.'... ');
  95.                sleep(1);
  96.                $i--;
  97.             }
  98.        
  99.         // No countdown timer please
  100.         } else {
  101.                
  102.             // Set number of seconds?
  103.             if($seconds > 0) {
  104.                 sleep($seconds);
  105.            
  106.             // No seconds mentioned, lets wait for user input    
  107.             } else {
  108.                 $this->write($this->wait_msg);
  109.                 $this->read();
  110.             }
  111.         }
  112.        
  113.         return TRUE;        
  114.     }
  115.  
  116. }
  117.  
  118. $cli = new CLI();
  119.  
  120. $cli->write("----------------");
  121. $cli->write("Phil's CLI Demo!");
  122. $cli->write("----------------");
  123.  
  124. // Output a new empty line
  125. $cli->new_line();
  126.  
  127. // Output multiple lines using an array
  128. $description = array(
  129.     'This library is designed to make working with the command line just a little bit easier.',
  130.     '',
  131.     'You can read and write to the command line without using confusing functions and constants!'
  132. );
  133.  
  134. // Output the array
  135. $cli->write($description);
  136.  
  137. // Multiple empty lines
  138. $cli->new_line(2);
  139.  
  140. // Questions / Input ---------------------------------------------
  141.  
  142. // Ask a question that can have any input
  143. $name = $cli->read('What is your name?');
  144.  
  145. $cli->new_line(2);
  146.  
  147. $cli->write("You are called '".$name."'.");
  148. // ---------------------------------------
  149.  
  150. $cli->new_line(2);
  151.  
  152. // Ask a question with a list of possible inputs
  153. $cheese_fan = $cli->read('Do you like cheese?', array('yes', 'no'));
  154.  
  155. $cli->new_line();
  156.  
  157. if($cheese_fan == 'yes') {
  158.     $cli->write($name." is a fan of cheese! You rock!");
  159.    
  160. } else {
  161.     $cli->write($name." is clearly rubbish, doesnt like cheese at all...");
  162. }
  163.  
  164.  
  165. // Ask the system to wait x seconds
  166. $cli->wait(2);
  167.  
  168. // Ask the system to wait x seconds
  169. $cli->wait();
  170.  
  171. $cli->new_line();
  172.  
  173. $cli->write("Exit in...");
  174.  
  175. // Ask the system to wait x seconds AND display a countdown timer
  176. $cli->wait(5, TRUE);
  177.  
  178. // Exit correctly
  179. exit(0);
  180. ?>

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post