clix

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: May 8, 2025 License: MIT Imports: 2 Imported by: 4

README

clix

small library meant to turn a command line args into config structs

clix is a helper function for github.com/urfave/cli

Installation

go get github.com/modfin/clix@latest

Usage V2

package main

import (
	"clix"
	"fmt"
	"github.com/urfave/cli/v2"
	"log"
	"os"
)

type Cfg struct {
	Str      string `cli:"a-str"`
	SubInt   SubInt
	SubSlice SubSlice
}
type SubInt struct {
	Int int `cli:"a-int"`
}
type SubSlice struct {
	SubStr   string   `cli:"a-str"`
	StrSlice []string `cli:"slice"`
}


func main() {
	app := &cli.App{
		Name: "test",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "a-str",
				EnvVars: []string{"STR"},
			},
			&cli.StringFlag{
				Name: "a-int",
			},
			&cli.StringSliceFlag{
				Name: "slice",
			},
		},
		Action: func(context *cli.Context) error {

			// Running
			// export STR="Something strange"
			// go run example/example.go --a-int 123 --slice="in the" --slice neighborhood

			// Takes the context and parses it recursively into a struct.
			// Since github.com/urfave/cli/v2 supports environment variables.
			// We can use both command line args and/or environment variables 
			// to parse the input into a struct.
			cfg := clix.Parse[Cfg](context)
			fmt.Printf("%+v", cfg)
			// { Str:Something strange 
			//   SubInt: {Int: 123} 
			//   SubSlice: {SubStr:Something strange StrSlice:[in the neighborhood]}
			// }
			return nil
		},
	}
	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}

Usage V3

package main

import (
	"context"
	"fmt"
	"github.com/modfin/clix"
	cli "github.com/urfave/cli/v3"
	"log"
	"os"
)

type Cfg struct {
	Str      string `cli:"a-str"`
	SubInt   SubInt
	SubSlice SubSlice
}
type SubInt struct {
	Int int `cli:"a-int"`
}
type SubSlice struct {
	SubStr   string   `cli:"a-str"`
	StrSlice []string `cli:"slice"`
}

func main() {
	cmd := &cli.Command{
		Name: "test",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "a-str",
				Sources: cli.EnvVars("STR"),
			},
			&cli.StringFlag{
				Name: "a-int",
			},
			&cli.StringSliceFlag{
				Name: "slice",
			},
		},
		Action: func(ctx context.Context, cmd *cli.Command) error {

			// Running
			// export STR="Something strange"
			// go run example.go --a-int 123 --slice="in the" --slice neighborhood

			// Takes the context and parses it recursively into a struct.
			// Since github.com/urfave/cli/v2 supports environment variables.
			// We can use both command line args and/or environment variables
			// to parse the input into a struct.

			cfg := clix.Parse[Cfg](clix.V3(cmd))
			fmt.Printf("%+v", cfg)

			// { Str:Something strange
			//   SubInt: {Int: 123}
			//   SubSlice: {SubStr:Something strange StrSlice:[in the neighborhood]}
			// }

			return nil
		},
	}
	if err := cmd.Run(context.Background(), os.Args); err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssignValueToCliFields added in v1.0.2

func AssignValueToCliFields(v interface{}, prefix string, c ContextReader)

AssignValueToCliFields recursively assigns values from CLI flags to struct fields. It uses reflection to iterate over the struct fields and set their values based on CLI flags. Parameters:

  • v: a pointer to the struct to populate
  • prefix: prefix for the CLI flags (used for nested structs)
  • c: the CLI context containing the flag values

func Parse

func Parse[A any](c ContextReader) A

Parse converts CLI context into a typed configuration struct. It uses reflection to map CLI flags to struct fields based on struct tags. Usage:

type Config struct {
    Host     string        `cli:"host"`
    Port     int           `cli:"port"`
    Timeout  time.Duration `cli:"timeout"`
    Database struct {
        Name string `cli:"name"`
        User string `cli:"user"`
    } `cli-prefix:"db-"`
}

cfg := clix.Parse[Config](ctx)

func ParseCommand added in v1.1.1

func ParseCommand[A any](cmd CommandReaderV3) A

ParseCommand converts a v3 CommandReaderV3 to a v2 ContextReader and uses the default Parse command, it is just a shorthand for `clix.Parse[Config](clix.V3(cmd))` example

 func(ctx context.Context, cmd *cli.Command) error {
	  config := clix.ParseCommand[Config](cmd)

func ParseContext added in v1.1.1

func ParseContext[A any](ctx ContextReader) A

ParseContext is an alias for `clix.Parse[Config](ctx) to align with the v3 function name`

Types

type CommandReaderV3 added in v1.1.0

type CommandReaderV3 interface {
	String(name string) string
	Int(name string) int
	Uint(name string) uint
	Bool(name string) bool
	Float(name string) float64
	Timestamp(name string) time.Time
	Duration(name string) time.Duration
	StringSlice(name string) []string
	IntSlice(name string) []int
	UintSlice(name string) []uint
	FloatSlice(name string) []float64
}

CommandReaderV3 is the interface that a v3 cli.Command must support in order to be convertible to a v2 ContextReader and the use of clix.Parse[]()

type ContextReader added in v1.0.2

type ContextReader interface {
	String(name string) string
	Int(name string) int
	Int64(name string) int64
	Uint(name string) uint
	Uint64(name string) uint64
	Bool(name string) bool
	Float64(name string) float64
	Timestamp(name string) *time.Time
	Duration(name string) time.Duration
	StringSlice(name string) []string
	IntSlice(name string) []int
	Int64Slice(name string) []int64
	UintSlice(name string) []uint
	Uint64Slice(name string) []uint64
	Float64Slice(name string) []float64
}

ContextReader defines the interface for reading values from a CLI context. This abstraction allows for easier testing by allowing mock implementations.

func V3 added in v1.1.0

V3 converts a v3 CommandReaderV3 to a v2 ContextReader example

 func(ctx context.Context, cmd *cli.Command) error {
	  config := clix.Parse[Config](clix.V3(cmd))

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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