Skip to content

Commit

Permalink
Update main.go (#107)
Browse files Browse the repository at this point in the history
* Update main.go

Update command line description of options

* command line help now like cbuild and shows up also without flags

* fixed a missing return check

* corrected wasted assign

---------

Co-authored-by: Bernd-Gunter Nitzler <[email protected]>
  • Loading branch information
jkrech and bgn42 authored Dec 15, 2023
1 parent e1e29e4 commit 99ea162
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 60 deletions.
106 changes: 68 additions & 38 deletions tools/eventlist/cmd/eventlist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,58 @@ func (s *includes) Set(v string) error {

var paths includes

func infoOpt(flags *flag.FlagSet, sopt string, lopt string, opt string) {
fmt.Print("\t")
func infoOpt(flags *flag.FlagSet, sopt string, lopt string, arg bool) error {
pos, err := fmt.Print(" ")
if err != nil {
return err
}
var n int
if sopt != "" {
fmt.Printf("-%s", sopt)
if n, err = fmt.Printf("-%s", sopt); err != nil {
return err
}
pos += n
}
if lopt != "" {
if sopt != "" {
fmt.Print(" ")
if sopt == "" {
if n, err = fmt.Printf(" "); err != nil {
return err
}
} else {
if n, err = fmt.Printf(", "); err != nil {
return err
}
}
pos += n
if n, err = fmt.Printf("--%s", lopt); err != nil {
return err
}
fmt.Printf("--%s", lopt)
pos += n
}
if opt != "" {
fmt.Printf(" %s", opt)
if arg {
if sopt == "" && lopt == "" {
if n, err = fmt.Printf(" "); err != nil {
return err
}
pos += n
}
if n, err = fmt.Printf(" arg"); err != nil {
return err
}
pos += n
}
fmt.Printf("%*s", 22-pos, " ")
if lopt == "help" {
fmt.Printf("\t%s\n", "show short help")
fmt.Printf("%s\n", "Print usage")
} else {
f := flags.Lookup(sopt)
if f == nil {
fmt.Printf("\t%s\n", "unknown option")
fmt.Printf("%s\n", "unknown option")
} else {
if opt == "" {
fmt.Printf("\t%s\n", f.Usage)
} else {
fmt.Printf(" %s\t%s\n", f.Value.String(), f.Usage)
}
fmt.Printf("%s\n", f.Usage)
}
}
return nil
}

func main() {
Expand Down Expand Up @@ -106,42 +130,48 @@ func main() {
usage := false

commFlag.Usage = func() {
fmt.Printf("Usage: %s [-I <scvdFile>]... [-o <outputFile>] [-a <elf/axfFile>] [-b] <logFile>\n",
Progname)
infoOpt(commFlag, "a", "", "<fileName>")
infoOpt(commFlag, "b", "begin", "")
infoOpt(commFlag, "h", "help", "")
infoOpt(commFlag, "I", "", "<fileName>")
infoOpt(commFlag, "o", "", "<fileName>")
infoOpt(commFlag, "s", "statistic", "")
infoOpt(commFlag, "V", "version", "")
infoOpt(commFlag, "f", "format", "<formatType>")
infoOpt(commFlag, "l", "level", "<Error|API|Op|Detail>")
fmt.Printf("%s: Event Listing %s\n\n", Progname, versionInfo)
fmt.Printf("Usage:\n %s [options] <logFile>\n\n", Progname)
fmt.Printf("Options:\n")
_ = infoOpt(commFlag, "a", "", true)
_ = infoOpt(commFlag, "b", "begin", false)
_ = infoOpt(commFlag, "h", "help", false)
_ = infoOpt(commFlag, "I", "", true)
_ = infoOpt(commFlag, "o", "", true)
_ = infoOpt(commFlag, "s", "statistic", false)
_ = infoOpt(commFlag, "V", "version", false)
_ = infoOpt(commFlag, "f", "format", true)
_ = infoOpt(commFlag, "l", "level", true)
usage = true
}
// parse command line
commFlag.Var(&paths, "I", "include SCVD file name")
outputFile := commFlag.String("o", "", "output file name")
elfFile := commFlag.String("a", "", "elf/axf file name")
formatType := commFlag.String("f", "", "format type: txt, json, xml")
level := commFlag.String("l", "", "level: Error|API|Op|Detail")
commFlag.Var(&paths, "I", "[...] Include SCVD file name(s)")
outputFile := commFlag.String("o", "", "Output file")
elfFile := commFlag.String("a", "", "Application file: elf/axf file name")
formatType := commFlag.String("f", "", "Output format: txt, json, xml")
level := commFlag.String("l", "", "Level: Error|API|Op|Detail")
var statBegin bool
commFlag.BoolVar(&statBegin, "b", false, "show statistic at beginning")
commFlag.BoolVar(&statBegin, "begin", false, "show statistic at beginning")
commFlag.BoolVar(&statBegin, "b", false, "Output order: show statistic before events")
commFlag.BoolVar(&statBegin, "begin", false, "Output order: show statistic before events")
var showVersion bool
commFlag.BoolVar(&showVersion, "V", false, "show version info")
commFlag.BoolVar(&showVersion, "version", false, "show version info")
commFlag.BoolVar(&showVersion, "V", false, "Show version info")
commFlag.BoolVar(&showVersion, "version", false, "Show version info")
var showStatistic bool
commFlag.BoolVar(&showStatistic, "s", false, "show statistic only")
commFlag.BoolVar(&showStatistic, "statistic", false, "show statistic only")
commFlag.BoolVar(&showStatistic, "s", false, "Output: show statistic but no events")
commFlag.BoolVar(&showStatistic, "statistic", false, "Output: show statistic but no events")
err = commFlag.Parse(os.Args[1:])

if usage || err != nil {
return
}

if len(os.Args) == 1 {
commFlag.Usage()
return
}

if showVersion {
fmt.Printf("%s %s\n", Progname, versionInfo)
fmt.Printf("%s: Event Listing %s\n", Progname, versionInfo)
return
}

Expand Down
59 changes: 37 additions & 22 deletions tools/eventlist/cmd/eventlist/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,35 @@ func Test_infoOpt(t *testing.T) { //nolint:golint,paralleltest
type args struct {
sopt string
lopt string
opt string
arg bool
}
tests := []struct {
name string
args args
want string
}{
{"test.run opt", args{"test.run", "", "ef"}, "\t-test.run ef yy\trun only tests and examples matching `regexp`\n"},
{"test.run", args{"test.run", "", ""}, "\t-test.run\trun only tests and examples matching `regexp`\n"},
{"test help", args{"", "help", ""}, "\t--help\tshow short help\n"},
{"test", args{"", "", ""}, "\t\tunknown option\n"},
{"test s", args{"a", "", ""}, "\t-a\tunknown option\n"},
{"test l", args{"", "cd", ""}, "\t--cd\tunknown option\n"},
{"test s l", args{"a", "cd", ""}, "\t-a --cd\tunknown option\n"},
{"test opt", args{"", "", "ef"}, "\t ef\tunknown option\n"},
{"test s opt", args{"a", "", "ef"}, "\t-a ef\tunknown option\n"},
{"test l opt", args{"", "cd", "ef"}, "\t--cd ef\tunknown option\n"},
{"test s l opt", args{"a", "cd", "ef"}, "\t-a --cd ef\tunknown option\n"},
{"test.run opt", args{"test.run", "", true},
" -test.run arg run only tests and examples matching `regexp`\n"},
{"test.run", args{"test.run", "", false},
" -test.run run only tests and examples matching `regexp`\n"},
{"test help", args{"", "help", false},
" --help Print usage\n"},
{"test", args{"", "", false},
" unknown option\n"},
{"test s", args{"a", "", false},
" -a unknown option\n"},
{"test l", args{"", "cd", false},
" --cd unknown option\n"},
{"test s l", args{"a", "cd", false},
" -a, --cd unknown option\n"},
{"test opt", args{"", "", true},
" arg unknown option\n"},
{"test s opt", args{"a", "", true},
" -a arg unknown option\n"},
{"test l opt", args{"", "cd", true},
" --cd arg unknown option\n"},
{"test s l opt", args{"a", "cd", true},
" -a, --cd arg unknown option\n"},
}
_ = flag.Set("test.run", "yy")
for _, tt := range tests { //nolint:golint,paralleltest
Expand All @@ -117,7 +128,7 @@ func Test_infoOpt(t *testing.T) { //nolint:golint,paralleltest
defer restore()
r, w, _ := os.Pipe()
os.Stdout = w
infoOpt(flag.CommandLine, tt.args.sopt, tt.args.lopt, tt.args.opt)
_ = infoOpt(flag.CommandLine, tt.args.sopt, tt.args.lopt, tt.args.arg)
w.Close()
buf, _ := io.ReadAll(r)
if string(buf) != tt.want {
Expand Down Expand Up @@ -153,16 +164,20 @@ func Test_main(t *testing.T) { //nolint:golint,paralleltest
"----- ----- ----- --- --- ------- ----- ----\\n"

help :=
"Usage: [^ ]+ \\[-I <scvdFile>\\]\\.\\.\\. \\[-o <outputFile>\\] \\[-a <elf/axfFile>\\] \\[-b\\] <logFile>\\n" +
"\\t-a <fileName> \\telf/axf file name\\n" +
"\\t-b --begin\\tshow statistic at beginning\\n" +
"\\t-h --help\\tshow short help\\n" +
"\\t-I <fileName> \\tinclude SCVD file name\\n" +
"\\t-o <fileName> \\toutput file name\\n" +
"\\t-s --statistic\\tshow statistic only\\n" +
"\\t-V --version\\tshow version info\\n"
"Usage:\\n" +
" [^ ]+ \\[-options\\] <logFile>\\n\\n" +
"Options:\\n" +
" -a arg Application file: telf/axf file name\\n" +
" -b --begin Output order: show statistic at beginning\\n" +
" -h --help Print usage\\n" +
" -I arg \\[\\.\\.\\.\\] Include SCVD file name\\n" +
" -o arg Output file\\n" +
" -s --statistic Output: show statistic but no events\\n" +
" -V --version Show version info\\n" +
" -f --format arg Output format: txt, json, xml\\n" +
" -l --level arg Level: Error|API|Op|Detail\\n"

versionInfo = "1.2.3 (C) 2022 Arm Ltd. and Contributors"
versionInfo = "1.2.3 (C) 2023 Arm Ltd. and Contributors"
tests := []struct {
name string
args []string
Expand Down

0 comments on commit 99ea162

Please sign in to comment.