parseOnePositional parses fs while allowing the single positional argument to come before the flags (Go's flag package stops parsing at the first non-flag argument, but the documented syntax is "chlink send --to ...").
(fs *flag.FlagSet, args []string, what string)
| 30 | // to come before the flags (Go's flag package stops parsing at the first |
| 31 | // non-flag argument, but the documented syntax is "chlink send <path> --to ..."). |
| 32 | func parseOnePositional(fs *flag.FlagSet, args []string, what string) (string, error) { |
| 33 | if len(args) > 0 && !strings.HasPrefix(args[0], "-") { |
| 34 | if err := fs.Parse(args[1:]); err != nil { |
| 35 | return "", err |
| 36 | } |
| 37 | if fs.NArg() != 0 { |
| 38 | return "", fmt.Errorf("unexpected extra arguments: %v", fs.Args()) |
| 39 | } |
| 40 | return args[0], nil |
| 41 | } |
| 42 | if err := fs.Parse(args); err != nil { |
| 43 | return "", err |
| 44 | } |
| 45 | if fs.NArg() != 1 { |
| 46 | fs.Usage() |
| 47 | return "", fmt.Errorf("expected exactly one %s", what) |
| 48 | } |
| 49 | return fs.Arg(0), nil |
| 50 | } |
| 51 | |
| 52 | // resolveTarget turns "ip" or "ip:port" into "host:port", defaulting the port. |
| 53 | func resolveTarget(to string, defaultPort int) (string, error) { |