summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Unterwurzacher2016-10-09 20:55:33 +0200
committerJakob Unterwurzacher2016-10-09 20:55:33 +0200
commit9f0793ab0f7f73f8829cb86d6a645fd01eaa6849 (patch)
treeabb0563914196138f40acad7a19e9d0f1fe77cce
parentb70d2ffd94805c139971ece3cd372dafea5c95f3 (diff)
main: more useful error message on unknown flag
-rw-r--r--cli_args.go18
-rw-r--r--main.go4
2 files changed, 17 insertions, 5 deletions
diff --git a/cli_args.go b/cli_args.go
index 2d5ad7e..9ba5496 100644
--- a/cli_args.go
+++ b/cli_args.go
@@ -2,6 +2,7 @@ package main
import (
"flag"
+ "fmt"
"os"
"strconv"
"strings"
@@ -57,7 +58,7 @@ func parseCliOpts() (args argContainer) {
var err error
var opensslAuto string
- flagSet = flag.NewFlagSet(tlog.ProgramName, flag.ExitOnError)
+ flagSet = flag.NewFlagSet(tlog.ProgramName, flag.ContinueOnError)
flagSet.Usage = usageText
flagSet.BoolVar(&args.debug, "d", false, "")
flagSet.BoolVar(&args.debug, "debug", false, "Enable debug output")
@@ -99,7 +100,12 @@ func parseCliOpts() (args argContainer) {
flagSet.BoolVar(&ignoredBool, "nosuid", false, ignoreText)
flagSet.BoolVar(&ignoredBool, "nodev", false, ignoreText)
// Actual parsing
- flagSet.Parse(os.Args[1:])
+ err = flagSet.Parse(os.Args[1:])
+ if err != nil {
+ tlog.Warn.Printf("You passed: %s", prettyArgs())
+ tlog.Fatal.Printf("%v", err)
+ os.Exit(2)
+ }
// "-openssl" needs some post-processing
if opensslAuto == "auto" {
@@ -117,3 +123,11 @@ func parseCliOpts() (args argContainer) {
}
return args
}
+
+// prettyArgs pretty-prints the command-line arguments.
+func prettyArgs() string {
+ pa := fmt.Sprintf("%q", os.Args[1:])
+ // Get rid of "[" and "]"
+ pa = pa[1 : len(pa)-1]
+ return pa
+}
diff --git a/main.go b/main.go
index 6d03ee2..c5675e2 100644
--- a/main.go
+++ b/main.go
@@ -216,9 +216,7 @@ func main() {
}
// Default operation: mount.
if flagSet.NArg() != 2 {
- prettyArgs := fmt.Sprintf("%q", os.Args[1:])
- // Get rid of "[" and "]"
- prettyArgs = prettyArgs[1 : len(prettyArgs)-1]
+ prettyArgs := prettyArgs()
tlog.Info.Printf("Wrong number of arguments (have %d, want 2). You passed: %s",
flagSet.NArg(), prettyArgs)
tlog.Fatal.Printf("Usage: %s [OPTIONS] CIPHERDIR MOUNTPOINT [-o COMMA-SEPARATED-OPTIONS]", tlog.ProgramName)