chrisallenlane / pharse Goto Github PK
View Code? Open in Web Editor NEWA command-line option-parsing class for PHP
A command-line option-parsing class for PHP
Test:
PHPUnit could be overkill, so I might just try to hack something together using assert()
statements. This doesn't need to be complicated.
I have an application that should show help and a -h[ost] option. However, I think it's pointless to have a shortcut for --help, and would like to disable it, but if I try to put the argument as false, the library creates the shortcut as "-" simple, and null creates a default one.
I think this could improve code readability and creation, since you can rely on IDE Autocompletion to know what types you have available.
I can implement this, if you agree (:
How does the default option works?
I've set an option with a default, and required: false; however, if the user does not specify that option, I don't get anything by default too... It works, though, if the user specify the option without a value.
There's nothing about this in the example either. Could you give a valid example?
For example, --user-name
seems to be parsed as user
and name
options. This is kind of a big flaw, and I'm not sure how I missed it before. The issue seems to originate somewhere around line 44
or so.
I tested the code so that we can start using it in our projects. For this I've forked the code and try to make it match our corporate standards. I guess the new code is very different from the original ideas here and it would be really hard to pull changes back. But I can put the list of issues I have found.
'0' value for the parameter is considered empty and not allowed
For example
ubuntu@soft-rose:/home/test/pharse$ ./example.php --username 0
Error: required value for --username was not specified.
The check 0 == null
is true in PHP and this is why '0' parameter value is considered empty.
if ($this->required && $this->value == null) {
String parameter can not have numeric value
For example
ubuntu@soft-rose:/home/test/pharse$ ./example.php --username 123
Error: option --username must be a string.
Parameter type is not checked for non required parameters
To test this you need to change example file
'username' => array(
'description' => 'Your username',
'default' => 'admin',
'type' => Pharse::PHARSE_INTEGER,
'required' => false,
'short' => 'u',
),
Then when you invoke it like this no error is shown
ubuntu@soft-rose:/home/test/pharse$ ./example.php --username abc --password abc
Only root is allowed to log in. Disconnect immediately or we will do literally nothing about it.
PHP unit tests
I have to admit I don't know how would you write PHP unit tests because of all these die()
statements in the code. I can understand that this is part of the concept but it makes testing very complicated. Also the code with static methods and properties is hard to test too.
Hope this might be helpful.
Thank you for your time. And wish you happy coding ;)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.