Skip to content

Commit 2667a4b

Browse files
authored
Merge pull request #78 from ilmanzo/fix_help_messages
Fix help message handling
2 parents 80b7044 + a63b173 commit 2667a4b

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

cmd/funkoverage.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
"strings"
99
)
1010

11-
const versionString = "0.4.4"
11+
const versionString = "0.4.5"
1212

1313
// --- CLI ---
1414

1515
func main() {
1616
if len(os.Args) < 2 {
17-
fmt.Println(helpText)
17+
fmt.Print(helpText)
1818
os.Exit(1)
1919
}
2020

@@ -24,9 +24,24 @@ func main() {
2424
reportCmd := flag.NewFlagSet("report", flag.ExitOnError)
2525
reportFormats := reportCmd.String("formats", "html,txt,xml", "Comma-separated list: html,xml,txt (default: html,txt,xml)")
2626

27+
wrapCmd.Usage = func() {
28+
fmt.Print(wrapHelpText)
29+
wrapCmd.PrintDefaults()
30+
}
31+
32+
unwrapCmd.Usage = func() {
33+
fmt.Print(unwrapHelpText)
34+
unwrapCmd.PrintDefaults()
35+
}
36+
37+
reportCmd.Usage = func() {
38+
fmt.Print(reportHelpText)
39+
reportCmd.PrintDefaults()
40+
}
41+
2742
switch os.Args[1] {
2843
case "help", "--help", "-h":
29-
fmt.Println(helpText)
44+
fmt.Print(helpText)
3045
return
3146
case "version", "--version", "-v":
3247
fmt.Println("funkoverage version", versionString)
@@ -114,7 +129,7 @@ func main() {
114129
}
115130
default:
116131
fmt.Println("Unknown command:", os.Args[1])
117-
fmt.Println(helpText)
132+
fmt.Print(helpText)
118133
os.Exit(1)
119134
}
120135
}

cmd/templates.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,59 @@
11
package main
22

3-
import _ "embed"
3+
import (
4+
_ "embed"
5+
"fmt"
6+
"strings"
7+
)
48

59
//go:embed templates/detailed.html
610
var detailedHTMLTemplateStr string
711

812
//go:embed templates/aggregate.html
913
var aggregateHTMLTemplate string
1014

11-
const helpText = `Usage:
12-
wrap /path/to/binary
13-
Wrap the given ELF binary with the Pin coverage wrapper.
15+
const wrapHelpText = `Usage: funkoverage wrap /path/to/binary
16+
Wrap the given ELF binary with the Pin coverage wrapper.`
1417

15-
unwrap /path/to/binary
16-
Restore the original binary previously wrapped.
18+
const unwrapHelpText = `Usage: funkoverage unwrap /path/to/binary
19+
Restore the original binary previously wrapped.`
1720

18-
report <inputdir|log1.txt,log2.txt> <outputdir> [--formats <formats>]
19-
Generate coverage reports from log files.
20-
<inputdir> Directory containing .log files (all will be used)
21-
log1.txt,log2.txt Comma-separated list of log files
22-
<outputdir> Output directory for reports (mandatory)
23-
--formats Comma-separated list: html,xml,txt (default: html,txt,xml)
21+
const reportHelpText = `Usage: funkoverage report <inputdir|log1.txt,log2.txt> <outputdir> [--formats <formats>]
2422
23+
Generate coverage reports from log files.
24+
<inputdir> Directory containing .log files (all will be used)
25+
log1.txt,log2.txt Comma-separated list of log files
26+
<outputdir> Output directory for reports (mandatory)
27+
--formats Comma-separated list: html,xml,txt (default: html,txt,xml)
28+
`
29+
30+
var helpText string
31+
32+
func init() {
33+
// We use fmt.Sprintf to build the main help text from the subcommand help texts
34+
// to avoid duplication. The subcommand help texts are modified slightly for
35+
// proper formatting in the main help view.
36+
helpText = fmt.Sprintf(`Usage:
37+
%s
38+
%s
39+
%s
2540
help
2641
Show this help message.
27-
2842
version
2943
Show program version.
3044
3145
Environment variables:
3246
PIN_ROOT Path to Intel Pin root directory (default: autodetect or required)
3347
PIN_TOOL_SEARCH_DIR Directory to search for FuncTracer.so (default: /usr/lib64/coverage-tools)
3448
LOG_DIR Directory for coverage logs (default: /var/coverage/data)
35-
SAFE_BIN_DIR Directory to store original binaries (default: /var/coverage/bin)`
49+
SAFE_BIN_DIR Directory to store original binaries (default: /var/coverage/bin)
50+
`,
51+
indent(strings.TrimPrefix(wrapHelpText, "Usage: funkoverage "), " "),
52+
indent(strings.TrimPrefix(unwrapHelpText, "Usage: funkoverage "), " "),
53+
indent(strings.TrimPrefix(reportHelpText, "Usage: funkoverage "), " "))
54+
}
55+
56+
// indent adds indentation to each line of a string.
57+
func indent(text, indentation string) string {
58+
return indentation + strings.ReplaceAll(strings.TrimSpace(text), "\n", "\n"+indentation)
59+
}

0 commit comments

Comments
 (0)