Download the PHP package horde/argv without Composer

On this page you can find all versions of the php package horde/argv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package argv


Horde_Argv


Contents


Basic Usage


While Horde_Argv is quite flexible and powerful, you don't have to jump through hoops or read reams of documentation to get started. This document aims to demonstrate some simple usage patterns that will get you started using Horde_Argv in your scripts.

To parse a command line with Horde_Argv, you must create an Horde_Argv_Parser instance and define some options. You'll have to import the Horde_Argv_Parser classes in any script that uses Horde_Argv, but it is suggested that you use an autoloader instead:

require_once 'Horde/Autoloader/Default.php';

Early on in the main program, create a parser:

$parser = new Horde_Argv_Parser();

Then you can start defining options. The basic syntax is:

$parser->addOption('opt_str', ..., array('attr' => 'value', ...));

That is, each option has one or more option strings, such as "-f" or "--file", and several option attributes that tell Horde_Argv what to expect and what to do when it encounters that option on the command line.

Typically, each option will have one short option string and one long option string, e.g.:

$parser->addOption('-f', '--file', ...);

You're free to define as many short option strings and as many long option strings as you like, as long as there is at least one option string overall.

Once all of your options are defined, instruct Horde_Argv to parse your program's command line:

list($values, $args) = $parser->parseArgs();

