Coder Social home page Coder Social logo

commandline.php's Introduction

CommandLine.php

PHP Command Line interface class. Provides friendly and flexible CLI argument parsing.

Usage

$args = CommandLine::parseArgs($_SERVER['argv']);

This command line option parser supports any combination of three types of options [single character options (-a -b or -ab or -c -d=dog or -cd dog), long options (--foo or --bar=baz or --bar baz) and arguments (arg1 arg2)] and returns a simple array.

[pfisher ~]$ php test.php --foo --bar=baz --spam eggs
  ["foo"]   => true
  ["bar"]   => "baz"
  ["spam"]  => "eggs"

[pfisher ~]$ php test.php -abc foo
  ["a"]     => true
  ["b"]     => true
  ["c"]     => "foo"

[pfisher ~]$ php test.php arg1 arg2 arg3
  [0]       => "arg1"
  [1]       => "arg2"
  [2]       => "arg3"

[pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --also-funny=spam=eggs \
> 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s
  [0]       => "plain-arg"
  ["foo"]   => true
  ["bar"]   => "baz"
  ["funny"] => "spam=eggs"
  ["also-funny"]=> "spam=eggs"
  [1]       => "plain arg 2"
  ["a"]     => true
  ["b"]     => true
  ["c"]     => true
  ["k"]     => "value"
  [2]       => "plain arg 3"
  ["s"]     => "overwrite"

Not supported: -cd=dog.

@author Patrick Fisher [email protected]

@since August 21, 2009

@see

Note: parseArgs.php contains a "minified" version of the same code, for your copypasta pleasure. For little scripts for which you want to have a nice interface.

commandline.php's People

Contributors

pwfisher avatar honzabrecka avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.