subflag

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2021 License: MIT Imports: 4 Imported by: 5

README

subflag

subflag is a subcommand library for flag package.

Installation

go get github.com/orisano/subflag

How to use

package main

import (
    "flag"
    "fmt"
    "log"
    "os"
    
    "github.com/orisano/subflag"
)

func main() {
    if err := subflag.SubCommand(os.Args[1:], []subflag.Command{&EchoCommand{}}); err != nil {
        log.Fatal(err)
    }
}

type EchoCommand struct {
    text string
}

func (c *EchoCommand) FlagSet() *flag.FlagSet {
    flagSet := flag.NewFlagSet("echo", flag.ExitOnError)
    flagSet.StringVar(&c.text, "t", c.text, "echo text (required)")
    return flagSet
}

func (c *EchoCommand) Run(args []string) error {
    if len(c.text) == 0 {
        return flag.ErrHelp
    }
    fmt.Println(c.text)
    return nil
}

Author

Nao Yonashiro (@orisano)

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidArguments = errors.New("invalid arguments (deprecated)")

ErrInvalidArguments is ... Deprecated: instead of use flag.ErrHelp

Functions

func SubCommand

func SubCommand(args []string, commands []Command) error

Types

type Command

type Command interface {
	FlagSet() *flag.FlagSet
	Run(args []string) error
}

Jump to

Keyboard shortcuts

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