A
A
arinoki2011-10-25 19:52:26
linux
arinoki, 2011-10-25 19:52:26

Php, console, options?

There is a question. How to beautifully organize the creation of an associative array based on the passed values?
Something like this - php script.php -filename someFileName -mime someMime -type someType.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Next_Alex, 2011-10-26
@arinoki

I did it like this:
A slightly modified method found on the net:

function parseArgs( $_SERVER, $_REQUEST )
{
   $out = array();
   if ( is_array( $_SERVER ) && is_array( $_REQUEST ) )
   {
      $cli_args = isset( $_SERVER[ 'argv' ] ) ? $_SERVER[ 'argv' ] : array();
      if ( count( $cli_args ) > 1 )
      {
         array_shift( $cli_args );
         foreach ( $cli_args as $arg )
         {
            // --foo --bar=baz
            if ( substr( $arg, 0, 2 ) == '--' )
            {
               $eqPos                  = strpos( $arg, '=' );
               // --foo
               if ( $eqPos === false )
               {
                  $key                = substr( $arg, 2 );
                  $value              = isset( $out[ $key ] ) ? $out[ $key ] : true;
                  $out[ $key ]          = $value;
               }
               // --bar=baz
               else
               {
                  $key                = substr( $arg, 2, $eqPos - 2 );
                  $value              = substr( $arg, $eqPos + 1 );
                  $out[ $key ]          = $value;
               }
            }
            // -k=value -abc
            else if ( substr( $arg, 0, 1 ) == '-' )
            {
               // -k=value
               if ( substr( $arg, 2, 1 ) == '=' )
               {
                  $key                = substr( $arg, 1, 1 );
                  $value              = substr( $arg, 3 );
                  $out[ $key ]          = $value;
               }
               // -abc
               else
               {
                  $chars              = str_split( substr( $arg, 1 ) );
                  foreach ( $chars as $char )
                  {
                     $key            = $char;
                     $value          = isset( $out[ $key ] ) ? $out[ $key ] : true;
                     $out[ $key ]      = $value;
                  }
               }
            }
            // plain-arg
            else
            {
               $value                  = $arg;
               $out[]                  = $value;
            }
         }
      }
      else
      {
         if ( count( $_REQUEST ) > 0 )
         {
            foreach ( $_REQUEST as $key => $value )
            {
               $out[ $key ] = $value;
            }
         }
      }
   }
   return $out;
}

it parses everything that is passed to the script when running via POST, GET or command line arguments and stuffs it into the returned array. The method itself lives in a separate inclusion.
And, accordingly, parsing the arguments in the script itself:
$arguments = parseArgs( $_SERVER, $_REQUEST );

The call from the console is like this (although in principle everything is clear from the code):
php myscript.php --firstarg=somedata -anotherarg=anotherdata --justaflag -anotherflag plainanarg

T
toxa82, 2011-10-25
@toxa82

They are already in the array: $argv — Array of arguments passed to script

N
niko83, 2011-10-25
@niko83

If I'm not mistaken, Zend has a lib to work with console calls

A
alexmuz, 2011-10-26
@alexmuz

Head-on solution (for sure, it can be improved well):

<?php

$i = 0;
$params = array();
while (++$i < $argc) {
    $arg = $argv[$i];
    if (substr($arg, 0, 2) == '--') {
        $key = substr($arg, 2);
    } else if (substr($arg, 0, 1) == '-') {
        $key = substr($arg, 1);
    } else {
        $params[] = $arg;
        continue;
    }
    $i++;
    $params[$key] = isset($argv[$i]) ? $argv[$i] : '';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question