Golang flag not working

Golang flag not working

GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. I see this issue as a regression, go1. So I could run tests with additional custom application relevant flags.

I think this is likely happening due to changed init precedence, that test framework init ran after app-level init. Looks like it's probably due to CLwhich was intended to address CC bradfitz. OK, so if that is an intended behavior I can fix my code by adding a go1. Init call inside. I don't think that a breaking change was intended. This seems like something that should be fixed before Go 1. We knew that fbc6a97 was going to break some people. It's arguably on the fence on whether it's inside the go1compat umbrella it's half API, half tooling and we guessed it would affect very few people.

That said, I haven't looked at this particular issue yet. I'll let cespare have a look first. It seems possible to fix this by putting the call to testing. Init in an earlier init function injected into the test variant of the package, rather than in testing.

If I understand correctly, the init functions within a package run in the order in which the source files were presented to the compiler, right? So vearutop this means that you shouldn't need to use testing.

golang flag not working

Init in your example code. In any case, it certainly shouldn't fail with such a mysterious error. As bcmills alluded to, the problem is that code which calls flag. Parse during initialization won't get the test flags. Putting this in a variable declaration expression means that it happens before flag.Or you can create custom flags that satisfy the Value interface with pointer receivers and couple them to flag parsing by.

Flags may then be used directly. If you're using the flags themselves, they are all pointers; if you bind to variables, they're values. After parsing, the arguments following the flags are available as the slice flag. Args or individually as flag. Arg i. The arguments are indexed from 0 through flag. NArg One or two minus signs may be used; they are equivalent. The last form is not permitted for boolean flags because the meaning of the command. Flag parsing stops just before the first non-flag argument "-" is a non-flag argument or after the terminator "--".

The default set of command-line flags is controlled by top-level functions. The FlagSet type allows one to define independent sets of flags, such as to implement subcommands in a command-line interface. The methods of FlagSet are analogous to the top-level functions for the command-line flag set. CommandLine is the default set of command-line flags, parsed from os. Usage prints a usage message documenting all defined command-line flags to CommandLine's output, which by default is os.

10 secrets in Go (Golang)

It is called when an error occurs while parsing flags. The function is a variable that may be changed to point to a custom function. By default it prints a simple header and calls PrintDefaults; for details about the format of the output and how to control it, see the documentation for PrintDefaults.

Custom usage functions may choose to exit the program; by default exiting happens anyway as the command line's error handling strategy is set to ExitOnError. Arg returns the i'th command-line argument. Arg 0 is the first remaining argument after flags have been processed. Arg returns an empty string if the requested element does not exist.

Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag. BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag. Duration defines a time.There are many ways to process CLI flags using Go. The first option is to not use any library at all, and inspect os.

The second option is to use the standard library flag package. The third option is to use one of the many 3rd party CLI libs out there, like Cobra. The first parameter is the flag name as used in the CLI command, the second parameter is the default value, and the third is the description.

The main difference is that in the first case, you get a pointer. In the second case, you get a value.

Golang Flag Not Working

Passing the wrong type for a flag will raise an error, halt the program, and the required usage will be printed to the user.

How do you set flag? Simple: attach -flagname to your CLI command, with 4 alternative but equivalent syntaxes:. You can pass as many flags as you want to a command, but the first time flag does not recognize a flag, it will stop parsing the additional ones. This means that flags must all go at the beginning, if you have non-flag parameters as well. Boolean flags can be set just by adding -countwhich makes the boolean flag get the true value.

You can do it by providing 2 flag handlers:. The flag package does not provide a built-in solution for this problem.

You need to manage this case yourself:. This is an introductory article. You can go into more in-depth topics from here, like implementing subcommands and definining your own flag types.

Int "count"5"the count of items" fmt. Println "count value "count. Durationname stringvalue time. String "description""default value""the description of the flag".

PrintDefaults os.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. So recently I ran into a problem with Golang flags and was curious to know if this is as intended, if its a problem or if I'm being a blithering idiot and there is a very simple answer to this.

The same happens if you then use. String "test", "", "test var" and then run it with. So my question is why can you not use double hyphens with flags? If you can, where did I go wrong when doing this? You can use double hyphens and this is documented in the package here :. Having declared your flags, flag.

Parse will try to remove one or two hyphens from the flags passed and use the remaining characters of each flag to detect whether one has been declared or not. This is done internally by the flag package in parseOne.

