-
Notifications
You must be signed in to change notification settings - Fork 47
/
args_trailer.go
38 lines (31 loc) · 988 Bytes
/
args_trailer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Copyright (c) 2021 - Present. Blend Labs, Inc. All rights reserved
Use of this source code is governed by a MIT license that can be found in the LICENSE file.
*/
package sh
import (
"strings"
"github.com/blend/go-sdk/ex"
)
// Errors
const (
ErrFlagsNoTrailer ex.Class = "sh; error parsing args trailer; missing '--' token, or nothing follows it"
)
// ArgsTrailer parses a set of os.Args, and returns everything after the `--` token.
// If there is no `--` token, an exception class "ErrFlagsNoTrailer" is returned.
func ArgsTrailer(args ...string) ([]string, error) {
var foundIndex int
for index, arg := range args {
if strings.TrimSpace(arg) == "--" {
foundIndex = index
break
}
}
if foundIndex == 0 {
return nil, ex.New(ErrFlagsNoTrailer, ex.OptMessagef("args: %v", strings.Join(args, " ")))
}
if foundIndex == len(args)-1 {
return nil, ex.New(ErrFlagsNoTrailer, ex.OptMessage("cannot be the last flag argument"))
}
return args[foundIndex+1:], nil
}