outPtr := flag.String("o", "path", "PATH to save file")
findPtr := flag.String("f", "find", "find sentences with that word")
flag.Parse()
Why when I pass values to the -f flag like “word” it takes that string, but is not the case of -o which always takes the default string “path” and ignores whatever string I pass in. I need some help to understand that.
Can yoü please provide a fully runnable example which we can build and run locally which shows the exact problem you have? Perhaps including some example runs from the terminal with their output and the expected output?
The arguments and flags are : env (pront env variables)
-f “string” --> print proverbs with that string
-o “path” Should write to a file in the specified path.
The problem is when giving the -o flag it just do use the defaut string at
outPtr := flag.String("o", "path", "PATH to save file")
which is “path” So it writes a file with the name path in the code directory.
./flags --> returns nothing
./flags 2 --> returns sentence #2
./flags -f with --> return sentences with word “with”
./flags list --> returns all sentences
./flags list -o ~/test.txt --> should return a file at ~/ named text.txt with the content of ./flags list
The problems show up when you provide the flag -o followed by the string. In the example the string is ~/test.txt but the program takes the default “path” specified at
outPtr := flag.String("o", "path", "PATH where the file is written at")
With the go flags package you need to provide options before positional arguments. That is, not “flags list -o …” but “flags -o … list” like you did with -f.
So, in conclusion always first is parsed options, after that the flag.Parse() function stops parsing. So that, it is mandatory to pass first arguments like -f or -o and then all non options arguments.
Is that right?