forked from cmarcelo/go-urbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
57 lines (49 loc) · 1.4 KB
/
parse.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
52
53
54
55
56
57
// SPDX-License-Identifier: MIT
package flagconfig
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"strings"
)
// TODO: This was originally a shared parse function for the various
// examples, moved to a package to make my life easier. Need some API
// review and figure out a way to play nice with the various flag
// error behaviours.
// Parse provides support for reading flags from a configuration file
// in addition of the command line. The filename is passed as a
// pointer so its name can be parsed from the command line as a flag.
//
// NOTE: The FlagSet must use ContinueOnError.
func Parse(fs *flag.FlagSet, arguments []string, flagConfig *string) error {
// TODO: Can we do it without depending on this?
if fs.ErrorHandling() != flag.ContinueOnError {
return fmt.Errorf("flagconfig: Wrong ErrorHandling for FlagSet, must use flag.ContinueOnError")
}
err := fs.Parse(arguments)
if err != nil {
return err
}
// There is a configuration file.
if len(*flagConfig) > 0 {
var config []byte
config, err = ioutil.ReadFile(*flagConfig)
if err != nil {
return err
}
config = bytes.TrimSpace(config)
err = fs.Parse(strings.Fields(string(config)))
if err != nil {
return err
}
// TODO: Do this with two instead of three runs.
// Redo the parse from the command line to make sure that gets
// precedence.
err = fs.Parse(arguments)
if err != nil {
return err
}
}
return nil
}