PHP code example of docopt / docopt

1. Go to this page and download the library: Download docopt/docopt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

docopt / docopt example snippets



    
    $doc = <<<DOC
    Naval Fate.
   
    Usage:
      naval_fate.php ship new <name>...
      naval_fate.php ship <name> move <x> <y> [--speed=<kn>]
      naval_fate.php ship shoot <x> <y>
      naval_fate.php mine (set|remove) <x> <y> [--moored | --drifting]
      naval_fate.php (-h | --help)
      naval_fate.php --version
   
    Options:
      -h --help     Show this screen.
      --version     Show version.
      --speed=<kn>  Speed in knots [default: 10].
      --moored      Moored (anchored) mine.
      --drifting    Drifting mine.
   
    DOC;
    
    


    
    ort form, simple API
    $args = Docopt::handle($doc);
   
    // short form (5.4 or better)
    $args = (new \Docopt\Handler)->handle($sdoc);
   
    // long form, simple API (equivalent to short)
    $params = array(
        'argv'=>array_slice($_SERVER['argv'], 1),
        'help'=>true,
        'version'=>null,
        'optionsFirst'=>false,
    );
    $args = Docopt::handle($doc, $params);
    
    // long form, full API
    $handler = new \Docopt\Handler(array(
        'help'=>true,
        'optionsFirst'=>false,
    ));
    $handler->handle($doc, $argv)

    
    
    $doc = <<<DOC
    Usage: my_program.php [-hso FILE] [--quiet | --verbose] [INPUT ...]
    
    Options:
      -h --help    show this
      -s --sorted  sorted output
      -o FILE      specify output file [default: ./test.txt]
      --quiet      print less text
      --verbose    print more text
   
    DOC

    
    
    $doc = <<<DOC
    Usage: my_program.php [-hso FILE] [--quiet | --verbose] [INPUT ...]
   
    -h --help    show this
    -s --sorted  sorted output
    -o FILE      specify output file [default: ./test.txt]
    --quiet      print less text
    --verbose    print more text
   
    DOC


    
    array(
      '--drifting'=>false,         'mine'=>false,
      '--help'=>false,             'move'=>true,
      '--moored'=>false,           'new'=>false,
      '--speed'=>'15',             'remove'=>false,
      '--version'=>false,          'set'=>false,
      '<name>'=>array('Guardian'), 'ship'=>true,
      '<x>'=>'100',                'shoot'=>false,
      '<y>'=>'150'
    )