Questions tagged [go-flag]

A Go package to implement command-line flag parsing.

Documentation

20 questions
35
votes
5 answers

Process command line arguments in go test

Is there a way to get the command line arguments in go "tests", When you call go test obviously your main is not run, so is there a way to process command line arguments, One way would be to use the flags packages and check for the command line…
Ali
  • 18,665
  • 21
  • 103
  • 138
24
votes
3 answers

Defining Independent FlagSets in GoLang

The Go documentation (http://golang.org/pkg/flag/) says: The FlagSet type allows one to define independent sets of flags, such as to implement subcommands in a command-line interface. I need this functionality but I can't figure out how to…
chowey
  • 9,138
  • 6
  • 54
  • 84
8
votes
2 answers

Golang flag: Ignore missing flag and parse multiple duplicate flags

I am new to Golang and I have been unable to find a solution to this problem using flag. How can I use flag so my program can handle calls like these, where the -term flag may be present a variable number of times, including 0 times: ./myprogram -f…
matt
  • 318
  • 3
  • 11
4
votes
1 answer

How to group subcommands using go_flags package?

I am using https://github.com/jessevdk/go-flags and trying to group the subcommands using the top-level option "group" in the struct field. But instead of grouping the subcommands, it actually groups the options in the subcommands. Here is my…
MC X
  • 337
  • 4
  • 16
4
votes
1 answer

How do I extract only flagsets that are being set explicitly in the cli?

I am new to cobra and viper. I wanted to know if there's a way to exclude flag values that aren't set by the user from cli. So my problem is, I have some optional flags in my cobra cmd that have default values. I wonder if there's a way to exclude…
dekauliya
  • 1,303
  • 2
  • 15
  • 26
3
votes
2 answers

How to reference go-flag IsSet, functional code example needed

New to Go, and having a basic conceptual problem (I think)... Trying to use github.com/jessevdk/go-flags and have it mostly working. --help and whatnot are working fine, flags are being passed, etc. I need to understand if a option was set via a…
Cassey
  • 118
  • 9
2
votes
1 answer

Golang flag package stops parsing when encountering a non-flag

The built-in flags package seems to break parsing upon encountering the first non-flag argument for a process. Consider this application: package main import ( "flag" "fmt" ) func main() { alpha := flag.String("alpha", "", "the…
Thomas Hunter II
  • 5,081
  • 7
  • 35
  • 54
2
votes
1 answer

How to use custom flag in tests (with `testify/suite`)

I want to add custom flags for my Go tests that use testify/suite. It looks like from this thread that it can only be in TestMain() (init() if it is before Go 1.13). However, with the testify/suite pacakge, TestMain() is not quite an option. I have…
cxc
  • 201
  • 2
  • 10
2
votes
2 answers

Golang execute function using flags

How can I execute a specific function by entering a flag, for example, I have three flags: wordPtr numbPtr forkPtr And when executing a single one, the function that calls is executed: .\data -wordPtr Test When executing that flag,…
Oscar P
  • 167
  • 1
  • 7
2
votes
1 answer

Check if all flags were set (no flags blank)

How do I make sure every flag argument was set from the command line? I would like to do this without checking each flag name specifically, and would like to instead check all flags dynamically. Here is my code, main.go: package main import ( …
Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
2
votes
1 answer

Raise custom error in command-line parsing

I'm using the flag module to parse my flags, but want to have at least one positional argument. How do I show the usage help when not enough positional arguments are present, as I would in python with parser.error? Currently, I am manually calling…
phihag
  • 278,196
  • 72
  • 453
  • 469
1
vote
1 answer

How do i open all files of the format "test*.txt" in golang

If I give a flag (in golang) as 'test*.txt' (as a CLI argument), I need to open all files of the said format (example: test.txt, test1.txt, test2.txt). I am thinking of doing a regex match with all the files present in the folder. Is there a better…
hrsh
  • 108
  • 5
1
vote
0 answers

Add mulitple commands with go-flags

Working with jessevdk/go-flags, I would like to get the following usage command: ./main [OPTIONS] arg1 [arg2] I am able to get the OPTIONS right but I cannot add commands. I see that I can use AddCommand but I have not been able to generate my…
Arkon
  • 2,648
  • 6
  • 26
  • 46
1
vote
1 answer

value returned by flags into my struct

How can I assign the string value returned by flag into my struct? I have the following code. destDbCfg = &dbhelper.DbConfig {} destDbCfg.Database = flag.String( "destDBName", "", "Destination DB Database Name") flag.Parse() Database is a string
buildmaestro
  • 1,386
  • 5
  • 22
  • 37
1
vote
0 answers

Global go-flag AddCommand clarification and/or example

In my early days of learning Go and trying to use package github.com/jessevdk/go-flags. I checked the Fly example referenced in another go-flags thread, but it apparently does not use AddCommand (at least per my grep). The godoc article recommends…
Cassey
  • 118
  • 9
1
2