From bcb86d1e025c6928018f94c5634eed88e50c0a58 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 3 Jun 2017 20:33:17 +0200 Subject: 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. --- main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'main.go') diff --git a/main.go b/main.go index 90bb653..38cd527 100644 --- a/main.go +++ b/main.go @@ -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 { -- cgit v1.2.3