github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/cli/args/move_postfix.go (about) 1 package args 2 3 import ( 4 "strings" 5 ) 6 7 /* 8 Used to move non option(-) arguments to the end of the command, this way 9 positional arguments do not have to go before the optional arguments 10 11 Arguments do not behave how a unix cli would expect them to: 12 - https://github.com/urfave/cli/issues/427 13 */ 14 func MovePostfixOptions(args []string, validFlags []ParsedFlag) []string { 15 if len(args) == 1 { 16 return args 17 } 18 19 // Create new arguments with the first arg as the command 20 new_args := []string{args[0]} 21 22 // Arguments after the command 23 args = args[1:] 24 positional_args := make([]string, 0) 25 flagBoolMap := buildFlagBoolMap(validFlags) 26 27 // skip handles the case of `--name someName` vs there is no skip needed for `--name=someName` 28 var skip bool 29 30 for idx, arg := range args { 31 if skip { 32 skip = false 33 continue 34 } 35 36 if len(arg) == 0 { 37 new_args = append(new_args, arg) 38 continue 39 } 40 41 if arg[:1] == "-" { 42 flagIsBool, ok := flagBoolMap[arg] 43 if ok { 44 if flagIsBool || strings.Contains(arg, "=") || len(args) == idx+1 { 45 if flagIsBool { 46 47 // if next idx exists and strings.ToLower(next idx) == "true" || "false" remove and skip 48 if len(args) > idx+1 { 49 if strings.ToLower(args[idx+1]) == "true" { 50 skip = true 51 } else if strings.ToLower(args[idx+1]) == "false" { 52 skip = true 53 arg = "-no-" + strings.ReplaceAll(arg, "-", "") 54 } 55 } 56 } 57 58 new_args = append(new_args, arg) 59 continue 60 } 61 62 // `--name someName` 63 new_args = append(new_args, arg, args[idx+1]) 64 skip = true 65 continue 66 } 67 } 68 69 // Everything else 70 positional_args = append(positional_args, arg) 71 } 72 73 return append(new_args, positional_args...) 74 }