CLI cleanup to consolidate session-related subcommands
This commit is contained in:
43
internal/app/session_args.go
Normal file
43
internal/app/session_args.go
Normal file
@@ -0,0 +1,43 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user