argv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2024 License: MIT Imports: 6 Imported by: 0

README

Argv

go-version go-build go-reportcard

Parse string slices into structs

Usage scenario

argv attempts to parse a string slice, composed of parameters and values, and fill a struct based on its annotation information and converting values to the specific field type.

The typical use case is to convert an arbitrary list of arguments and values to a well-defined struct:

Sample main.go:

package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/oddbit-project/argv"
	"os"
	"strings"
)

type CertInfo struct {
	CommonName         string `argv:"CN"`
	OrganizationalUnit string `argv:"OU"`
	Organization       string `argv:"O,optional"` // optional parameter
	Algorithm          string `argv:"alg"`
	KeyLen             uint   `argv:"bits"`
	Days               uint32 `argv:"days"`
}

func main() {
	if len(os.Args) < 2 {
		fmt.Printf("Usage: %s <command> [options]\n", os.Args[0])
		os.Exit(0)
	}
	cmd := os.Args[1]

	switch cmd {
	case "gencert":
		record := &CertInfo{}

		// attempt to parse & serialize values from command line
		err := argv.ParseArgv(record, os.Args[2:])
		if err == nil {
			str, _ := json.Marshal(record)
			fmt.Printf("Parsed parameters:\n %s\n", string(str))
			os.Exit(0)
		}

		// empty arg list, show available names
		if errors.Is(err, argv.ErrEmptyArgs) {
			names, err := argv.ParseNames(record)
			if err != nil {
				fmt.Println(err)
				os.Exit(-1)
			}
			fmt.Println("Available args: ", strings.Join(names, ", "))
			os.Exit(0)
		}

		fmt.Println(err)
		os.Exit(-1)
	default:
		fmt.Printf("'%s' is not a supported command\n", cmd)
		os.Exit(-1)
	}
}

Running:

$ go run main.go gencert
empty argument list
exit status 255

$ go run main.go gencert 
Available args:  CN, OU, O,optional, alg, bits, days

$ go run main.go gencert -CN "certificate name" -OU "SomeOrg"
value for arg 'alg' is missing
exit status 255

$  go run main.go gencert -CN "certificate name" -OU "SomeOrg" -alg "rsa" -bits 4096 -days 365
Parsed parameters:
 {"CommonName":"certificate name","OrganizationalUnit":"SomeOrg","Organization":"","Algorithm":"rsa","KeyLen":4096,"Days":365}

Supported field types

type description
int Int value
int8 Int8 value
int32 Int32 value
int64 Int64 value
uint Uit value
uint8 Uint8 value
uint32 Uint32 value
uint64 Uint64 value
float32 float32 value; supports scientific notation
float64 float64 value; supports scientific notation
time.Time RFC3339 time string
bool true/false or 1/0 string
string arbitrary string
[]string list of strings, such as "value1,value2"

Documentation

Index

Constants

View Source
const (
	// global errors
	ErrEmptyArgs             = utils.Error("empty argument list")
	ErrInvalidDest           = utils.Error("dest must be a ptr")
	ErrInvalidDestType       = utils.Error("invalid argument type; dest must be a struct")
	ErrInvalidParameterCount = utils.Error("invalid parameter count")

	// field error types
	ErrTypeReadOnly     = 1
	ErrTypeMissingValue = 2
	ErrTypeInvalidValue = 3
	ErrTypeNotSupported = 4
)

Variables

This section is empty.

Functions

func AddParser

func AddParser(in string, fn FieldParser)

add a custom field parser

func AddReservedType

func AddReservedType(t string)

func ParseArgv

func ParseArgv(dest any, argv []string) error

func ParseNames

func ParseNames(dest any) ([]string, error)

Types

type FieldError

type FieldError struct {
	FieldName  string
	ErrorType  int
	FieldError error
}

field validation errors

func ErrInvalidValue

func ErrInvalidValue(fieldName string, fieldError error) FieldError

func ErrMissingValue

func ErrMissingValue(fieldName string) FieldError

func ErrNotSupported

func ErrNotSupported(fieldName string) FieldError

func ErrReadOnly

func ErrReadOnly(fieldName string) FieldError

func (FieldError) Error

func (e FieldError) Error() string

type FieldParser

type FieldParser func(in string) (any, error)

field mapper

Directories

Path Synopsis
examples
nested command
simple command

Jump to

Keyboard shortcuts

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