
Build command line apps using PHP 7 - webNeat
https://github.com/tarsana/command
======
TazeTSchnitzel
I routinely use vanilla PHP (no special libraries) for CLI scripts. It's
actually fantastic for this. It has a large set of functions that do
everything you want, no imports required, and it has C-like syntax with real
types and functions.

~~~
VMG
How would you compare it to python?

~~~
astrodust
It's like Python that forgot to shave. For fifty years.

------
SwellJoe
One of the things I've hated about maintaining PHP web apps is how reliant on
the web interface they usually are; this has thankfully started to change.
Drupal (finally) has decent CLI tools. MediaWiki is among the few to have had
CLI tools for many years.

But, the thing that struck me about this project and the examples is just how
verbose PHP has gotten over the years. It is strikingly similar to old Java,
in terms of class definitions and the like. I know PHP has also gotten faster
and lot of "grown up" language features in that time, too, but some of its
charm as a language with strong "whipuptitude" is lost. I'm certain it's a
better language than it was a decade or two ago, and it was, frankly, awful by
many measures back then. But, it sure does take a lot of typing.

So, I like that things like this exist (though I likely wouldn't use it...I
really need most of my CLI tools to be standalone with no non-core
dependencies). I'd like it if every web developer knew how to make good CLI
apps and did so for every web app. I use Perl or POSIX shell mostly for my CLI
apps, but PHP makes perfect sense for tools that work with existing web apps
(the Drupal helper scripts I've written are mostly in PHP).

~~~
camus2
> But, the thing that struck me about this project and the examples is just
> how verbose PHP has gotten over the years. It is strikingly similar to old
> Java, in terms of class definitions and the like.

It's all opt-in. Nothing forces you to use Composer, classes or even type
hinting in PHP if you do not depend on third party libraries backed by
composer. Most of C extensions from 10 years ago are still here and procedural
code from that era would probably still work today on PHP 7.

~~~
SwellJoe
Sure, I think they're good things. But, some other languages manage to do the
same without so many letters and without so much boilerplate.

~~~
NightlyDev
Just curious - what languages do you have in mind?

~~~
SwellJoe
JavaScript has gotten extremely concise in recent years, even with type
annotations (with TypeScript or Flow). Perl with Moose/Moo is very concise. I
dunno about Ruby or Python in terms of type annotations, but both have more
concise class definition syntax. So, kinda all of the direct competitors to
PHP have a more concise syntax for the same features.

But, maybe more importantly, this seems to be using classes where it makes
little sense to use classes, but I see it a lot in PHP code. An idiomatic
implementation of the same kinds of tools in JS/Perl/Python/Ruby might be
shorter simply by virtue of not wrapping everything in a class. It's sort of
an anti-pattern to OOP-all-the-things in those languages, while in PHP there
seems to be a religious zeal for classes.

------
qualitytime
I feel this is an overkill since PHP provides very well for the command line
out of the box.

Main reason for commenting is the current (and I'm sure more) comments
questioning "why?" or "right tool for this job".

Please, HN overlords, please please add a flag (icon could be a cow) and total
counts so that I may filter out such comments.

~~~
hamandcheese
Comments already start to fade out as they get downvotes. It's easy enough to
skim past them.

~~~
wmichelin
Noob question (I've been on HN for over 2 years but I suppose I don't have
enough "karma" to do so), but how can one downvote posts?

~~~
grzm
While comments can be flagged and/or downvoted, submissions can only be
flagged. There's a karma threshold to flag submissions—offhand I don't recall
what it is.

From the FAQ
([https://news.ycombinator.com/newsfaq.html](https://news.ycombinator.com/newsfaq.html)):

> _Why don 't I see down arrows?_

> _There are no down arrows on stories. They appear on comments after users
> reach a certain karma threshold, but never on direct replies._

------
pbowyer
Looks good, there's also Symfony's console library which does a great job:
[https://symfony.com/doc/current/components/console.html](https://symfony.com/doc/current/components/console.html)

------
ourmandave
PHP is just the first lang. It's part of the Tarsana project which is his
framework for writing command line apps in any lang.

To quote the Tarsana project git page...

 _The PHP language was chosen simply because I wanted to rewrite my lumen-
generators using this framework.

Now I start to consider switching to Javascript..._

because javascript.

------
jameslk
PHP is not really a good candidate for something meant to be distributed to
other platforms due to the fun fact that a lot of custom configurations that
affect PHP's runtime behavior are held in a system-wide ini file. External
modules that allow certain features to work also need to be loaded from this
configuration file. This makes having your application work consistently
across platforms kind of a nightmare.

~~~
TazeTSchnitzel
php.ini has its problems, but I don't understand this remark:

> External modules that allow certain features to work also need to be loaded
> from this configuration file.

What's wrong with this? The modules in question (at lest, most important
extensions) are part of the core codebase and will be available as packages on
whatever platform you're using, and the package manager _should_ configure
them for you.

~~~
jameslk
The problem is that the distributed script(s), if the rely on external
modules, will have to guess if those modules are available, rather than just
bringing them with the script like other language dependency managers provide.

For example, with Ruby's gems or Node's npm, if I install a command line
package, external modules (native C extensions) will be installed as well.

------
ing33k
shameless plug :

I built a small CLI app using PHP for HTTP load testing ( WIP ), check it out

[https://github.com/vamsiikrishna/vex](https://github.com/vamsiikrishna/vex)

------
jpswade
How does this differ from Symfony console?

Why would you choose PHP7 vs another language?

Why do you need this level of abstraction just for a simple command line
function?

------
petraeus
Been doing this for years using CI and now Laravel 5

------
troels
Symfony already provides this functionality. I fail to see why I would use
this project over the de-facto standard PHP framework?

~~~
ceejayoz
Yeah, this does seem to be largely duplicating symfony/console (which anyone
using Laravel also has some experience with, incidentally).

~~~
webNeat
My first intention was to extend Symfony/Console and make it more easy to use,
but the main pain points were defining and parsing command line args, mocking
stdin and filesystem, ...

------
Cozumel
..but why? PHP can do all of this out of the box.

~~~
webNeat
Can you define and parse command line arguments that easy using raw PHP ?

~~~
TazeTSchnitzel
[https://php.net/getopt](https://php.net/getopt)

~~~
webNeat
I know this one, but it's not as powerful as using something like
[https://github.com/tarsana/syntax](https://github.com/tarsana/syntax),
especially if the arguments are very complicated like here
[https://github.com/webNeat/lumen-generators#resource-
generat...](https://github.com/webNeat/lumen-generators#resource-generator)
and need validation.

------
hnbroseph
ever feel like things are moving backwards?

------
carlmr
Why?

~~~
veeti
Why stop there? Forget Electron, build your next desktop application using
PHP-GTK!

[http://gtk.php.net/index.php](http://gtk.php.net/index.php)

~~~
jsjohnst
I've written a handful of apps using PHP-gtk (all written more than ten years
ago). I'm sure it's improved since then, but I'd still say it's not worth the
effort.

------
EGreg
When is PHP finally going to have a vibrant ecosystem of epoll-based async IO,
like Node?

AmPHP

ReactPHP

Kraken

And so on.

I want to run a PHP webserver and handle my own requests!

------
oscrperez
I mean, great exercise. But let's stop and think, is PHP the right tool for
this job? :P

~~~
megous
It is fine, especially if you want cli tool that can do http, database
connections, etc. without any external deps or other PHP code and want it
quickly.

