aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorTeddy Wing2017-06-03 20:33:17 +0200
committerTeddy Wing2017-06-03 20:53:14 +0200
commitbcb86d1e025c6928018f94c5634eed88e50c0a58 (patch)
tree134032a50a34250587f0a8a21d6e40806c935240 /main.go
parent57f64ce855309b27ea483af81cfdb1d38d1db7f3 (diff)
downloadtimetasker-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.
Diffstat (limited to 'main.go')
-rw-r--r--main.go5
1 files changed, 4 insertions, 1 deletions
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 {