Command Line Arguments

I have the following flag definitions:

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?

This is where I have the code:


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.

I do not see anything related to a flag o there. Only f.

Please share a minimal code to reproduce your problem.

A single minimal main package, everything included, a true sscce.

1 Like

Oh I forgot to push the code. Now is there. I am building a minimal example to show you.

I have the file. Here it goes.
https://play.golang.org/p/gIQLm-PKA_H

How do we need to run it to actually be able to see your problem, which other preconditions needs to be met?

./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.

From the docs of flag:

Flag parsing stops just before the first non-flag argument (“-” is a non-flag argument) or after the terminator “–”.

So you need to provide the flag before your command.

1 Like

But it is a requirement the fillowing:

./flags -f with -o ~/test.txt 

Should returns the test.txt file with the sentences found that contain the word “with”

That’s two options and no positional arguments, so that’s fine.

1 Like

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?

Thank you for your help @calmh @NobbZ

I am here again: Is there any possibility of dealing with the requirement that I can pass positional arguments first? like in:

./binary list -o ~/test.txt

Sure, by using a different command line parser. I like kingpin.

1 Like

Thank you!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.