GithubHelp home page GithubHelp logo

comphlete's Introduction

comphlete

Packagist Version Build Status

Dynamic bash completion from PHP

Why?

Say you have a cli script to read some data, using comphlete you can autocomplete data identifiers directly from the command line. It's both fun and powerful. And works well with symfony console apps.

Installation

composer require hanneskod/comphlete

Usage

With symfony apps

Create an empty symfony console application (here named myapp.php).

$application = new \Symfony\Component\Console\Application();

$application->add(new \hanneskod\comphlete\Symfony\ComphleteCommand);

$application->run();

This creates a hidden command named _complete that handles autocompletion.

To register autocompletion in your enviroment use (in .bashrc)

source $(myapp.php _complete --generate-bash-script --app-name=myapp.php)

NOTE that the ComphleteCommand does not work for single command applications. If your application is a single command app you'll have to revert to the default way of createing suggestions. See below.

The (not so) hard way

Create your autocomplete definition in a php script (here named test.php).

namespace hanneskod\comphlete;

$definition = (new Definition)
    // first argument with a fixed set of suggestions
    ->addArgument(0, ['foo', 'bar', 'baz'])

    // second argument with a dynamic callback
    ->addArgument(1, function () {
        // load suggestions from database...
        return ['aa', 'bb'];
    })

    // simple option
    ->addOption('foo')

    // option with suggested values
    ->addOption('bar', ['val1', 'val2'])
;

$completer = new Completer($definition);

$input = (new InputFactory)->createFromArgv($argv);

echo Helper::dump($completer->complete($input));

To load into you environment create a bash script (note that this requires test.php to be in you PATH to work properly).

php bash_load_script_template.php test.php > load.sh

And source it (in .bashrc)

source load.sh

Using contexts

A common design pattern is to have an application define a number of commands with their own sets of arguments and options. Comphlete supports this by the use of contexts. Here is an app with an import and an export command.

$import = (new ContextDefinition('import'))
    ->addArgument(1, ['some-argument'])
;

$export = (new ContextDefinition('export'))
    ->addArgument(1, ['another-argument'])
;

$def = (new ContextContainerDefinition)
    ->addContext($import)
    ->addContext($export)
;

$completer = new Completer($def);

$input = (new InputFactory)->createFromArgv($argv);

echo Helper::dump($completer->complete($input));

comphlete's People

Contributors

hanneskod avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

comphlete's Issues

Fails to complete namespaced arguments

Due to the handling of special characters. Fix:

  1. Pass the word being replaced to the complete script.
  2. Evaluate this word before sending suggestions:
    • If word is empty send as normal
    • If word matches the node being replaced send as normal
    • If not try to trim from the start of suggestions so that all suggestions start with word

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.