-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag.go
66 lines (56 loc) · 1.44 KB
/
flag.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
58
59
60
61
62
63
64
65
66
// Copyright © 2023 The Gomon Project.
package main
import (
"fmt"
"strconv"
"strings"
"github.com/zosmac/gocore"
)
type (
// flagpids, because []Pid cannot be a receiver type for flag.Value Set and String.
flagpids []Pid
)
var (
// flags defines the command line flags.
flags = struct {
verbose bool
pids flagpids
}{}
)
// init initializes the command line flags.
func init() {
gocore.Flags.CommandDescription = `The gotree command prints a tree listing of the processes running currently on the system.`
gocore.Flags.Var(
&flags.verbose,
"verbose",
"[-verbose]",
"Include full command path, arguments, and environment variables for each process in the list",
)
gocore.Flags.Var(
&flags.pids,
"pids",
"[-pids <pid>[,<pid>...]]",
"Print process tree for specific processes selected with comma separated list `pid[,pid...]`",
)
}
// Set is a flag.Value interface method to enable logLevel as a command line flag.
func (pids *flagpids) Set(arg string) error {
*pids = []Pid{}
args := strings.Split(arg, ",")
for _, arg := range args {
pid, err := strconv.Atoi(arg)
if err != nil {
return fmt.Errorf("%s, %v", arg, err)
}
*pids = append(*pids, Pid(pid))
}
return nil
}
// String is a flag.Value interface method to enable logLevel as a command line flag.
func (pids flagpids) String() string {
var args []string
for _, pid := range pids {
args = append(args, pid.String())
}
return strings.Join(args, ",")
}