ardanlabs/conf

os.Arg[1:] & Parse errors

millergarym opened this issue · 2 comments

It's not clear from the documentation that Parse should be called with os.Args[1:].
Does seem to matter for the short example

var cfg struct {
	Port int
	Args conf.Args
}

func main() {
	if err := conf.Parse(os.Args, "CRUD", &cfg); err != nil {
		log.Fatalf("main : Parsing Config : %v", err)
	}
}

The bellow example doesn't work with os.Args.
Should conf.Parse(os.Args, .. return an error in this example?

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/ardanlabs/conf"
)

type ip struct {
	Name      string   `conf:"default:localhost,env:IP_NAME_VAR"`
	IP        string   `conf:"default:127.0.0.0"`
	Endpoints []string `conf:"default:127.0.0.1:200;127.0.0.1:829"`
}
type Embed struct {
	Name     string        `conf:"default:bill"`
	Duration time.Duration `conf:"default:1s,flag:e-dur,short:d"`
}
type config struct {
	AnInt   int    `conf:"default:9"`
	AString string `conf:"default:B,short:s"`
	Bool    bool
	Skip    string `conf:"-"`
	IP      ip
	Embed
}

func main() {
	cfg := config{}
	if err := conf.Parse(os.Args[1:], "TEST", &cfg); err != nil { // <- can't be os.Args,
		log.Fatalf("main : Parsing Config : %v", err)
	}
	us, err := conf.Usage("Test", &cfg)
	if err != nil {
		fmt.Printf("err %v\n", err)
	}
	fmt.Printf("%+v\n", us)
	fmt.Printf("%+v\n", cfg)
}

The parse API was written with the idea that the slice contained the command line arguments. os.Args appropriately has the command in the first index position of the slice. In other words, the API is not bound to os.Args or it's format. This has bothered me but I haven't thought of a good solution.

I am still up in the air on this one but since this has been published and is being used in this configuration, I don't think it can be changed at this time.