Giter VIP home page Giter VIP logo

cmdmux's Introduction

cmdmux

Package cmdmux implements a command parser and router for terminal program.

Build Status GoDoc

TODO

  • Build the handle routes from help nodes so that when adding a new command, just add a help node and no need to touch other parts.

Overview

In general, there are two styles a terminal program to interact with users.

  1. Use -o, -p to specify parameters. Most programmes are in this way.
  2. Use sub-commands, like git which uses only one level sub-command.

The first way can be implemented by the flag package of Golang, and the second this package.

The package can:

  1. Build a terminal program with various commands easily.
  2. Generate a shell completion file (Now only for bash) !
  3. Output commands tree.

Usage

Build command line

A simple example:

package main

import (
	"fmt"
	"os"

	"github.com/choueric/cmdmux"
)

type Options struct {
	arch string
}

func rootHandler(args []string, data interface{}) (int, error) {
	fmt.Println("Usage:")
	cmdmux.PrintTree(os.Stderr)
	return 0, nil
}

func buildHandler(args []string, data interface{}) (int, error) {
	opt := data.(*Options)
	fmt.Printf("invoke 'build' of %s\n", opt.arch)
	return 1, nil
}

func buildKernelHandler(args []string, data interface{}) (int, error) {
	fmt.Printf("invoke 'build kernel', args = %v\n", args)
	return 2, nil
}

func main() {
	opt := &Options{arch: "arm"}

	cmdmux.HandleFunc("/", rootHandler)
	cmdmux.HandleFunc("/build", buildHandler)
	cmdmux.HandleFunc("/build/kernel", buildKernelHandler)
	cmdmux.HandleFunc("/build/kernel/image", buildKernelHandler)
	cmdmux.HandleFunc("/build/uboot", buildKernelHandler)

	cmdmux.Execute(opt)
}

The package uses HandleFunc() to add handler for specific sub-command, like the package http. The sub-command is represented by a command-path, like "build", "build/kernel".

After adding handlers, invoke Execute() to parse the command line, route to the correct handler and execute it.

The only one parameter of Execute() is passed to the parameter data of the handler function. The parameter args of handler function is the rest part of command line stripped off the command-path part.

The results of this example are like:

$ test build
invoke 'build' of arm

$ test build kernel optoins one
invoke 'build kernel', args = [options one]

$ test cmd
Usage:
/*
└── build*
    ├── kernel*
    │   └── image*
    └── uboot*

Print Commands Tree

In the above example, the root handler rootHandler() invoke PrintTree() to output the commands tree. If one node has handler, it is appended a '*' symbol after its command name.

Generate Shell Completion File

After building various commands with HandleFunc(), it's time to get a shell (bash actually) completion file which helps users of your program input commands easily on terminal.

Below is a example to get such file for test program:

// some HandleFunc codes ...

file, err := os.Create("test-completion")
if err != nil {
	fmt.Println(err)
	return
}
defer file.Close()

if err = cmdmux.GenerateCompletion("test", file); err != nil {
	fmt.Println(err)
}

Then apply this file:

$ source ./test-completion

License

See the LICENSE file.

cmdmux's People

Contributors

choueric avatar

Stargazers

 avatar

Watchers

 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.