Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExistingDir() still checked when program run with --version #261

Open
gebn opened this issue Jan 13, 2019 · 2 comments
Open

ExistingDir() still checked when program run with --version #261

gebn opened this issue Jan 13, 2019 · 2 comments

Comments

@gebn
Copy link

gebn commented Jan 13, 2019

When using kingpin.Version("..."), passing --version means .Required() arguments do not have to be provided. I have an argument with a .Default() that must be an .ExistingDir(). The default path does not exist on the system. When passing only --version, I get an error that the default directory doesn't exist, rather than the version. Passing --version --help means the version is printed correctly.

It would be very useful if kingpin did not check that .ExistingDir()s actually existed if the program is run with only the --version flag.

gebn added a commit to gebn/plexbackup that referenced this issue Jan 13, 2019
@ygersie
Copy link

ygersie commented Oct 18, 2019

Hit the same issue, reproduce:

package main

import (
	"github.com/alecthomas/kingpin"
)

var (
	foo = kingpin.Flag("foo", "Foo").Default("foo.txt").File()
)

func main() {
	kingpin.Version("0.1")
	kingpin.Parse()
}
[~/goprojects/ygersie/test]$ go run kingpin.go --version
kingpin: error: open foo.txt: no such file or directory, try --help
exit status 1

@autarch
Copy link
Contributor

autarch commented Feb 16, 2020

I took a look at this and it's not easily fixable, AFAICT.

The issue is that internally --version is just yet another flag like all the others. When you call .Parse() the sequence always goes as follows:

  1. Do the raw CLI parsing so we know what flags & args were passed
  2. Set default values for flags
  3. Set values for all flags based on CLI (& env vars, I assume)
  4. Applies pre-actions, which is where --version is handled

The problem is that the pre-actions are able to see the flags from steps 2 & 3.

To fix this there would need to be some sort of pre-pre-action special case, maybe an "immediate action", where the presence of a particular flag can short circuit the normal processing.

This is doable, but I'm not sure what the best way to implement this in a non-hacky, extensible way would be.

bobrik added a commit to bobrik/ebpf_exporter that referenced this issue Oct 19, 2021
There's no good default config path anyway, and we hit the following issue:

* alecthomas/kingpin#261

```
$ ebpf_exporter --version
ebpf_exporter: error: open config.yaml: no such file or directory, try --help
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants