Skip to content

Commit c7de4bc

Browse files
committed
Ignore unknown flags
Signed-off-by: Lewis Marshall <lewis@lmars.net>
1 parent 9f15236 commit c7de4bc

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

main.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,25 @@ var (
3333
fields string
3434
)
3535

36+
// ignore unknown flags
37+
var flags = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
38+
3639
// Initialize flags.
3740
func init() {
38-
flag.BoolVar(&printVersion, "v", false, "print version.")
39-
flag.StringVar(&inputFile, "L", "", `source file names are read from the specified file. If file is "-", input is read from standard in.`)
40-
flag.StringVar(&outputFile, "f", "", `write output to specified file. If file is "-", output is written to standard out.`)
41-
flag.BoolVar(&recurse, "R", false, "recurse into directories in the file list.")
42-
flag.BoolVar(&sortOutput, "sort", true, "sort tags.")
43-
flag.BoolVar(&silent, "silent", false, "do not produce any output on error.")
44-
flag.BoolVar(&relative, "tag-relative", false, "file paths should be relative to the directory containing the tag file.")
45-
flag.BoolVar(&listLangs, "list-languages", false, "list supported languages.")
46-
flag.StringVar(&fields, "fields", "", "include selected extension fields (only +l).")
47-
48-
flag.Usage = func() {
41+
flags.BoolVar(&printVersion, "v", false, "print version.")
42+
flags.StringVar(&inputFile, "L", "", `source file names are read from the specified file. If file is "-", input is read from standard in.`)
43+
flags.StringVar(&outputFile, "f", "", `write output to specified file. If file is "-", output is written to standard out.`)
44+
flags.BoolVar(&recurse, "R", false, "recurse into directories in the file list.")
45+
flags.BoolVar(&sortOutput, "sort", true, "sort tags.")
46+
flags.BoolVar(&silent, "silent", false, "do not produce any output on error.")
47+
flags.BoolVar(&relative, "tag-relative", false, "file paths should be relative to the directory containing the tag file.")
48+
flags.BoolVar(&listLangs, "list-languages", false, "list supported languages.")
49+
flags.StringVar(&fields, "fields", "", "include selected extension fields (only +l).")
50+
51+
flags.Usage = func() {
4952
fmt.Fprintf(os.Stderr, "gotags version %s\n\n", Version)
5053
fmt.Fprintf(os.Stderr, "Usage: %s [options] file(s)\n\n", os.Args[0])
51-
flag.PrintDefaults()
54+
flags.PrintDefaults()
5255
}
5356
}
5457

@@ -113,7 +116,7 @@ func readNames(names []string) ([]string, error) {
113116
func getFileNames() ([]string, error) {
114117
var names []string
115118

116-
names = append(names, flag.Args()...)
119+
names = append(names, flags.Args()...)
117120
names, err := readNames(names)
118121
if err != nil {
119122
return nil, err
@@ -130,7 +133,7 @@ func getFileNames() ([]string, error) {
130133
}
131134

132135
func main() {
133-
flag.Parse()
136+
flags.Parse(os.Args[1:])
134137

135138
if printVersion {
136139
fmt.Printf("gotags version %s\n", Version)
@@ -145,13 +148,13 @@ func main() {
145148
files, err := getFileNames()
146149
if err != nil {
147150
fmt.Fprintf(os.Stderr, "cannot get specified files\n\n")
148-
flag.Usage()
151+
flags.Usage()
149152
os.Exit(1)
150153
}
151154

152155
if len(files) == 0 && len(inputFile) == 0 {
153156
fmt.Fprintf(os.Stderr, "no file specified\n\n")
154-
flag.Usage()
157+
flags.Usage()
155158
os.Exit(1)
156159
}
157160

@@ -169,7 +172,7 @@ func main() {
169172
fieldSet, err := parseFields(fields)
170173
if err != nil {
171174
fmt.Fprintf(os.Stderr, "%s\n\n", err)
172-
flag.Usage()
175+
flags.Usage()
173176
os.Exit(1)
174177
}
175178

0 commit comments

Comments
 (0)