Code Monkey home page Code Monkey logo

cling's Introduction

Cling

Cling is a Micro Framework for building Command Line Applications

Features

  • Routing of Command Line Arguments
  • Short and Long Arguments
  • Automagic --help creation
  • Configuration

Todo

As Cling is very new, there's still lot of work left to be done

  • Testing
  • Template System
  • Logging
  • Proper Error and Eception Handling
  • The :stdin command needs to check if there is data coming in on

Requirements

  • PHP 5.3

"Hello World" Application

<?php
require 'Cling/Cling.php';

$app = new Cling();
$app->command('hello-world:', function($name) {
    echo "Hello $name\n";
})->help("Hello World Example Command.");

$app->run();

Documentation

Configure Cling

To configue Cling, you can pass an associative array to the constructor method. The Options are:

  • debug Enable or Disable debug output (Default: false)

Routes

Command Line Options

A Command Line Option can be handled by the command function:

$app->command('<Long Option Name>', '<1 letter short option'>, '<callable>');

The Short Option parameter is optional.

Special Routes

A :* command is a Route that is executed regardless of Command Line Options. Multiple :* Routes are executed in the order they are set.

Custom not Found Handler

If a user calls the application without specifying a valid command line option, the notFound handler is called. You can override the default handler, by calling the notFound method with a user function:

$app = new Cling();

$app->notFound(function() use ($app) {
    echo "I can't help you...\n";
});

The application will exit immediatle if the notFound method is being called.

Views

Console Applications should, just like a Website, facilitate Views:

$app = new Cling(array('template.path' => 'views/'));

$app->command('hello-world:', function($name) use ($app) {
    $app->view()->set('name', $name);
    $app->view()->display('example.txt.php')
})->help("Example with a View.");

The view would look then something like:

Hello <?php echo $name; ?>!

I Hope You're doing fine.

Logging

To Log message to files, stdout or stderr:

$app = new Cling(array('log.destination' => \Cling\Logger::LOG_STDOUT));

$app->log()->info('Hello World!');

Log Destinations

  • \Cling\Logger::LOG_STDOUT => Log to STDOUT
  • \Cling\Logger::LOG_STDERR => Log to STDERR
  • \Cling\Logger::LOG_FILE => Log to a file in a directory set by 'log.dir'
  • \Cling\Logger::LOG_ERROR_LOG => Log via Default 'error_log'

Log Levels

The Following Log Levels Are available

  • emerg => Emergency: system is unusable
  • alert => Alert: action must be taken immediately
  • crit => Critical: critical conditions
  • error => Error: error conditions
  • warn => Warning: warning conditions
  • notice => Notice: normal but significant condition
  • info => Informational: informational messages
  • debug => Debug: debug messages

cling's People

Watchers

 avatar  avatar  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.