/
tool.go
35 lines (30 loc) · 1.01 KB
/
tool.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
// Copyright 2020 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// Package tool contains various helper utilitites useful for implementation of command line tools.
package tool
import (
"flag"
"fmt"
"os"
)
// Init handles common tasks for command line tools:
// - invokes flag.Parse
// - adds support for optional flags (see OptionalFlags)
// - adds support for cpu/mem profiling (-cpuprofile/memprofile flags)
//
// Use as defer tool.Init()().
func Init() func() {
flagCPUProfile := flag.String("cpuprofile", "", "write CPU profile to this file")
flagMEMProfile := flag.String("memprofile", "", "write memory profile to this file")
if err := ParseFlags(flag.CommandLine, os.Args[1:]); err != nil {
Fail(err)
}
return installProfiling(*flagCPUProfile, *flagMEMProfile)
}
func Failf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
func Fail(err error) {
Failf("%v", err)
}