On 7 January 2011 16:55, la...@garfieldtech.com <la...@garfieldtech.com> wrote:
> Hi folks.  I have a project coming up that will involve writing a
> non-trivial command line PHP application.  Most of it will be nice and
> abstracted and standalone and all of that jazz, but it will need to do
> command line interation.  I'm not sure yet if it will be interactive or if I
> just need to parse lots of command line switches.
> Has anyone used a CLI-handling library they like?  I recall briefly using
> the PEAR CLI library many many years ago and disliking it because it was
> only barely a step above the raw PHP-CLI SAPI, and still required lots of
> if-else branching in my code.  I don't know if there's anything better since
> then, however.  I prefer clean OO to procedural, but can work with
> procedural if needs be.  The fewer dependencies it has the better as well.
> Any recommendations?
> (Open source, GPLv2-compatible required.)
> --Larry Garfield
Hello Larry,

Sorry for being late to the game.

I use PHP on Windows and I use PHP CLI for nearly everything I do that
isn't web based. Occasionally I'll use a .BAT file, but invariably, I
need logging and reporting, etc. I get all of that from pre-existing
PHP classes, so a no brainer in that regard.

I co-maintain the PEAR Console_CommandLine package [1]. This allows
quite complex command line argument setups.

At the bottom of this post, I've included the help screen from an app.
Long/short options all catered for. "commands" can have their own
options/arguments too (so script command --help would show that
command's help). If you are on windows, take a look at PHP command
line usage on windows [2] to make things a lot easier for yourself.

The app shows me differences in source files between different servers
(live, dev, test, etc). To get values during run time from the
console, I use (in the most basic of example),


As PHP on windows requires an [ENTER] key to be pressed to pass the
typed string to the code (even for fgetc(STDIN) ), then this may not
be what you want.

Try this ...

php -n -r "echo fgetc(STDIN);"

If you can then press 1 letter and see 2 copies of it and the program
quits, then that's it - in a nutshell. On windows, I have to press
[ENTER]. If I type abc[ENTER], then only the a is returned and the bc
is buffered somewhere.

php -n -r "echo fgetc(STDIN), fgetc(STDIN);"

shows the buffering.

I also use Jason Hood's ANSICON [3] (full source available), which
allows me to output coloured text in PHP using a simple home-brew
class (ANSI::Write($text, $foreground = null, $background = null)). If
you build phpdoc using PhD and enable the coloured output, all looks
great with ANSICON.

ANSICON supports quite a few of the non colour related ANSI code
sequences (cursor positing, inserting/deleting lines/characters/etc.),
so maybe doing things like drawing windows, boxes, menus, etc. could
all be done using just ANSICON. It would be a LOT of work for probably
not a lot of users.

As far as I know, there are no libraries for PHP on windows allowing
you to do this sort of thing from the command line. I know of curses
(ncurses ?), so if you are on something other than windows, these are
an option for colour processing.

As for "pushing the envelope" ...

I run multiple PHP scripts as Windows Services using the pecl
extension win32service [4], implementing multiple "threads" using ...

$WshShell = new COM("WScript.Shell");

and have "shared memory" via WinCache [5].

All command line scripts in essence. Why? Well. I know PHP quite well.
I understand the 2 extensions very well. I can re-use all my existing
code/frameworks. And I can fit these service into the environment in
such a way that the system admins/sysops only see these tasks as
normal windows services. If they need to reboot a server bank, the
scripts shutdown automatically, appropriately and correctly and
restart after the reboot. I'm just working on adding dependency to the
services so that if (for example) they take down the mail server for
some reason, any of the PHP services requiring mail will also close
down correctly. Just as if you had a "real" service.

And, once I'd found the solution and documented it, using PHP scripts
as command line filters, allows you to do some pretty complex argument

collect --source internet | filter email | updateDB --server x --user
y --password z | generateReports --smtp random

for example, where collect, filter, updateDB and generateReports are
all PHP scripts which acccept command line parameters as well as use
stdin to read data in realtime. Just like | more would.


[1] http://pear.php.net/package/Console_CommandLine
[2] http://docs.php.net/manual/en/install.windows.commandline.php
[3] http://adoxa.110mb.com/ansicon/index.html
[4] http://pecl.php.net/package/win32service
[5] http://pecl.php.net/package/wincache

 VD3 : Source code synchronisation.

View and synchronize source code modules.

  VD3 [options]
  VD3 [options] <command> [options] [args]

  -m module(s), --module=module(s)            available modules
(repeat option for multiple modules)
                                              The modules are :
                                               AC      ( Accounts
Department ) Validate an Excel Worksheet against the Access Accounts
Purchase Ledger          Default
                                               CCC     ( TripleC
      ) Operator Activity Report, Bank Summary, Narrative and
Collection PDF Generation  Default
                                               EIQ     ( EarnieIQ
      ) Backup of Earnie IQ Payroll SQL Databases
                                               GLOBAL  ( All Sites
      ) Globally available Audio, CSS Stylesheets, JavaScript and
Images                 Special
                                               INI     ( INI
      ) PHP Configuration Files
                                               MU      ( Merlin
      ) Backup of Merlin SQL Database
                                               PH      ( Phone Bills
      ) Mobile Phone Usage Recharging
                                               PHP     ( Includes
      ) Server side PHP include files and templates
                                               RC      ( Recharge
      ) Recharge Authorised Work
                                               SO      ( Sales
Operations    ) Purchase Order Validation, Fax Decoder, Amend Period,
Gather Faxes, etc.         Default

        The sub-modules are:

         INC   ( local_includes ) Data files

         TMPL  ( Templates      ) Templates
                                               TW      ( Timeware
      ) Timeware Absence Colours
                                               VS      ( Vehicle
Services    ) Online Vehicle Services
                                               WR      ( Web Reports
      ) Fleet Management System Web Reporter
                                               WS      ( Web Services
      ) SOAP based Web Services
  -s submodule(s), --sub-module=submodule(s)  submodule appropriate to
a specific module - see --help for details
  -d, --differences-only                      show differences only
  -i, --include-missing                       include missing files as different
  -h, --help                                  show this help message and exit
  -v, --version                               show the program version and exit

  sync  Synchronize all information
  push  Push file(s) to all systems
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to