Coder Social home page Coder Social logo

command's Introduction

command

command is a simple Go package for implementing a CLI.

Example usage:

package main

import (
	"github.com/SlyMarbo/command"
	"os"
)

func main() {
	scanner := command.NewScanner(os.Stdin, true) // Read from stdin.
	
	for scanner.Scan() {
		token := scanner.Token()
		switch {
			
			// Check for an empty token.
			case token.Blank():
				// ...
			
			// Compare against one string.
			case token.Equals("example"):
				// ...
				
			// Compare against multiple strings.
			case token.Equals("more", "other"):
				// ...
				
			// Check for a prefix.
			case token.HasPrefix("start"):
				token.String() // Full string, starting with "start".
				token.Body()   // Modified string, starting after "start".
				
			// HasSuffix works identically.
		}
	}
	
	if err := scanner.Err(); err != nil {
		// handle error.
	}
}

If the entered command was "stop pid 1234", then the command package could be used as follows:

t := scanner.Token()
switch {
	
	case token.HasPrefix("stop", "kill"):
		if token.HasPrefix("pid") {
			pid, err := token.Int(0, 0) // Automatic base, type int.
			
			// ...
		}
	
	// ...
}

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.