| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package cli |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | |
| 8 | "github.com/jessevdk/go-flags" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/executable" |
| 11 | ) |
| 12 | |
| 13 | // Option defines command line options. |
| 14 | type Option struct { |
| 15 | UpdateEvery int |
| 16 | Module string `short:"m" long:"modules" description:"module name to run" default:"all"` |
| 17 | Job []string `short:"j" long:"job" description:"job name to run"` |
| 18 | ConfDir []string `short:"c" long:"config-dir" description:"config dir to read"` |
| 19 | WatchPath []string `short:"w" long:"watch-path" description:"config path to watch"` |
| 20 | Debug bool `short:"d" long:"debug" description:"debug mode"` |
| 21 | Version bool `short:"v" long:"version" description:"display the version and exit"` |
| 22 | Function string `long:"function" description:"execute function once (module name)"` |
| 23 | FunctionArgs []string `long:"function-args" description:"function args (repeatable, e.g. info)"` |
| 24 | FunctionPayload string `long:"function-payload" description:"function payload JSON or @file.json"` |
| 25 | FunctionTimeout string `long:"function-timeout" description:"function timeout (e.g. 60s)"` |
| 26 | } |
| 27 | |
| 28 | // Parse returns parsed command-line flags in Option struct |
| 29 | func Parse(args []string) (*Option, error) { |
| 30 | opt := &Option{ |
| 31 | UpdateEvery: 1, |
| 32 | } |
| 33 | parser := flags.NewParser(opt, flags.Default) |
| 34 | parser.Name = executable.Name |
| 35 | parser.Usage = "[OPTIONS] [update every]" |
| 36 | |
| 37 | rest, err := parser.ParseArgs(args) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | |
| 42 | if len(rest) > 1 { |
| 43 | if opt.UpdateEvery, err = strconv.Atoi(rest[1]); err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return opt, nil |
| 49 | } |
| 50 | |
| 51 | func IsHelp(err error) bool { |
| 52 | return flags.WroteHelp(err) |
| 53 | } |