Skip to content

Commit dc4ef34

Browse files
authored
Merge pull request #5 from sarus-suite/custom_flag_format
Custom command line help message flag format
2 parents 8c5c539 + 0586e43 commit dc4ef34

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

common/cli.go

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package common
33
import (
44
"flag"
55
"fmt"
6+
"io"
67
"os"
78

89
"github.com/sirupsen/logrus"
@@ -12,25 +13,69 @@ import (
1213
const Version = "1.0.0"
1314

1415
func usage_banner() {
15-
fmt.Fprintf(flag.CommandLine.Output(), `
16+
out := flag.CommandLine.Output()
17+
18+
// Header
19+
fmt.Fprintf(out, `
1620
Parallax
1721
OCI image migration tool for Podman on HPC systems
1822
1923
Usage:
20-
parallax -migrate -image <image[:tag]> [options]
21-
parallax -rmi -image <image[:tag]> [options]
24+
parallax --migrate --image <image[:tag]> [options]
25+
parallax --rmi --image <image[:tag]> [options]
2226
2327
Options:
2428
`)
25-
flag.PrintDefaults()
26-
fmt.Fprintf(flag.CommandLine.Output(), `
29+
30+
// New flag section
31+
order := []string{
32+
"migrate",
33+
"rmi",
34+
"image",
35+
"podmanRoot",
36+
"roStoragePath",
37+
"mksquashfsPath",
38+
"log-level",
39+
"version",
40+
}
41+
for _, name := range order {
42+
if f := flag.CommandLine.Lookup(name); f != nil {
43+
printFlag(out, f)
44+
}
45+
}
46+
47+
// Footer
48+
fmt.Fprintf(out, `
2749
Examples:
28-
parallax -migrate -image ubuntu:latest
29-
parallax -rmi -image alpine:3.18
50+
parallax --migrate --image ubuntu:latest
51+
parallax --rmi --image alpine:3.18
3052
3153
`)
3254
}
3355

56+
func printFlag(out io.Writer, f *flag.Flag) {
57+
// double-dashed name!
58+
line := fmt.Sprintf(" --%s", f.Name)
59+
60+
// alignment
61+
if len(f.Name) < 8 {
62+
line += "\t"
63+
} else {
64+
line += "\t"
65+
}
66+
67+
// usage text
68+
line += f.Usage
69+
70+
// show default
71+
if def := f.DefValue; def != "" && def != "false" {
72+
line += fmt.Sprintf(" (default %q)", def)
73+
}
74+
// and print
75+
fmt.Fprintln(out, line)
76+
}
77+
78+
3479
// Track we are being asked
3580
type Operation int
3681
const (
@@ -108,3 +153,4 @@ func ParseAndValidateFlags(fs *flag.FlagSet, args []string) (*CLI, error) {
108153
LogLevel: level,
109154
}, nil
110155
}
156+

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func main() {
2525
cli, err := common.ParseAndValidateFlags(flag.CommandLine, os.Args[1:])
2626
if err != nil {
2727
fmt.Fprintln(os.Stderr, err)
28-
flag.Usage()
28+
flag.CommandLine.Usage()
2929
os.Exit(2)
3030
}
3131

0 commit comments

Comments
 (0)