(You can pass an argument list to parseArgs() if you like, but that's rarely necessary: by default it uses $_SERVER['argv'].)

parseArgs() returns two values:

This tutorial document only covers the four most important option attributes: "action", "type", "dest" (destination), and "help". Of these, "action" is the most fundamental.

Option actions

Actions tell Horde_Argv what to do when it encounters an option on the command line. There is a fixed set of actions hard-coded into Horde_Argv; adding new actions is an advanced topic covered in Extending Horde_Argv. Most actions tell Horde_Argv to store a value in some variable -- for example, take a string from the command line and store it in an attribute of options.

If you don't specify an option action, Horde_Argv defaults to "store".

The store action

The most common option action is store, which tells Horde_Argv to take the next argument (or the remainder of the current argument), ensure that it is of the correct type, and store it to your chosen destination.

For example:

$parser->addOption(
    '-f', '--file',
    array('action' => 'store', 'type' => 'string', 'dest' => 'filename')
);

Now let's make up a fake command line and ask Horde_Argv to parse it:

$args = array('-f', 'foo.txt');
list($values, $args) = $parser->parseArgs(args);

When Horde_Argv sees the "-f", it consumes the next argument, "foo.txt", and stores it in $values->filename, where values is the first return value from parseArgs(). So, after this call to parseArgs(), $values->filename is "foo.txt".

Some other option types supported by Horde_Argv are "int" and "float". Here's an option that expects an integer argument:

$parser->addOption('-n', array('type' => 'int', 'dest' => 'num'));

Note that I didn't supply a long option, which is perfectly acceptable. I also didn't specify the action, since the default is "store".

Let's parse another fake command-line. This time, we'll jam the option argument right up against the option -- "-n42" (one argument) is equivalent to "-n 42" (two arguments).

list($values, $args) = $parser->parseArgs(array('-n42'));
echo $values->num;

will print "42".

Trying out the "float" type is left as an exercise for the reader.

If you don't specify a type, Horde_Argv assumes "string". Combined with the fact that the default action is "store", that means our first example can be a lot shorter:

$parser->addOption('-f', '--file', array('dest' => 'filename'))

If you don't supply a destination, Horde_Argv figures out a sensible default from the option strings: if the first long option string is "--foo-bar", then the default destination is "foo_bar". If there are no long option strings, Horde_Argv looks at the first short option: the default destination for "-f" is "f".

Adding types is covered in "Extending Horde_Argv".

Handling flag (boolean) options

Flag options -- set a variable to TRUE or FALSE when a particular option is seen -- are quite common. Horde_Argv supports them with two separate actions, "store_true" and "store_false". For example, you might have a verbose flag that is turned on with "-v" and off with "-q":

$parser->addOption('-v', array('action' => 'store_true', 'dest' => 'verbose'));
$parser->addOption('-q', array('action' => 'store_false', 'dest' => 'verbose'));

Here we have two different options with the same destination, which is perfectly OK. (It just means you have to be a bit careful when setting default values -- see Default values, below.)

When Horde_Argv sees "-v" on the command line, it sets the verbose attribute of the special "option values" object to a TRUE value; when it sees "-q", it sets verbose to a FALSE value.

Other actions

Some other actions supported by Horde_Argv are:

:"store_const": store a constant value :"append": append this option's argument to a list :"count": increment a counter by one :"callback": call a specified function

These are covered in the Option Callbacks documents.

Default values

All of the above examples involve setting some variable (the "destination") when certain command-line options are seen. What happens if those options are never seen? Since we didn't supply any defaults, they are all set to NULL. Usually, this is just fine, but sometimes you want more control. To address that need, Horde_Argv lets you supply a default value for each destination, which is assigned before the command-line is parsed.

First, consider the verbose/quiet example. If we want Horde_Argv to set verbose to TRUE unless "-q" is seen, then we can do this:

$parser->addOption('-v', array('action' => 'store_true', 'dest' => 'verbose', $default => true));
$parser->addOption('-q', array('action' => 'store_false', 'dest' => 'verbose'));

Oddly enough, this is exactly equivalent:

$parser->addOption('-v', array('action' => 'store_true', 'dest' => 'verbose'));
$parser->addOption('-q', array('action' => 'store_false', 'dest' => 'verbose', $default => true));

Those are equivalent because you're supplying a default value for the option's destination, and these two options happen to have the same destination (the verbose variable).

Consider this:

$parser->addOption('-v', array('action' => 'store_true', 'dest' => 'verbose', $default => false));
$parser->addOption('-q', array('action' => 'store_false', 'dest' => 'verbose', $default => true));

Again, the default value for verbose will be TRUE: the last default value supplied for any particular destination attribute is the one that counts.

A clearer way to specify default values is the setDefaults() method of Horde_Argv_Parser, which you can call at any time before calling parseArgs():

$parser->setDefaults(array('verbose' => true));
$parser->addOption(...);
list($values, $args) = $parser->parseArgs();

As before, the last value specified for a given option destination is the one that counts. For clarity, try to use one method or the other of setting default values, not both.

Generating help

There is one more feature that you will use in every script: Horde_Argv's ability to generate help messages. All you have to do is supply a help value for each option. Let's create a new parser and populate it with user-friendly (documented) options:

$usage = 'usage: %prog [options] arg1 arg2';
$parser = new Horde_Argv_Parser(array('usage' => $usage));
$parser->addOption(
    '-v', '--verbose',
    array('action' => 'store_true', 'dest' => 'verbose', $default => 1,
          'help' => 'make lots of noise [default]')
);
$parser->addOption(
    '-q', '--quiet',
    array('action' => 'store_false', 'dest' => 'verbose', 
          'help' => 'be vewwy quiet (I'm hunting wabbits)')
);
$parser->addOption(
    '-f', '--filename',
    array('metavar' => 'FILE', 'help' => 'write output to FILE')
);
$parser->addOption(
    '-m', '--mode',
    array('default' => 'intermediate',
          'help' => 'interaction mode: one of "novice", "intermediate" [default], "expert"')
);

If Horde_Argv encounters either '-h' or '--help' on the command-line, or if you just call $parser->printHelp(), it prints the following to stdout:

usage: <yourscript> [options] arg1 arg2

options:
  -h, --help           show this help message and exit
  -v, --verbose        make lots of noise [default]
  -q, --quiet          be vewwy quiet (I'm hunting wabbits)
  -fFILE, --filename=FILE
                       write output to FILE
  -mMODE, --mode=MODE  interaction mode: one of 'novice', 'intermediate'
                       [default], 'expert'

There's a lot going on here to help Horde_Argv generate the best possible help message:

Print a version number

Similar to the brief usage string, Horde_Argv can also print a version string for your program. You have to supply the string, as the version argument to Horde_Argv_Parser:

$parser = new Horde_Argv_Parser(array('usage' => '%prog [-f] [-q]', 'version' => '%prog 1.0'));

Note that "%prog" is expanded just like it is in usage. Apart from that, version can contain anything you like. When you supply it, Horde_Argv automatically adds a "--version" option to your parser. If it encounters this option on the command line, it expands your version string (by replacing "%prog"), prints it to stdout, and exits.

For example, if your script is called "/usr/bin/foo", a user might do:

$ /usr/bin/foo --version
foo 1.0

Error-handling

The one thing you need to know for basic usage is how Horde_Argv behaves when it encounters an error on the command-line -- e.g. "-n4x" where the "-n" option takes an integer. Horde_Argv prints your usage message to stderr, followed by a useful and human-readable error message. Then it terminates with a non-zero exit status by calling exit().

If you don't like this, subclass Horde_Argv_Parser and override the parserError() method. See Extending Horde_Argv.

Putting it all together

Here's what a Horde_Argv-based scripts usually look like:

require_once 'Horde/Autoloader/Default.php';

[...]

$usage = 'usage: %prog [options] arg';
$parser = new Horde_Argv_Parser(array('usage' => $usage));
$parser->addOption(
    '-f', '--file',
    array('type' => 'string', 'dest' => 'filename',
          'help' => 'read data from FILENAME')
);
$parser->addOption(
    '-v', '--verbose',
    array('action' => 'store_true', 'dest' => 'verbose')
);
$parser->addOption(
    '-q', '--quiet',
    array('action' => 'store_false', 'dest' => 'verbose')
);
[... more options ...]

list($values, $args) = $parser->parseArgs();
if (count($args) != 1) {
    $parser->parserError('incorrect number of arguments');
}

if ($values->verbose) {
    printf('reading %s...%n', $values->filename);
}

[... go to work ...]

All versions of argv with dependencies

PHP Build Version
Package Version
Requires php Version ^8
horde/cli Version ^3 || dev-FRAMEWORK_6_0
horde/exception Version ^3 || dev-FRAMEWORK_6_0
horde/translation Version ^3 || dev-FRAMEWORK_6_0
horde/util Version ^3 || dev-FRAMEWORK_6_0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package horde/argv contains the following files

Loading the files please wait ...