Make getopt ignore first argument

Asked

Viewed 83 times

3

I’m creating a PHP CLI, and wanted to run something like this

php console.php comando -a foo -b bar -d

But with the function getopt I can’t get past the comando if not the Buga function and do not receive any of the other arguments. I need to set a default parameter as -c:

php console.php -c comando -a foo -b bar -d

Does anyone know any method to "parse" the arguments by ignoring the first?

PS: I also need to get the first argument, to know which command should be executed.

Code: (github link)

public function run() {

  $command = $this->getCommand();

  if (is_null($command)) return FALSE;
  $optcll = $command->getOptionCollection();
  $opts = $optcll->dump();

  // LINHA QUE PEGO OS ARGUMENTOS DO COMANDO
  $args = getopt($opts['options'], $opts['longopts']);

  return $command->execute($this, $args);

}
  • 1

    What is the code and console.php?

  • 1

    That there is just example, is the instantiation of a class that executes the code of other classes and etc... The problem is the function getopt which I am using to pick up the parameters passed. https://github.com/KaduAmaral/CMP/blob/master/CMP/Console.php#L37

  • 1

    The code is public in the repository: https://github.com/KaduAmaral/CMP

  • 1

    The solution needs to be necessarily using getopt?

  • 1

    See if in this post something is usable: https://stackoverflow.com/a/34536611/1377664

  • Not long, I need one workaround pro PHP. I know that the Symfony console does something similar to what I want... but thanks for the @Dvdsamm link

Show 1 more comment

1 answer

3


I don’t know if it satisfies your needs, but there is a library docopt for PHP.

With it, all you have to do is write the documentation of your commands and the library will handle the entries, handing you an object with the information provided by the user. For example, considering your example, we could do:

<?php

$doc = <<<DOC
Descrição da aplicação.

Usage:
  console.php comando [-a A] [-b B] [-d]
  console.php (-h | --help)
  console.php --version

Options:
  -h --help     Exibe a mensagem de ajuda.
  --version     Exibe a versão.
  -a A          Valor de A.
  -b B          Valor de B.
  -d            Define como D.
DOC;

require('vendor/autoload.php');

$args = Docopt::handle($doc, array('version'=>'0.1.0'));

foreach ($args as $k => $v)
{
    echo $k.': '.json_encode($v).PHP_EOL;
}

Thus, you can display the help message with the command:

$ php console.php -h

Descrição da aplicação.

Usage:
  console.php comando [-a A] [-b B] [-d]
  console.php (-h | --help)
  console.php --version

Options:
  -h --help     Exibe a mensagem de ajuda.
  --version     Exibe a versão.
  -a A          Valor de A.
  -b B          Valor de B.
  -d            Define como D.

Display version with command:

$ php console.php --version

0.1.0

Or execute the commands provided in Usage:

$ php console.php comando

comando: true
-a: null
-b: null
-d: false
--help: false
--version: false

As I defined the options as optional and did not inform them, they were defined as null those who possess value, such as -a and -b, and how false those who are flags, as -d. When you enter the figures, you are:

$ php console.php comando -a foo -b bar -d

comando: true
-a: "foo"
-b: "bar"
-d: true
--help: false
--version: false
  • 1

    I’m still studying how to use function getopt to say if it is possible to do it the way you tried and how to do it. I update the answer as soon as possible.

  • 1

    Ball show, apparently will suit me perfectly, I will need to make some adjustments to the code, but as soon as test warning...

  • I implemented it here, it was perfect, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.