gocommander

package module
v0.0.0-...-8125d25 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2018 License: MIT Imports: 12 Imported by: 0

README

Package gocommander provides a command line interface making your tool developments easier and happier.

Build Status Report Status

Copyright 2018- Tatsuhiro Aoshima (hiro4bbh@gmail.com).

Introduction

Package gocommander provides command line interface making your tool developments easier and happier.

See gocommander's document on GoDoc.

Documentation

Overview

Package gocommander provides a command line interface making your tool developments easier and happier.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultName is the default value of CommanderSettings.Name.
	DefaultName = "An go-commander application"
	// DefaultCopyright is the default value of CommanderSettings.Copyright.
	DefaultCopyright = "Copyright 2018- Tatsuhiro Aoshima (hiro4bbh@gmail.com)."
)

Functions

func CreateFile

func CreateFile(name FilePath) (*os.File, error)

CreateFile creates the parent directories if needed, creates a new file, and returns the *os.File.

This function returns an error in file operations.

func DecompressFile

func DecompressFile(file *os.File, cache bool, logger *golog.Logger) (*os.File, error)

DecompressFile returns a decompressed file. This function automatically closes the given file if returning the decompressed file. If the file does not exist or cache is false, then this function decompress the file. Currently, this function supports gzip (extension ".gz").

This function returns an error in decompression.

func DownloadAndDecompressFile

func DownloadAndDecompressFile(dirpath FilePath, rawurl string, cache bool, logger *golog.Logger) (*os.File, error)

DownloadAndDecompressFile is DownloadFile followed by DecompressFile.

This function returns an error by DownloadFile or DecompressFile.

func DownloadFile

func DownloadFile(dirpath FilePath, rawurl string, cache bool, logger *golog.Logger) (*os.File, error)

DownloadFile returns a downloaded file. If the file does not exist or cache is false, then this function downloads the file.

This function returns an error in downloading.

Types

type BoxString

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

BoxString represents the states BoxString(str) or BoxString(none).

func Env

func Env(name string) BoxString

Env returns the environment variable with the given name.

func NewBoxString

func NewBoxString(o interface{}) BoxString

NewBoxString returns a new BoxString.

func (BoxString) String

func (box BoxString) String() string

String returns the string representation.

func (BoxString) Unwrap

func (box BoxString) Unwrap() (string, error)

Unwrap returns the string if ok, otherwise returns an error.

func (BoxString) UnwrapFilePath

func (box BoxString) UnwrapFilePath() (FilePath, error)

UnwrapFilePath returns the FilePath string if ok, otherwise returns an error.

func (BoxString) UnwrapFilePathOr

func (box BoxString) UnwrapFilePathOr(defval FilePath) FilePath

UnwrapFilePathOr returns the FilePath string if ok, otherwise returns defval.

func (BoxString) UnwrapOr

func (box BoxString) UnwrapOr(defval string) string

UnwrapOr returns the string if ok, otherwise returns defval.

type Command

type Command interface {
	// Description returns the command description.
	Description() string
	// Init is called, and initializes the command options at the command added to a commander.
	Init(ctx *Context)
	// Run is called on the command run by a commander.
	//
	// This function can return an error in running.
	Run(ctx *Context) error
}

Command is the command interface.

type Commander

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

Commander is a manager of commands.

func New

func New(settings *Settings) *Commander

New returns a new Commander with the given CommanderSettings. If settings is nil, then the fields are filled with the default values. If a field of settings is zero value, then the field is filled with the corresponding default value.

func (*Commander) Add

func (commander *Commander) Add(name string, cmd Command) *Context

Add adds a new command with the given command name and command handlers, and returns its Context.

This function calls panic if the given command name is used or the given command name is illegal. The illegal command names are "h", "help", or one ending with "+" or "-" or "=".

func (*Commander) Copyright

func (commander *Commander) Copyright() string

Copyright returns the commander's copyright.

func (*Commander) Get

func (commander *Commander) Get(name string) *Context

Get returns the Context with the given command name.

