/
directive.go
51 lines (47 loc) · 1.36 KB
/
directive.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
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cli
import (
"fmt"
"strings"
"unicode"
"cogentcore.org/core/types"
"github.com/mattn/go-shellwords"
)
// ParseDirective parses and returns a comment directive from
// the given comment string. The returned directive will be nil
// if there is no directive contained in the given comment.
// Directives are of the following form (the slashes are optional):
//
// //tool:directive args...
func ParseDirective(comment string) (*types.Directive, error) {
comment = strings.TrimPrefix(comment, "//")
rs := []rune(comment)
if len(rs) == 0 || unicode.IsSpace(rs[0]) { // directives must not have whitespace as their first character
return nil, nil
}
// directives can not have newlines
if strings.Contains(comment, "\n") {
return nil, nil
}
before, after, found := strings.Cut(comment, ":")
if !found {
return nil, nil
}
directive := &types.Directive{}
directive.Tool = before
args, err := shellwords.Parse(after)
if err != nil {
return nil, fmt.Errorf("error parsing args: %w", err)
}
directive.Args = args
if len(args) > 0 {
directive.Directive = directive.Args[0]
directive.Args = directive.Args[1:]
}
if len(directive.Args) == 0 {
directive.Args = nil
}
return directive, nil
}