Giter Club home page Giter Club logo

Comments (6)

tflori avatar tflori commented on June 26, 2024 1

I don't know how your files look like. The example does not work without further changes that's true. For example the move and delete commands don't exist.

Also the Commands needs to be auto loaded or loaded manually. Without touching the CopyCommand my cli file looks like this:

#!/usr/bin/env php
<?php

use GetOpt\GetOpt;
use GetOpt\Option;
use GetOpt\Command;
use GetOpt\ArgumentException;
use GetOpt\ArgumentException\Missing;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/CopyCommand.php';

define('NAME', 'AwesomeApp');
define('VERSION', '1.0-alpha');

$getOpt = new GetOpt();

// define common options
$getOpt->addOptions([

    Option::create(null, 'version', GetOpt::NO_ARGUMENT)
        ->setDescription('Show version information and quit'),

    Option::create('?', 'help', GetOpt::NO_ARGUMENT)
        ->setDescription('Show this help and quit'),

]);

// add simple commands
$getOpt->addCommand(Command::create('test-setup', function () {
    echo 'When you see this message the setup works.' . PHP_EOL;
})->setDescription('Check if setup works'));

// add commands
$getOpt->addCommand(new CopyCommand());

// process arguments and catch user errors
try {
    try {
        $getOpt->process();
    } catch (Missing $exception) {
        // catch missing exceptions if help is requested
        if (!$getOpt->getOption('help')) {
            throw $exception;
        }
    }
} catch (ArgumentException $exception) {
    file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
    echo PHP_EOL . $getOpt->getHelpText();
    exit;
}

// show version and quit
if ($getOpt->getOption('version')) {
    echo sprintf('%s: %s' . PHP_EOL, NAME, VERSION);
    exit;
}

// show help and quit
$command = $getOpt->getCommand();
if (!$command || $getOpt->getOption('help')) {
    echo $getOpt->getHelpText();
    exit;
}

// call the requested command
call_user_func($command->getHandler(), $getOpt);

The example works but the copy command has a logical error.

So please provide your getopt-php version and the files you created to test the example. It seems you have modified the example. Please note that a multiple operand can only be the last operand (see http://getopt-php.github.io/getopt-php/operands.html)

from getopt-php.

tflori avatar tflori commented on June 26, 2024 1

you are right in version 3.0 there was only Command::handler() to get the handler. And the example was different: https://github.com/getopt-php/getopt-php/blob/3.0.3/docs/example.md

Maybe we should update the installation.md file to the latest minor release (3.2) or even remove the version but the question is: why your composer is configured to minimum version? ^3.0 will install version 3.2.2 on my machine:

$ composer require ulrichsg/getopt-php:"^3.0"
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing ulrichsg/getopt-php (3.2.2): Loading from cache
Writing lock file
Generating autoload files

An option is always optional but when you define an option and omit the argument and the argument is required then it fails:

$ ./cli copy -f
Option 'file' must have a value

Usage: ./cli copy [options] [operands]



Options:
  --version         Show version information and quit
  -?, --help        Show this help and quit
  -f, --file <arg>

from getopt-php.

tflori avatar tflori commented on June 26, 2024 1

http://getopt-php.github.io/getopt-php/installation.html updated

from getopt-php.

TCB13 avatar TCB13 commented on June 26, 2024

@tflori I was already doing the changes you did. Seems like the problem is that I was running version 3.0 instead of something newer. The documentation should not point people to 3.0 and then give out examples that only work under 3.2. On the example we're calling $command->getHandler() but this method isn't available under 3.0.

http://getopt-php.github.io/getopt-php/installation.html

Use composer
$ /path/to/composer require ulrichsg/getopt-php:"^3.0"

This should be updated to:

Use composer
$ /path/to/composer require ulrichsg/getopt-php

from getopt-php.

TCB13 avatar TCB13 commented on June 26, 2024

Another thing that is probably missing is why this modified version of the CopyCommand example doesn't output anything saying that I'm missing an argument... I mean isn't what Option::create("f", "file", GetOpt::REQUIRED_ARGUMENT) is for?


use GetOpt\Command;
use GetOpt\GetOpt;
use GetOpt\Option;

class CopyCommand extends Command
{
    public function __construct()
    {
        $this->addOptions([
            Option::create("f", "file", GetOpt::REQUIRED_ARGUMENT)
        ]);

        parent::__construct("copy", [$this, "handle"]);
    }

    public function handle(GetOpt $getOpt)
    {
        print $getOpt->getOption("file");
    }
}

from getopt-php.

TCB13 avatar TCB13 commented on June 26, 2024

why your composer is configured to minimum version

I've no idea, I just followed the guide (composer require ulrichsg/getopt-php:"^3.0") and ended up with 3.0 due to some reason (?) and no I don't have anything else on composer.json.

from getopt-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.