func (*Commander) Help

func (commander *Commander) Help()

Help writes the help message to the writer of the commander's logger.

func (*Commander) Logger

func (commander *Commander) Logger() *golog.Logger

Logger returns the commander's logger.

func (*Commander) Name

func (commander *Commander) Name() string

Name returns the commander's name.

func (*Commander) Parse

func (commander *Commander) Parse(args []string) (int, error)

Parse parses the given command line arguments, and returns the next argument index.

This function returns an error in parsing.

func (*Commander) Reset

func (commander *Commander) Reset()

Reset resets the commander and its command states.

func (*Commander) Run

func (commander *Commander) Run() error

Run runs the commands in order.

This function stops the execution as soon as a command returns and error, then returns it.

type Context

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

Context is the command instance with a ContextHandlers.

func (*Context) AddOption

func (ctx *Context) AddOption(name string, opt Option, description string)

AddOption adds the given opt with the given name.

This function calls panic if the given name is already used or the given name is illegal. The illegal name are "h" or "help".

func (*Context) GetOption

func (ctx *Context) GetOption(name string) Option

GetOption returns the Option with the given option name.

func (*Context) Help

func (ctx *Context) Help(cmdname string)

Help writes the help message to the writer of the commander's logger.

func (*Context) Logger

func (ctx *Context) Logger() *golog.Logger

Logger returns the command's logger.

func (*Context) OptionsString

func (ctx *Context) OptionsString() string

OptionsString returns the string representation of options.

func (*Context) Parse

func (ctx *Context) Parse(args []string) (int, error)

Parse parses the given command line arguments, and returns the next argument index.

This function returns an error in parsing.

type FilePath

type FilePath string

FilePath is the string with filepath methods.

func HomeDir

func HomeDir() FilePath

HomeDir returns the user's home directory path.

This function calls panic in getting the path.

func (FilePath) Base

func (p FilePath) Base() FilePath

Base is filepath.Base(p).

func (FilePath) Dir

func (p FilePath) Dir() FilePath

Dir is filepath.Dir(p).

func (FilePath) Ext

func (p FilePath) Ext() string

Ext is filepath.Ext(p).

func (FilePath) Join

func (p FilePath) Join(q FilePath) FilePath

Join returns the joined FilePath.

type Option

type Option interface {
	// Set sets the value parsed from the given str, and returns error if occurred.
	// str has the form "", "+", "-", or "=$VALUE".
	Set(str string) error
	// String returns the string representation of the option value.
	String() string
	// ValueFormat returns the option value format.
	ValueFormat() string
}

Option is the interface for option values.

type OptionBool

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

OptionBool is Option having a bool variable.

func NewOptionBool

func NewOptionBool(value bool) *OptionBool

NewOptionBool returns a new OptionBool with the given default value.

func (*OptionBool) Get

func (opt *OptionBool) Get() bool

Get returns the value.

func (*OptionBool) Set

func (opt *OptionBool) Set(str string) error

Set is for interface Option.

func (*OptionBool) String

func (opt *OptionBool) String() string

String is for interface Option.

func (*OptionBool) ValueFormat

func (opt *OptionBool) ValueFormat() string

ValueFormat is for interface Option.

type OptionString

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

OptionString is Option having a string variable.

func NewOptionString

func NewOptionString(value string) *OptionString

NewOptionString returns a new OptionString with the given default value.

func (*OptionString) Get

func (opt *OptionString) Get() string

Get returns the value.

func (*OptionString) Set

func (opt *OptionString) Set(str string) error

Set is for interface Option.

func (*OptionString) String

func (opt *OptionString) String() string

String is for interface Option.

func (*OptionString) ValueFormat

func (opt *OptionString) ValueFormat() string

ValueFormat is for interface Option.

type Settings

type Settings struct {
	// Name is the commander's name.
	Name string
	// Copyright is the commander's copyright.
	Copyright string
	// Logger is the commander's logger.
	Logger *golog.Logger
}

Settings has commander settings.

Jump to

Keyboard shortcuts

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