Argument option parser for go.
I always had to write codes for following operations.
- Parse argument options.
- 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.
We can get this package by go get
command.
$ go get github.com/mozzzzy/arguments/v2
Or if we use go.mod
, we can write like following snippet.
module <your module name>
go 1.13
require(
github.com/mozzzzy/arguments/v2 v2.0.1
)
And then execute go get
.
$ go get
We can see an example code in examples directory.
First, we have to import some modules.
import (
"github.com/mozzzzy/arguments/v2"
"github.com/mozzzzy/arguments/v2/argumentOption" // if you parse options (like -h --help)
"github.com/mozzzzy/arguments/v2/argumentOperand" // if you parse operands
)
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 available for ValueType
.
If we have to parse option that doesn't have value (like --help),
we should skip configuring ValueType
field.
var args arguments.Args
opt := argumentOption.Option{
LongKey: "help",
ShortKey: "h",
Description: "show help message and exit.",
}
if err := args.AddOption(opt); err != nil {
fmt.Println(err.Error())
return
}
Description
is the description of the option. This is used in usage message.
Required: true
specifies the option is required.
If required option is not set, args.Parse()
method returns error.
DefaultValue
specifies the default value of the option.
If the option is not specified, the default value is used.
We often have to validate option values.
We can validate them easily.
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
.
we can add multiple option rules at once by AddOptions()
method.
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
}
After adding option rules, we can parse command line options using Parse()
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
}
if err := args.Parse(); err != nil {
fmt.Println(err.Error())
return
}
To get value of parsed options, we use GetIntOpt()
GetStringOpt()
and GetOpt()
method.
The parameter is the long key or short key.
// GetStringOpt()
if val, err := args.GetStringOpt("long-key"); err != nil {
fmt.Println(err.Error())
fmt.Println(args)
} else {
fmt.Println(val)
}
// GetIntOpt()
if val, err := args.GetIntOpt("long-key"); err != nil {
fmt.Println(err.Error())
fmt.Println(args)
} else {
fmt.Println(val)
}
// GetOpt()
if val, err := args.GetOpt("long-key"); err != nil {
fmt.Println(err.Error())
fmt.Println(args)
} else {
valInt, ok := val.(int)
if ok {
fmt.Println(valInt)
} else {
fmt.Println("value of --long-key is not integer.")
}
}
To check an option is set, we use OptIsSet()
method.
if args.OptIsSet("--help") {
fmt.Println("--help -h option is specified.")
}
arguments.Args
has String()
method.
We can print usage message just by printing arguments.Args
.
fmt.Println(args)
We can handle operands by almost the same way with options.
The differences are following points.
- Use
argumentOperand.Operand
instead ofargumentOption.Option
- In
argumentOperand.Operand
, useKey
instead ofLongKey
andShortKey
- Add operand rule using
AddOperand()
andAddOperands()
methods. - Get operand value using
GetStringOperand()
GetIntOperand()
andGetOperand()
- To check an operand is set, use
OperandIsSet()
instead ofOptionIsSet()
Following code is an example.
ope1 := argumentOperand.Operand{
Key: "operand1",
ValueType: "string",
Description: "some operand",
Required: true,
}
ope2 := argumentOperand.Operand{
Key: "operand2",
ValueType: "int",
Description: "some operand",
}
ope3 := argumentOperand.Operand{
Key: "operand3",
ValueType: "int",
Description: "some operand",
}
if err := args.AddOperand(ope1); err != nil {
fmt.Println(err.Error())
return
}
if err := args.AddOperands([]argumentOperand.Operand {ope2, ope3}); err != nil {
fmt.Println(err.Error())
return
}
if err := args.Parse(); err != nil {
fmt.Println(err.Error())
fmt.Println(args)
return
}
if str2, err := args.GetStringOperand("operand1"); err != nil {
fmt.Println(err.Error())
fmt.Println(args)
return
} else {
fmt.Println(str2)
}
if integer2, err := args.GetIntOperand("operand2"); err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(integer2)
}
if ope3, err := args.GetOperand("operand3"); err != nil {
fmt.Println(err.Error())
} else {
integer3, ok := ope3.(int)
if ok {
fmt.Println(integer3)
} else {
fmt.Println("value of operand3 is not integer.")
}
}