44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package app
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func isCLIFlagToken(arg string) bool {
|
|
return strings.HasPrefix(arg, "-") && arg != "-"
|
|
}
|
|
|
|
func pullLeadingSessionID(args []string) (string, []string) {
|
|
if len(args) == 0 || isCLIFlagToken(args[0]) {
|
|
return "", args
|
|
}
|
|
rest := append([]string(nil), args[1:]...)
|
|
return strings.TrimSpace(args[0]), rest
|
|
}
|
|
|
|
func applyPositionalSessionID(command, positional string, sessionID *string) error {
|
|
positional = strings.TrimSpace(positional)
|
|
if positional == "" {
|
|
return nil
|
|
}
|
|
existing := strings.TrimSpace(*sessionID)
|
|
if existing != "" && existing != positional {
|
|
return fmt.Errorf("%s: positional session id %q does not match expected session id %q", command, positional, existing)
|
|
}
|
|
*sessionID = positional
|
|
return nil
|
|
}
|
|
|
|
func applyParsedSessionIDArg(command string, fs *flag.FlagSet, sessionID *string) error {
|
|
switch fs.NArg() {
|
|
case 0:
|
|
return nil
|
|
case 1:
|
|
return applyPositionalSessionID(command, fs.Arg(0), sessionID)
|
|
default:
|
|
return fmt.Errorf("%s: unexpected positional arguments", command)
|
|
}
|
|
}
|