项目作者: jacekciach

项目描述 :
A simple command-line options parser. The main idea was to create a class that will not require any configuration. The basic usage is to just create a new object and go.
高级语言: PHP
项目地址: git://github.com/jacekciach/commandline.git
创建时间: 2018-09-02T14:37:11Z
项目社区:https://github.com/jacekciach/commandline

开源协议:MIT License

下载


CommandLine

Build Status

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

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

Requirements

  • PHP 7

with Composer

  1. composer require jacekciach/commandline

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

Example 1.

  1. <?php
  2. require_once __DIR__. '/vendor/autoload.php';
  3. use CommandLine\CommandLine;
  4. $cmd = new CommandLine();
  5. var_export(array(
  6. "binary" => $cmd->binary(), // returns PHP_BINARY
  7. "script" => $cmd->script(), // returns the name of the script
  8. "options" => $cmd->options(), // returns all "options" passed from command line
  9. "params" => $cmd->params(), // returns all "params" passed from command line
  10. ));
  11. echo PHP_EOL;

So a command:

  1. $ php example1.php --start --msg="Hello, World!" file1.txt --book

will return something like:

  1. array (
  2. 'binary' => '/usr/bin/php7.0',
  3. 'script' => 'example1.php',
  4. 'options' =>
  5. array (
  6. 'start' => true,
  7. 'msg' => 'Hello, World!',
  8. ),
  9. 'params' =>
  10. array (
  11. 0 => 'file1.txt',
  12. 1 => '--book'
  13. ),
  14. )

Example 2.

  1. $ php example2.php --start --msg="Hellow World!" "Test Application" user
  1. <?php
  2. require_once __DIR__. '/vendor/autoload.php';
  3. use CommandLine\CommandLine;
  4. $cmd = new CommandLine();
  5. echo 'start = ' . var_export($cmd->option('start'), true) . PHP_EOL;
  6. echo 'msg = ' . var_export($cmd->option('msg'), true) . PHP_EOL;
  7. echo 'stop = ' . var_export($cmd->option('stop'), true) . PHP_EOL; // reading a not existing options will return NULL
  8. echo PHP_EOL;
  9. echo 'param(0) = ' . var_export($cmd->param(0), true) . PHP_EOL;
  10. echo 'param(1) = ' . var_export($cmd->param(1), true) . PHP_EOL;
  11. echo 'param(2) = ' . var_export($cmd->param(2), true) . PHP_EOL; // reading a not existing param will return NULL

will output:

  1. start = true
  2. msg = 'Hellow World!'
  3. stop = NULL
  4. param(0) = 'Application name'
  5. param(1) = 'user'
  6. param(2