diff options
| author | Teddy Wing | 2017-06-03 20:33:17 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-06-03 20:53:14 +0200 | 
| commit | bcb86d1e025c6928018f94c5634eed88e50c0a58 (patch) | |
| tree | 134032a50a34250587f0a8a21d6e40806c935240 | |
| parent | 57f64ce855309b27ea483af81cfdb1d38d1db7f3 (diff) | |
| download | timetasker-bcb86d1e025c6928018f94c5634eed88e50c0a58.tar.bz2 | |
main.go: Custom `Required()` for `--project` argument
As far as I've been able to figure out, Kingpin doesn't have a mechanism
for dependent arguments or conditional requireds. Thus there's no way to
say "--position is only required if --write-config isn't passed".
In that case, write our own "required" check, to enforce the presence of
"--project" only if "--write-config" isn't passed. We duplicate the
message that Kingpin provides from `Required()` and leverage its error
formatting (we should probably use this for our other error messages
too).
The only difference is that `--position=POSITION` won't appear on the
first line of the Help text, which would have emphasised the fact that
it's required. It's possible to configure Kingpin's help text via
templates, but I don't think that's worth the trouble at this point.
| -rw-r--r-- | main.go | 5 | 
1 files changed, 4 insertions, 1 deletions
| @@ -32,7 +32,6 @@ func main() {  		"Project alias defined in config.toml.",  	).  		Short('p'). -		Required().  		String()  	time_spent := kingpin.Flag("time", "Time spent working on project.").  		Short('t'). @@ -53,6 +52,10 @@ func main() {  	kingpin.Version(VERSION)  	kingpin.Parse() +	if *project_alias == "" && !*write_config { +		kingpin.Fatalf("required flag --project not provided, try --help") +	} +  	if *write_config {  		err = maybeWriteConfig()  		if err != nil { | 
