Build command line apps using PHP 7
github.com
github.com
Vanilla PHP has better argument parsing than many recent hipster language. Go has some sort of ugly non conventional argument library which is barely usable, Node has none. But nothing beats vanilla Python for that tasks IMHO.
import "flag"
var ip = flag.Int("flagname", 1234, "help message for flagname")
...
// flag.Parse() once in the main function
vs import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number",
type=int)
...
args = parser.parse_args()
In the Go example you get a pointer just like in C, in the python example you get a special object and can access its fields. Both are simple, but Golang is IMHO closer to C in this matter.flag package bothers me because it breaks getopt conventions for no good reason.
> . . . argument parsing . . .
> Node has none.
https://nodejs.org/docs/latest/api/process.html#process_proc...This is not argument parsing. I'm pretty sure Node doesn't even do anything, it just gets an array of strings from the CLI, argv C style isn't getting options prefixed with - or -- directly from the CLI, and putting them in a map or something. So no, node has no facility in its standard library for that. Node is not known for its extensive standard library in anyway.
I know Perl, C, C++ and PHP quite well, but I'd never, ever call C-like syntax a plus.
It's astonishing how feeble modern PHP is, that to comapre strings you often have to use strcmp. In 2017. It's like all the cruft of ANSI C with a whole bunch of $ characters spiked in.
C-style syntax is fairly popular [1]. If it had lisp syntax or haskell syntax most people would complain that they can't figure out what's going on without learning all that syntax.
>that to comapre strings you often have to use strcmp
For equality === works just fine. For < and > there is no one obviously correct order, so it's entirely reasonable to use functions for that.
1: https://en.wikipedia.org/wiki/List_of_C-family_programming_l...
Well, versus whatever abomination bash uses, it's quite readable…
It's the most common syntax pattern of popular languages. Statistically, more developers know a language with a "C-like" syntax than Lisp, Haskell, Erlang..
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).
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.
And yet nearly everybody does. Is there a compelling upside to justify that tradeoff? I can't think of a worse fate for a language than trying to mimic Java circa 1998, but that's exactly what's happening.
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.
Something that probably hurts the language here is the lack of autoloading for functions, whereas it exists for classes, so exposing bare functions is a runtime performance penalty (their containing files must be loaded on every request, if you use Composer), which results in a preference for putting everything in a class. :/
That's not to denigrate drush or the developer(s) of drush. drush is/was a remarkable improvement over the pre-drush era. I just think Drupal wasn't built to be driven by a CLI. It isn't API-native, by any stretch of the imagination, nor is it front-end agnostic. So, drush is historically working around rather than working with, and it shows in that most of Drupal is still inaccessible to drush (at least in Drupal 7; I haven't deployed Drupal 8 and probably never will as the migration from 6 to 7 was too painful for me to be willing to ever do it again).
So...I think we may just disagree on what "decent" and "finally" means. Also, compare to some other ways to build web apps; most modern web frameworks have pretty obvious ways to build CLI tools, and often are built around standard frameworks for doing so (Yeoman or generators/console in Rails). Drupal being based on Symfony now gets it a lot of that same kind of thing; so, it is recently decent, by my estimation. Others may disagree, of course.
Like me. As far as I can see, while Symfony might be a decent framework but the integration between Drupal and Symfony is atrocious because it's an incredibly bad fit as the two systems had fundamentally different philosophies, mostly in the explicit vs implicit department. Compare a Drupal hook implementation to a Symfony event subscriber and you will see.
And I must be missing something what Symfony Console apps bring to the table compared to the absolutely trivial drush implementations... But each to their own, I guess.
I feel like Drupal could be a workshop on why "a wrong abstraction is worse than no abstraction". I'd assumed things were better on that front in D8, based on what I'd read, and based on the theory that at least the wrong abstractions would be common across everything (Drupal has always had the problem of a dozen different modules for every trivial thing, and every module needs a different combination of those trivial thing modules). I'd hope, at least, that database abstractions are nicer? The database API in Drupal 7 is, frankly, awful.
While Larry Garfield architected it, yours truly here put in quite some work implementing the API you like so much :) But that's just context -- I could ask you what are your problems but I won't, I am out and the API is frozen until D9. I would recommend filing an issue to tell what you don't like.
My problem is that by the time something reaches the database is it completely unrecognizable and is spewed all over the database in a half dozen tables. This is a problem of D7 much more than D6. It takes a handful of queries to build an "entity", for instance, and forget hitting the database from outside of Drupal.
Every CMS we've used in the past, I was able to use the database as the source of truth across many apps in many languages; the database was effectively the API I used to share data across our Perl and Python apps and Drupal. With Drupal 7 I can't do that at all. I have to go through Drupal to pull anything out or put anything into the database in a way that Drupal can work with it. It's just a pain in the ass. And, Drupal 7 is an awful experience for building APIs, so I can't really integrate stuff that way, either. I understand improving the API building experience is a focus for D8.
Anyway, I want my database to look like my model, and Drupal 7 doesn't do that. It spreads the model across a whole bunch of tables (I've got like 60 "drupal_field" tables in my D7 database...I have no idea what to do with all that mess). And, the APIs in Drupal to work with entities and fields and such are really ornery to work with and getting useful errors out of them is impossible. That's not the fault of the low-level database API, but it is the fault of how Drupal 7 implements putting things into the database. If that makes sense.
Mostly I've just realized Drupal doesn't fit my mental model of how CRUD apps should work, and working with it has been a real chore. I know lots of people are super productive with it, but in several years of poking at it in my spare time, I still don't "get it". I honestly still don't even have a good mental model for how the pieces fit together; it's so big, there are so many competing ways to do anything, it's full of re-implementations from one version to the next, etc. I can't make any sense out of it, and every time I come back to it it takes me hours just to get up to speed enough to make minor changes.
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.
From the FAQ (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.
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.
> 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.
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.
I built a small CLI app using PHP for HTTP load testing ( WIP ), check it out
Why would you choose PHP7 vs another language?
Why do you need this level of abstraction just for a simple command line function?
one example: https://docs.moodle.org/22/en/Administration_via_command_lin...
?
PHP enforces memory limits out of the box by default. What languages have execution limits on CLI?
There are plenty of companies that rely on PHP that also have to work with CLI. I believe they are better informed when it comes to making decisions between handling CLI via an entirely different language and sticking to PHP.
Perhaps Peachpie, when it reaches production quality, will also help with that, as then calling .NET (and maybe, through that, WinRT) APIs from PHP code will be made easier.
AmPHP
ReactPHP
Kraken
And so on.
I want to run a PHP webserver and handle my own requests!
Let's stop and think. Do you know what the job is or are you calling it in with this comment?
[COW ICON]