PHPackages                             jacekciach/commandline - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. jacekciach/commandline

ActiveLibrary[CLI &amp; Console](/categories/cli)

jacekciach/commandline
======================

A simple command-line options parser

v1.6.2(7y ago)019MITPHPPHP ^7.0

Since Sep 2Pushed 7y ago1 watchersCompare

[ Source](https://github.com/jacekciach/commandline)[ Packagist](https://packagist.org/packages/jacekciach/commandline)[ RSS](/packages/jacekciach-commandline/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (1)Versions (12)Used By (0)

CommandLine
===========

[](#commandline)

[![Build Status](https://camo.githubusercontent.com/b397ef5e459f8fa4265b274445445919bdbba9742b0fe3ebc79c093358674d9f/68747470733a2f2f7472617669732d63692e636f6d2f6a6163656b63696163682f636f6d6d616e646c696e652e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/jacekciach/commandline)

A simple command-line options parser.

The main idea was to create a class that will not require any configuration.

A basic usage is to just create a new object and go.

Description
-----------

[](#description)

The class parses all arguments passed in `$argv`. Parsed arguments are easily accessed with class' methods. These methods are documents in `CommandLine.php`.

A command-line argument can be either an `option` or a `param`:

- `options` have to be passed before `params`; when the parser encounters the first `param`, all arguments that come after will be treated as `params` as well
- an `option` is an argument starting with a dash `-` (i.e. `-v`, `-4=yes`) or dashes `--` (i.e. `--enabled`, `--start=now`)
- `params` start with a first argument not being an `option`
- the class generally won't filter `options`, make them required, optional, etc.
- however, the class supports `$allowedOptions` and `$shortOptionsMap` (see the class' `__construct` documentation); they are optional and `CommandLine` works perfectly well without using them

A developer needs to implement a logic connected with parsed arguments (their meaning, correctness, dependencies, etc.). The class will not take care of these things: its purpose is to be a convenient "reader" of `$argv`.

Installation
------------

[](#installation)

### Requirements

[](#requirements)

- PHP 7

### with Composer

[](#with-composer)

```
composer require jacekciach/commandline

```

### without Composer

[](#without-composer)

Just download `CommandLine.php` from the repository; this is the only required file.

Then include this file in your code with `include` or `require`.

Examples
--------

[](#examples)

### Example 1.

[](#example-1)

```
