承接 yeriomin/getopt 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yeriomin/getopt

最新稳定版本:v1.0.1

Composer 安装命令:

composer require yeriomin/getopt

包简介

A small PHP getopt helper/library. Provides a customizable input parameter interpretation and usage message generation.

README 文档

README

#getopt

Latest Version Build Status Scrutinizer Code Quality SensioLabsInsight

A small PHP getopt helper/library. Provides a customizable input parameter interpretation and usage message generation.

Usage

$ composer require yeriomin/getopt

<?php
include 'vendor/autoload.php';
$getopt = new \Yeriomin\Getopt\Getopt();
$arguments = $getopt->getArguments();

Features

  1. Obviously it parses command line options. Uses $_SERVER['argv'].
  2. Short and long options, arguments.
  3. Short options clustering.
  4. Required options.
  5. -- argument separator.
  6. Usage message generation.

Detailed usage

Arguments and options

Lets see how getopt deals with the following input:

$ php somescript.php arg1 --option1 value1 -o value2 -abc -- --option3 value3

Getting option value

echo $getopt->option1; // "value1"
echo $getopt->optionWhichIsNotProvided; // null

getArguments()

Returns an array of arguments. For the above example returns:

array(3) {
  [0] =>
  string(4) "arg1"
  [1] =>
  string(9) "--option3"
  [2] =>
  string(6) "value3"
}

getOptionsLong()

Returns an array of long options. For the above example returns:

array(1) {
  'option1' =>
  string(6) "value1"
}

getOptionsShort()

Returns an array of short options. For the above example returns:

array(4) {
  'o' =>
  string(6) "value2"
  'a' =>
  bool(true)
  'b' =>
  bool(true)
  'c' =>
  bool(true)
}

Defining options

If you just want to get console arguments, you just need the three methods covered in the previous section. However giving getopt definitions of options you expect lets you define required options and get a usage message.

addOptionDefinition()

$optionDefinition = new \Yeriomin\Getopt\OptionDefinition(
    'c',
    'config',
    'Path to a configuration file'
);
$getopt->addOptionDefinition($optionDefinition);

Doing this defines -c|--config option. Providing -c populates --config and vice versa.

Required options

$optionDefinition = new \Yeriomin\Getopt\OptionDefinition(
    'c',
    'config',
    'Path to a configuration file',
    true
);
$getopt->addOptionDefinition($optionDefinition);

Forth argument in the OptionDefinition constructor makes option required. You can do the same with $optionDefinition->setRequired(). If any required option is not provided, getopt will throw an exception when getArguments, getOptionsShort or getOptionsLong are called.

getUsageMessage()

This method lets you get a usage message based on the options you have defined. Is looks somewhat like a man page.

<?php
include 'vendor/autoload.php';
$getopt = new \Yeriomin\Getopt\Getopt();
$optionConfig = new \Yeriomin\Getopt\OptionDefinition(
    'c',
    'config',
    'Path to a configuration file',
    true
);
$getopt->addOptionDefinition($optionConfig);
$optionHelp = new \Yeriomin\Getopt\OptionDefinition(
    'h',
    'help',
    'Show script help message',
);
$getopt->addOptionDefinition($optionHelp);
try {
    $configPath = $getopt->c;
} catch (\Yeriomin\Getopt\GetoptException $e) {
    echo $e->getMessage() . "\n";
    echo $getopt->getUsageMessage();
    exit(1);
}

Trying to run this script with no arguments would give us the following message:

Missing required options: -c/--config
Usage: somscript.php [OPTIONS] [ARGUMENTS]

Options:
 -c, --config Path to a configuration file
 -h, --help   Show script help message

统计信息

  • 总下载量: 182
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-09