Git Product home page Git Product logo

go-cmdline-app's Introduction

go cmdline app

Purpose

This package is mean to help us quickly establish an cmdline app with basic functions like

  • run a main function
  • read flags in cmdline input arguments
  • detect what user has input in stdin, and deal with them

How to use

  • just implement interfaces in app/interfaces.go
  • and use main func to run them
  • there is a template of main in main_template.go
  • there is also a echo app example in examples folder
  • and a template of app implementation
import (
	"flag"

	"github.com/haorenfsa/go-cmdline-app/app"
	"github.com/haorenfsa/go-cmdline-app/templates"
)

// main is a template main func
func main() {
	templateAPP := templates.NewAPP()
	templateAPP.SetFlags()
	flag.Parse()
	go templateAPP.Run()
	app.StartDealWithCmdSets(templateAPP)
}
// Flags are the flags given in cmdline
type Flags struct {
	Banner *string
}

// APP is a example implementation of APP interface
type APP struct {
	flags *Flags
}

// NewAPP is the constuctor of APP
func NewAPP() *APP {
	return &APP{
		flags: &Flags{},
	}
}

// SetFlags implements APP.SetFlags
func (a *APP) SetFlags() {
	a.flags.Banner = flag.String("b", "==============Thank you for using cmdline ==============\n", "the starting banner will be print to stdout at begin")
}

// Run implements APP.Run
func (a *APP) Run() {
	fmt.Print(*a.flags.Banner)
}

// RunOnCmdSet implements CmdSetRunner.RunOnCmdSet
func (a *APP) RunOnCmdSet(cmdSet app.CmdSet) {
	if cmdSet[0] == "quit" {
		os.Exit(0)
	}
}

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.