bobziuchkovski / writ Goto Github PK
View Code? Open in Web Editor NEWA flexible command and option parser for Go
License: MIT License
A flexible command and option parser for Go
License: MIT License
E.g. if -a
and -b
are both specified then the last one specified will be used.
A workaround might be adding --mode
option and specify --mode=a
or --mode=b
.
I'd propose to extend/change the OptionDecoder
interface, where both option name and value are passed to the Decode
function.
Is it possible to get the filenames for io.Reader and io.Writer options? I like the automatic handling of them, but still prefer to sometimes get to the filenames (for error messages and the like)
Setting a placeholder:"<some-thing>" as a struct tag for types other than
string` causes the option parsing to silently fail and omit that option:
type Config struct {
# Included
Start int64 `option:"start-time" description:"The timestamp (in Unix seconds) to request logs from."`
# Omitted (i.e. not parsed, not included in help text)
End int64 `option:"end-time" placeholder="<unix-timestamp>" description:"The timestamp (in Unix seconds) to request logs to."`
# Included Count int `option:"c, count" default:"1" description:"The number (count) of logs to retrieve."`
}
Is this intended? Can be useful to describe formatting.
$ cat test.go
package main
import (
"os"
"github.com/bobziuchkovski/writ"
)
type Foo struct {
Bar Bar `command:"bar"`
}
type Bar struct {
HelpFlag bool `flag:"h, help"`
}
func main() {
foo := &Foo{}
cmd := writ.New("foo", foo)
path, _, err := cmd.Decode(os.Args[1:])
if err != nil {
path.Last().ExitHelp(err)
}
}
$ go run test.go bar --help --help
Usage: foo bar [OPTION]... [ARG]...
Error: option "bar" specified too many times
exit status 1
I believe the error message should be option "help" specified too many times
Has an Option.Default
field been considered? I prefer to write options without struct tags (since struct tags aren't checked by the compiler, leading to my issue yesterday), but right now you need to write the following:
{
Names: []string{"count"},
Description: "The (maximum) number of logs to request.",
Placeholder: "<num>",
Decoder: writ.NewDefaulter(writ.NewOptionDecoder(config.Count), "1"),
},
This isn't bad, but it:
int64
or other time (say, for Unix timestamps) then you can't set a default.Default
field would be more explicit and would better map against the existing struct tag names.This allows you to have a common set of options/flags across sub-commands - e.g.
type Common struct {
FileName string
Headers string
...
}
type Fetch struct {
Common
From string
}
type Restore struct {
Common
To []string
}
The structs here are simple, but should illustrate the use-case. The embedded struct does not seem to be parsed.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.