From 8290a04536d6102f9fba94a40b1f3a490fdda576 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Wed, 18 Mar 2026 21:07:12 +0000 Subject: [PATCH] initial commit --- cli.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 9 ++++++++ 3 files changed, 79 insertions(+) create mode 100644 cli.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/cli.go b/cli.go new file mode 100644 index 0000000..acdaf96 --- /dev/null +++ b/cli.go @@ -0,0 +1,65 @@ +package cli + +import ( + "fmt" + "os" + + "path/filepath" + + "git.sr.ht/~sircmpwn/getopt" +) + +var undef = "(undefined)" +var program = undef +var version = undef +var progname = undef + +func Init(prog, ver string) { + program = prog + version = ver + progname = filepath.Base(os.Args[0]) +} + +func Usage(args string) { + fmt.Fprintf(os.Stderr, "usage: %s %s", progname, args) + os.Exit(1) +} + +func Version() { + fmt.Printf("%s version %s\n", program, version) + os.Exit(0) +} + +func Warn(format string, args ...any) { + fmt.Fprintf(os.Stderr, "%s: %s\n", + progname, fmt.Sprintf(format, args...)) +} + +func Die(format string, args ...any) { + Warn(format, args...) + os.Exit(1) +} + +func Fail(err error) { + Die("%s", err.Error()) +} + +func Bool(name string, value bool, usage string) *bool { + return getopt.Bool(name, value, usage) +} + +func Int(name string, value int, usage string) *int { + return getopt.Int(name, value, usage) +} + +func Uint(name string, value uint, usage string) *uint { + return getopt.Uint(name, value, usage) +} + +func String(name, value, usage string) *string { + return getopt.String(name, value, usage) +} + +func Parse() error { + return getopt.Parse() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..167a092 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.baxters.nz/jeremy/cli + +go 1.26.1 + +require git.sr.ht/~sircmpwn/getopt v1.0.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1436d21 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +git.sr.ht/~sircmpwn/getopt v1.0.0 h1:/pRHjO6/OCbBF4puqD98n6xtPEgE//oq5U8NXjP7ROc= +git.sr.ht/~sircmpwn/getopt v1.0.0/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=