cosiner / flag Goto Github PK
View Code? Open in Web Editor NEWFlag is a simple but powerful command line option parsing library for Go support infinite level subcommand
License: MIT License
Flag is a simple but powerful command line option parsing library for Go support infinite level subcommand
License: MIT License
Given:
type flags struct {
Something []string `names:"-s" usage:"pass in something(s)"`
Rest []string `args:"true"`
}
how does the user end the greedy consumption of -s
and provide non-flag arguments? E.g., a common pattern in flag parsing libraries requires multi-value argument flags to be specified once for each argument:
cmd -s A -s B -s C XYZ
would produce len(a.Something) == 3
and a.Rest == []string{"XYZ"}
. If multi-value arguments are greedy, len(a.Something) == 4
and a.Rest == []string{}
.
How does a user pass in arguments to Rest
with greedy consumption?
It looks like the 3rd nested structs aren't visible. Example:
type Arguments struct {
Index struct {
Enable bool
Full struct {
Enable bool
// This is not visible
List struct {
Enable bool
}
// This is not visible as well
Syncto struct {
Enable bool
Cassandra struct {
Enable bool
All bool `names:"--all" default:"true" desc:"Just sync everything from Redis to Cassandra"`
}
}
}
}
}
This is the error I got:
go run -race main.go index full list
StandaloneValue: standalone value without flag: [main index full] list
For example:
command help set
If I've understood the library, is not possible to use this syntax
This should work: go run main.go logs --from "-1d" /var/log/messages
But it doesn't. Here's the error log:
StandaloneFlag: standalone flag without values: [main logs].--from
'-' to ensure next argument must be a flag
That seems to imply that implied flags will work here the same as they do with tar
:
tar -xvf test.tar.gz
tar xvf test.tar.gz
Is that true?
Hi,
Maybe I missed something, but I couldn't find a way to declare mandatory settings.
It would be great to have such feature implemented.
Regards
I must be missing something simple here, let's say I have subcommands build
and test
.
How do I know if user ran build
instead of test
?
Hi, I'm not sure how it compiles, but your example refers to flag.Flag
as simply Flag
, which was confusing on the first read. Otherwise, looks like a neat API and is currently on my shortlist for consideration :)
Is a good idea add a tag for setting postitional arguments. For example:
type Args struct {
Version string `names:"-v" usage:"Version"`
Pos string `names:"positional" positional:"true" usage"positional flag"`
}
Result would be:
Usage:
cmd [FLAG] [positional]...
Flags:
-v Version
-h, --help show help
Positional flags:
positional positional flag
Works here: d92f27a
Stop working here: e5854e4
Example config struct:
type Arguments struct {
Config string `names:"-c" default:"." desc:"Path to config directory"`
Index struct {
Enable bool
Full struct {
Enable bool
List struct {
Enable bool
Unsynced bool `names:"--unsynced" default:"false" desc:"Display only unsynced metric indices"`
}
Syncto struct {
Enable bool
Storage struct {
Enable bool
Unsynced bool `names:"--unsynced" default:"false" desc:"Sync only unsynced metric indices"`
}
}
}
Short struct {
Enable bool
List struct {
Enable bool
Unsynced bool `names:"--unsynced" default:"false" desc:"Display only unsynced metric indices"`
}
Syncto struct {
Enable bool
Storage struct {
Enable bool
Unsynced bool `names:"--unsynced" default:"false" desc:"Sync only unsynced metric indices"`
}
}
}
}
}
Error:
go run main.go index short syncto storage
StandaloneValue: standalone value without flag: [main index short syncto] storage
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.