gonfig icon indicating copy to clipboard operation
gonfig copied to clipboard

add support for an optional --version flag

Open costela opened this issue 6 years ago • 7 comments

This PR adds support for a simple--version flag. It is only enabled if a value is provided to the new Conf.VersionString setting.

The current implementation is a bit less customizable than the help settings (no VersionMessage or VersionDescription).

This also expands testing to check command output, with a - maybe overkill? - panic/recover combo. Please let me know if you think this should be handled some other way.

costela avatar Jan 29 '19 13:01 costela

Replaced strings.Builder usage with bytes.Buffer, which should be available in go < 1.10.

costela avatar Jan 29 '19 14:01 costela

I didn't know --version emits exit code 2, tbh.

Yeah, I was wondering why --help does it, but didn't wanna change it "just because". Maybe there's someone out there depending on this behavior.

OTOH, maybe --version doesn't have to imitate it...

Thoughts?

costela avatar Jan 29 '19 22:01 costela

I think I found that exit code 2 was for "misuse of the command line arguments", so basically when the help is printed not with --help but with a parsing failure or an unknown flag or so.

I guess I was just lazy and had help always exit 2. I think exiting "OK" for version is better.

stevenroose avatar Jan 29 '19 23:01 stevenroose

Fair enough. New version does exit(0) and is a bit more specific during testing, avoiding shadowing actual panics in the code. (note that we now only check if we exit, not the return code)

costela avatar Jan 30 '19 00:01 costela

Yeah ok, this might make sense.

stevenroose avatar Jan 30 '19 00:01 stevenroose

Perhaps still not entirely convinced why gonfig should do this, though.

I mean a program could have a

Version bool `short:"b"`

and then just in the first line of the program

if conf.Version {
    fmt.Println("v0.1.3")
    os.Exit(0)
}

That's like 5 lines of code that you're trying to reduce to one, kinda.

stevenroose avatar Jan 30 '19 00:01 stevenroose

True, just that this is boilerplate that I'd bet is basically everywhere. Just felt like something gonfig might make even easier and less "boilerplaty' with minimal effort (considering the biggest chunk of added code is testing that arguably should already be there for covering the exit-behavior of --help anyway).

But of course, this is a judgment call. No problem if you think this is just not the place for it. Feel free to close the PR! :+1:

costela avatar Jan 30 '19 07:01 costela