Comments (6)
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.
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.
http://getopt-php.github.io/getopt-php/installation.html updated
from getopt-php.
@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.
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.
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)
- Version 3.3 HOT 2
- Error Reporting is Atrocious HOT 7
- ArgumentException should not be sent immediately HOT 11
- Add option to validate default value HOT 5
- Can help be a command? HOT 2
- Modifications needed for PHP 7.4 compliance HOT 1
- Option with missing REQUIRED_ARGUMENT consumes next option? HOT 2
- A lot of warnings in travis for newer php versions HOT 9
- Required Option HOT 1
- Cleaning up merged branches HOT 3
- Outdated CHANGELOG.md file HOT 3
- What are the version constraints? HOT 3
- Error on PHP 8.1.0 HOT 2
- Failed Operand validation should throw non-zero exit code HOT 2
- Replace gitlab-ci with a better integrated solution HOT 1
- Missing return type signature in PHP 8.1 (actuel version from packagist 4.2.0)
- Localization ca.php HOT 2
- How to setup arguments (or named operands)? HOT 1
- CommandInterface misses getHandler() method HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from getopt-php.