ogier / pflag Goto Github PK
View Code? Open in Web Editor NEWDrop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
License: BSD 3-Clause "New" or "Revised" License
Drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
License: BSD 3-Clause "New" or "Revised" License
Hello,
I'm wondering if it's possible to change source of flags. For the moment you seems to use os.Args to parse flags. Is it possible to change this source like giving an array ?
Thanks :)
consider a command foo --bar --baz
where --bar
is defined, but --baz
isn't.
pflag prints unknown flag: --bar
even though the actual offending flag is --baz
I'm probably missing something obvious, but how do I use pflag so that the only valid option is the short variant? That is, if I want just "-h" rather than something like "-h" and "--help"?
If I use:
flag.BoolVar(&help, "h", false, "Give help")
then Parse() requires --h
If I use:
flag.BoolVarP(&help, "", "h", false, "Give help")
then Parse() requires "-h" and the odd "--" whereas I was hoping an empty long-opt would have deactivated that variant.
I'm pretty sure that Posix/GNU do not insist on both variants of an option, but I've been wrong before.
Most cli tools I use have a space character to delimit the flag and it's option, and it would be cool to be able to do so for pflag. I realize this would be yet another departure from the flag
stdlib, but it would definitely be cool to have.
I tried just changing https://github.com/ogier/pflag/blob/master/flag.go#L429
to a space character, but it seems I'll also need to change something in FlagSet
, just not sure where. Would be happy to make a PR adding another method that configures this and make the requisite changes given some direction :)
I'm having some trouble passing an element of a map to stringtostring, where the value may or may not contain equal signs, quotes, and/or commas. It seems there is no normative passing, quoting, escaping mechanism.
The documentation says the items won't be split on commas, but if they contain an equal sign, they are. Then to have the commas and quotes preserved, they must be quoted: but this won't be correct if they don't contain an equal sign, no?
First of all, thanks for a nice software!
I tried to create a subcommand example but I couldn't make it work as expected.
https://gist.github.com/hnakamur/3c8fba54b792536a196c
With Go's standard flag package, my example works as expected.
https://gist.github.com/hnakamur/2512fd35b8e0b41d2c08
So, could you add a subcommand example?
Thanks!
Does this library support passing multiple flags of the same key?
e.g. --something=... --something=... --something=...
parsed to [...,...,...]
It would be great if pflag
could accept case-insensitive long flag names, perhaps as an option, so that it would treat variations like --baseUrl
, --baseURL
and --baseurl
as one and the same.
A discussion that led to this feature request is available at gohugoio/hugo/issues/959
Many thanks!
Currently only flag.Bool
can be used as --flag
without supplying a value. There's some situation when non-bool flag should be able to use default value when no value was supplied.
E.g. the ls
command in coreutils
have a --color
string flag. When there's no value supplied, it assume to be always
. Right now there's no way to do the same using pflag
var files = flag.StringSlice("files", "", "")
for _, file := range *files {
fmt.Println(file)
}
./p --files a b c -b xxx
which prints:
a
b
c
#now if I got three files
$ ls *.txt
a.txt b.txt c.txt
./p --files *.txt -b xxx
#shall print:
a.txt
b.txt
c.txt
Well..what's different from standard library?
Is there any functionality to parse ranges as flags. For example the namp tool takes a range of ports to scan nmap HOST -p 10-100
This would scan ports in the range 10 to 100. Similar functionalities being implemented would be helpful!
This is important for packages (like https://github.com/golang/glog) where the flags are embedded deeply into the code.
I already have this supported in kubernetes here that we can borrow/steal:
https://github.com/GoogleCloudPlatform/kubernetes/blob/master/pkg/util/plog_import.go
(I typo'd the name of that file and I'll fix it up soon).
This would be a lot easier if the pflag.Value.Type
didn't exist or was an extended optional interface. It is unclear why that is there and where it is used.
Please use semantic versioning:
If I've got multiple flags, say, like this:
--user="something"
--language="blah"
--starCount=2
....
...
...
then how to check which flag has been given input in the cli? Because different flag has different functionality.
http://godoc.org/github.com/ogier/pflag
Quicker than starting a godoc http server.
I register two flags, then look them up. The short-hand and descriptions are not correct. They are swapped around with each other. Unless there's user error involved.
INFO[0000] "tenant-id" "azure subscription id" *pflag.Flag
INFO[0000] "subscription-id" "azure tenant id" *pflag.Flag
https://gist.github.com/anonymous/94dccf42b133a425f41e:
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
)
func main() {
if err := NewRootCmd().Execute(); err != nil {
log.Fatalln(err)
}
}
type Args struct {
TenantID string
SubscriptionID string
}
var args Args
func NewRootCmd() *cobra.Command {
var rootCmd = &cobra.Command{
Use: "root",
}
rootCmd.Flags().StringVar(&args.TenantID, "tenant-id", "", "azure subscription id")
rootCmd.Flags().StringVar(&args.SubscriptionID, "subscription-id", "", "azure tenant id")
x := rootCmd.Flags().Lookup("tenant-id")
log.Infof("%q %q %T", x.Name, x.Usage, x)
// prints: INFO[0000] "tenant-id" "azure subscription id" *pflag.Flag
x = rootCmd.Flags().Lookup("subscription-id")
log.Infof("%q %q %T", x.Name, x.Usage, x)
// prints: INFO[0000] "subscription-id" "azure tenant id" *pflag.Flag
return rootCmd
}
Hello,
I would like to confirm if this package is still maintained or basically the maintained fork is https://github.com/spf13/pflag.
In case that the mentioned fork is the maintained version, could you please add a note at the beginning of the README; I think that it will be very useful for the people who lands in the repo browsing in Google.
Many thanks.
Hi,
is there a way to don't lose the -h flag that in the standard library produce a list of all flags registered?
Hi have register a flag:
var userID *int = pflag.IntP("user", "u", 0, "User ID")
flag.Parse()
fmt.Printf("User ID %v\n", *userID)
if I run go run main.go -h
I get this message:
Usage of /tmp/go-build252835213/command-line-arguments/_obj/exe/main:
exit status 2
Thanks
The flag
package does this with the boolFlag interface. See http://golang.org/src/flag/flag.go?s=11723:11745#L764.
We use this in kubernetes here: https://github.com/GoogleCloudPlatform/kubernetes/blob/master/pkg/version/verflag/verflag.go#L40
I've just started using gpm
to manage dependency versions. Could I persuade you to tag a release?
I spent a bit scratching my head trying to figure out why my flags weren't being set/parsed correctly. The issue was that I was using f := pflag.FlagSet{}
instead of f := pflag.NewFlagSet(...)
Making an interface would be a simple way to make the behavior more obvious. Thanks!
Hi, things is I want parse 2 bool args, but there is a positional arg, like this:
gofind ./ -d -f
./ is a positional arg, but once I add this , -d -f cannot parse which is default by false , so it should be true if set. But always false. How to parse them? and what if I want using
-d -f as -df just like tar -xvf?
Many thanks if can get a reply or advise!
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.