app

package module
v0.0.0-...-d01c3e8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2021 License: GPL-3.0 Imports: 3 Imported by: 0

README

app

Go Reference

Simple cli applications runner.

Usage

Commands must implements app.Command interface:

type exampleCommand struct {
	test string
}

func (c *exampleCommand) Name() string {
	return "example"
}

func (c *exampleCommand) Usage() string {
	return "just example command"
}

func (c *exampleCommand) Flags() *flag.FlagSet {
	fs := flag.NewFlagSet(c.Name(), flag.ExitOnError)
	fs.StringVar(&c.test, "test", "value", "test flag")
	return fs
}

func (c *exampleCommand) Run(args []string) error {
	fmt.Printf("Runned %s command with flag -test=%s and args: %v", c.Name(), c.test, args)
	return nil
}

Main file example:

package main

import "github.com/neonxp/app"

func main() {
    app := app.New([]app.Command{
        exampleCommand{}, // <- Register command
    })
	if err := app.Start(os.Args); err != nil { // <- Run!
		fmt.Println(err)
		os.Exit(1)
	}
}

That's all!

Full example

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoCommand = fmt.Errorf("No command provided")

Functions

This section is empty.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

func New

func New(commands []Command) *App

New returns new App.

func (*App) Start

func (a *App) Start(args []string) error

Start cli application

type Command

type Command interface {
	// Name returns name of command
	Name() string
	// Usage returns short help to command
	Usage() string
	// Flags returns FlagSet with flags for current command
	Flags() *flag.FlagSet
	// Run command
	Run(args []string) error
}

Command represents top level command of cli application

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL