arguments

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2020 License: MIT Imports: 7 Imported by: 1

README

arguments

Argument option parser for go.

What is this

I always have to write codes for following operations.

  1. Parse argument options.
  2. Handle following situations.
    • Our required options are not provided.
    • The specified options are invalid to use.

So I aggregate codes to provide above operations to this package.

Install

We can get this package by go get command.

$ go get github.com/mozzzzy/arguments

Usage

We can see an example code in examples directory.

Import

First, we have to import arguments and arguments/argumentOption like this.

import (
  "github.com/mozzzzy/arguments"
  "github.com/mozzzzy/arguments/argumentOption"
)
Add an option rule

To parse command line options, we have to create arguments.Args and then add an option rule by AddOption() method.

var args arguments.Args

opt := argumentOption.Option{
	LongKey:        "long-key",
	ShortKey:       "s",
	ValueType:      "string",
	Description:    "some option.",
}
if err := args.AddOption(opt); err != nil {
	fmt.Println(err.Error())
	return
}

LongKey and ShortKey is keys of the option. For example, above code add a rule for --long-key and -s.
int and string are avairable for ValueType.
Description is the description of the option. This is used in usage message.

If we have to parse multiple options, we can use AddOptions() method instead.

opt1 := argumentOption.Option{
	LongKey:        "string",
	ShortKey:       "s",
	ValueType:      "string",
	Description:    "some option.",
}
opt2 := argumentOption.Option{
	LongKey:        "int",
	ShortKey:       "i",
	ValueType:      "int",
	Description:    "some option.",
}

if err := args.AddOptions([]argumentOption.Option{opt1, opt2}); err != nil {
	fmt.Println(err.Error())
	return
}

Parse options

After adding option rules, we can parse command line options using Parse() method.

if err := args.Parse(); err != nil {
	fmt.Println(err.Error())
	return
}

Print Usage

arguments.Args has String() method.
We can print usage message just by printing arguments.Args.

fmt.Println(args)

Get option's value

To get value of an option, we use GetInt() and GetString() method.
The parameter is the long key or short key.

if val, err := args.GetString("long-key"); err != nil {
	fmt.Println(err.Error())
	fmt.Println(args)
} else {
	fmt.Println(val)
}

Detailed usage

Required option

We can specify required options by adding Required field to argumentOption.Option. If the program is executed without the required option, Parse() method would fail with error.

opt := argumentOption.Option{
	LongKey:        "long-key",
	ShortKey:       "s",
	ValueType:      "int",
	Description:    "some option.",
	Required:       true,
}
Default value

We can also specify default value of an option. To specify default value, we add DefaultValue field to argumentOption.Option.

opt := argumentOption.Option{
	LongKey:        "long-key",
	ShortKey:       "s",
	ValueType:      "int",
	Description:    "some option.",
	DefaultValue:   80,
}
Validator

We often have to validate values of specified options.
This package can validate them easily.
We can specify validator function and its parameter to Validator and ValidatorParam.

opt := argumentOption.Option{
	LongKey:        "long-key",
	ShortKey:       "s",
	ValueType:      "int",
	Description:    "some option.",
	DefaultValue:   80,
	Validator:      validator.ValidateInt,
	ValidatorParam: validator.ParamInt{Min: 0, Max: 100},
}

Validator function should be func (interface{}, interface{}) error.
The first parameter is the argumentOption.Option data. The second parameter is ValidatorParam.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Args

type Args struct {
	Executed string

	Operands []string
	// contains filtered or unexported fields
}

func (*Args) AddOption

func (args *Args) AddOption(opt argumentOption.Option) error

func (*Args) AddOptions

func (args *Args) AddOptions(opts []argumentOption.Option) error

func (Args) Get

func (args Args) Get(key string) (interface{}, error)

func (Args) GetInt

func (args Args) GetInt(key string) (int, error)

func (Args) GetString

func (args Args) GetString(key string) (string, error)

func (Args) IsSet

func (args Args) IsSet(key string) bool

func (*Args) Parse

func (args *Args) Parse() error

func (Args) String

func (arg Args) String() string

func (Args) Validate

func (arg Args) Validate() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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