summaryrefslogtreecommitdiff
path: root/cli_args.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2018-06-05 21:02:35 +0200
committerJakob Unterwurzacher2018-06-05 21:02:35 +0200
commite29a81efc3df88b451a4a9464724a952d97b4115 (patch)
tree04efcc8e5cb0101ec12b9a6e3238a874675b97cc /cli_args.go
parent98aa9cb1768985b5f4dff33eebf1bc0ebd4a90b3 (diff)
main: make prefixOArgs errors testable
By returning an error instead of calling os.Exit, error cases can be tested easily. Error cases were not tested until now.
Diffstat (limited to 'cli_args.go')
-rw-r--r--cli_args.go19
1 files changed, 11 insertions, 8 deletions
diff --git a/cli_args.go b/cli_args.go
index 2f0c936..6253c14 100644
--- a/cli_args.go
+++ b/cli_args.go
@@ -42,16 +42,16 @@ var flagSet *flag.FlagSet
// prefixOArgs transform options passed via "-o foo,bar" into regular options
// like "-foo -bar" and prefixes them to the command line.
// Testcases in TestPrefixOArgs().
-func prefixOArgs(osArgs []string) []string {
+func prefixOArgs(osArgs []string) ([]string, error) {
// Need at least 3, example: gocryptfs -o foo,bar
// ^ 0 ^ 1 ^ 2
if len(osArgs) < 3 {
- return osArgs
+ return osArgs, nil
}
// Passing "--" disables "-o" parsing. Ignore element 0 (program name).
for _, v := range osArgs[1:] {
if v == "--" {
- return osArgs
+ return osArgs, nil
}
}
// Find and extract "-o foo,bar"
@@ -60,8 +60,7 @@ func prefixOArgs(osArgs []string) []string {
if osArgs[i] == "-o" {
// Last argument?
if i+1 >= len(osArgs) {
- tlog.Fatal.Printf("The \"-o\" option requires an argument")
- os.Exit(exitcodes.Usage)
+ return nil, fmt.Errorf("The \"-o\" option requires an argument")
}
oOpts = strings.Split(osArgs[i+1], ",")
// Skip over the arguments to "-o"
@@ -87,16 +86,20 @@ func prefixOArgs(osArgs []string) []string {
}
// Add other arguments
newArgs = append(newArgs, otherArgs...)
- return newArgs
+ return newArgs, nil
}
// parseCliOpts - parse command line options (i.e. arguments that start with "-")
func parseCliOpts() (args argContainer) {
- os.Args = prefixOArgs(os.Args)
-
var err error
var opensslAuto string
+ os.Args, err = prefixOArgs(os.Args)
+ if err != nil {
+ tlog.Fatal.Println(err)
+ os.Exit(exitcodes.Usage)
+ }
+
flagSet = flag.NewFlagSet(tlog.ProgramName, flag.ContinueOnError)
flagSet.Usage = helpShort
flagSet.BoolVar(&args.debug, "d", false, "")