PHP Cli arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The87Boy

    #1

    PHP Cli arguments

    I am trying to make a PHP Cli program, but there is something I do not
    know what to do

    If we take an example:
    php5 test.php --add Hello World I am great --delete World great --sort
    test.txt

    I want to have an array with the commands, that look like this:
    Array (
    [--add] =Array('Hello', 'World', 'I', 'am', 'great'),
    [--delete] =Array('World', 'great'),
    ['--sort'] =Array()
    );

    I have made 2 arrays; one with commands that takes arguments and
    another with commands that takes no arguments. And in the earlier
    example, the --sort command takes no argument.

    Now I do not now how to make the finished array
  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: PHP Cli arguments

    The87Boy wrote:
    I am trying to make a PHP Cli program, but there is something I do not
    know what to do
    >
    If we take an example:
    php5 test.php --add Hello World I am great --delete World great --sort
    test.txt
    Why don't you escape the parameters?

    php5 test.php --add 'Hello World I am great' --delete 'World great' \
    --sort test.txt

    It's standard practice, and it will ease your life when iterating through
    $argv...

    Cheers,
    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Menos mal que la linea no da erržžžžžÍž...

    Comment

    • shimmyshack

      #3
      Re: PHP Cli arguments

      On Dec 28, 2:33 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
      escomposlinux.-.punto.-.orgwrote:
      The87Boy wrote:
      I am trying to make a PHP Cli program, but there is something I do not
      know what to do
      >
      If we take an example:
      php5 test.php --add Hello World I am great --delete World great --sort
      test.txt
      >
      Why don't you escape the parameters?
      >
      php5 test.php --add 'Hello World I am great' --delete 'World great' \
      --sort test.txt
      >
      It's standard practice, and it will ease your life when iterating through
      $argv...
      >
      Cheers,
      --
      ----------------------------------
      Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
      >
      Menos mal que la linea no da erržžžžžÍž...
      yes excatly escape the commands using " or ' and then explode by
      space (" ") once you have the commands as a string

      Comment

      • The87Boy

        #4
        Re: PHP Cli arguments

        On Dec 28, 3:33 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
        escomposlinux.-.punto.-.orgwrote:
        Why don't you escape the parameters?
        >
        php5 test.php --add 'Hello World I am great' --delete 'World great' \
        --sort test.txt
        >
        It's standard practice, and it will ease your life when iterating through
        $argv...
        The reason why I have done it, is that it should be 5 seperate words,
        and it should work with and without escapes

        Comment

        • The87Boy

          #5
          Re: PHP Cli arguments

          On Dec 28, 3:54 pm, shimmyshack <matt.fa...@gma il.comwrote:
          yes excatly escape the commands using " or ' and then explode by
          space (" ") once you have the commands as a string
          Yes, but how do I split it up commands (the arguments that starts with
          - or --)?

          Comment

          • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

            #6
            Re: PHP Cli arguments

            The87Boy wrote:
            On Dec 28, 3:54 pm, shimmyshack <matt.fa...@gma il.comwrote:
            >yes excatly escape the commands using " or ' and then explode by
            >space (" ") once you have the commands as a string
            >
            Yes, but how do I split it up commands (the arguments that starts with
            - or --)?
            They are already in $argv !!



            --
            ----------------------------------
            Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

            Proudly running Debian Linux with 2.6.22-3-amd64 kernel, KDE 3.5.8, and PHP
            5.2.4-2 generating this signature.
            Uptime: 16:19:29 up 36 days, 2:35, 4 users, load average: 0.64, 0.93,
            0.98

            Comment

            • The87Boy

              #7
              Re: PHP Cli arguments

              On Dec 28, 4:21 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
              escomposlinux.-.punto.-.orgwrote:
              The87Boy wrote:
              On Dec 28, 3:54 pm, shimmyshack <matt.fa...@gma il.comwrote:
              yes excatly escape the commands using " or ' and then explode by
              space (" ") once you have the commands as a string
              >
              Yes, but how do I split it up commands (the arguments that starts with
              - or --)?
              >
              They are already in $argv !!
              >
              http://php.net/manual/en/features.commandline.php
              That is not the question, but how I can split up, when there is an
              argument starting with - or --, so the array's key is the argument
              that starts with - or -- and the array's values is the arguments, that
              follows the argument

              Comment

              • Csaba Gabor

                #8
                Re: PHP Cli arguments

                On Dec 28, 6:21 am, The87Boy <the87...@gmail .comwrote:
                I am trying to make a PHP Cli program, but there is something I do not
                know what to do
                >
                If we take an example:
                php5 test.php --add Hello World I am great --delete World great --sort
                test.txt
                >
                I want to have an array with the commands, that look like this:
                Array (
                [--add] =Array('Hello', 'World', 'I', 'am', 'great'),
                [--delete] =Array('World', 'great'),
                ['--sort'] =Array()
                );
                How about this (untested):

                $aArgs = array();
                foreach ($argv as $arg) {
                if (substr($arg,0, 2)=="--") $aArgs[$a=$arg] = array();
                else $aArgs[$a][] = $arg; }

                Csaba Gabor from Vancouver

                Comment

                • The87Boy

                  #9
                  Re: PHP Cli arguments

                  On Dec 28, 4:38 pm, Csaba Gabor <dans...@gmail. comwrote:
                  On Dec 28, 6:21 am, The87Boy <the87...@gmail .comwrote:
                  >
                  I am trying to make a PHP Cli program, but there is something I do not
                  know what to do
                  >
                  If we take an example:
                  php5 test.php --add Hello World I am great --delete World great --sort
                  test.txt
                  >
                  I want to have an array with the commands, that look like this:
                  Array (
                  [--add] =Array('Hello', 'World', 'I', 'am', 'great'),
                  [--delete] =Array('World', 'great'),
                  ['--sort'] =Array()
                  );
                  >
                  How about this (untested):
                  >
                  $aArgs = array();
                  foreach ($argv as $arg) {
                  if (substr($arg,0, 2)=="--") $aArgs[$a=$arg] = array();
                  else $aArgs[$a][] = $arg; }
                  You are right, but I thought, I could validate if the argument
                  starting with - or --, was in the array with arguments

                  Comment

                  Working...