Declaring a flag with name -test will literally map it as is, resulting in flag. Parse internally looking for the name test instead of -test which is the one you actually declared, resulting in the error you see.

Learn more. Issue with golang flags Ask Question. Asked 2 years, 10 months ago. Active 1 year, 6 months ago. Viewed 2k times. String "-test", "", "test var" flag. John John 2 2 silver badges 8 8 bronze badges. Don't add the - to the flag name when you define it. If your question is truly "why can you not use double hyphens with flags", then it's not a question for SO, it's a question for whoever wrote the flag package.

That's just how the package works, and if you use it according to its documentation, it works according to its documentation. BTW, the flag packages will allow you to use --name for flags as a compatibility feature, even if that's not how they are declared. Active Oldest Votes. You can use double hyphens and this is documented in the package here : One or two minus signs may be used; they are equivalent. String "test", "", "test var" and you can use this flag with both one.

String "test", "", "test var" flag. Eugene Lisitsky Eugene Lisitsky 9, 4 4 gold badges 26 26 silver badges 52 52 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. Podcast Ben answers his first question on Stack Overflow.You might burn your fingers in that case, if you keep adding it in multiple sub-commands without giving it a second thought.

To understand what is really happening and why it is happening follow along. The cmd line tool built with cobra has following structure. The main tool is called cobrademo. And the sub-commands are alpha and num. Sub-command num has one more sub-command called one. Now I want a persistent flag --config to be availabe under sub-command one and alpha both.

golang flag not working

So I created a func that allows me to add this flag under any command, which looked like following:. Above code is here. Now this is called from one.

Share Post

If you see the output of both the commands it is different. While it should have been same i. What made it work in case of sub-command one and it did not work in case of sub-command alpha? Now we are registering a persistent flag twice once for sub-command one and again for alpha.

golang flag not working

And these calls happen from the init func of those files. If you look at the order of the evaluation of those init functions then it happens in alphabetical order. Hence the init func of alpha is called first and the flag config is registered there first and again it is registered for one.

So the final flag is just registered for one. Hence the functionality works correctly for one and not for alpha. So the right way to work with persistent flags is to register them only once. In our case most of the sub-commands will need it, so the right way to use it is to add it to the rootCmd.Golang Flag Not Working.

I don't see any reason to fail without letting user override. This is because Replica Sets are meant to be used as.

Parseotherwise debugging may fail on: flag provided but not defined: -test. Here is the dilemma: should Qux use Bar Start a new search. Posted on April 25, This time I was playing with github issues api and made small script which create new issues in given repository. CMake is an open-source, cross-platform family of tools designed to build, test and package software. Where's the code?

Check out the latest source code from github. Tomorrow I will work on any two articles from above, and then we can have a bot trial for the remaining third article. If copying was successful, copied to clipboard is displayed next to the local dev server URL. Golang flag Examples Pass command-line arguments to a program and receive them with the flag package. He is also a co-author of the book Go in Action, the author of the blog GoingGo.

For this reason, we use an exponential backoff Golang library to make sure our requests are successful. Go - Logical Operators - The following table lists all the logical operators supported by Go language.

Subscribe to RSS

In addition to the above, As of 2. Learn how to pass and accept command line flags, also known as arguments or parameters, in the Go programming language. If your question is truly "why can you not use double hyphens with flags", then it's not a question for SO, it's a question for whoever wrote the flag package.

Thus, what needs to be done is loop through all the packages, and merge the outputs into one. This flag allows you to delegate some work to an external program from the Go tool. Golang tcp packet. But note that even with Gustavo's nice suggestion you still do not end up with true getopt-like flag parsingn and -nofix would be accepted too, and -nv would not.GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. When trying to run the apt-buildpack integration tests on a computer running Go 1. Testing flags are now registered in the new Init function, which is invoked by the generated main function for the test. As a result, testing flags are now only registered when running a test binary, and packages that call flag.

Parse during package initialization may cause tests to fail. I think this is the test case problem, not the ginkgoproblem. In some cases explicit call for testing. Init before init functions in suites may help:. It occurs when flag. Parse is called within the init function. The solution is to move flag. Parse from the init function to somewhere else e. An example how to reproduce this error without using ginkgo can be found here.

I can reproduce it with the latest go version go1. I agree with zhubuntu that this is not a Ginkgo issue. I therefore suggest to close this issue. The solution is for users not to call flag.


thoughts on “Golang flag not working”

Leave a Reply

Your email address will not be published. Required fields are marked *