Browse code

use tabwriter to display usage in mflag

Docker-DCO-1.1-Signed-off-by: Victor Vieux <vieux@docker.com> (github: vieux)

Victor Vieux authored on 2014/05/07 02:43:46
Showing 2 changed files
... ...
@@ -2,6 +2,7 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+
5 6
 	flag "github.com/dotcloud/docker/pkg/mflag"
6 7
 )
7 8
 
... ...
@@ -19,15 +20,17 @@ func init() {
19 19
 	flag.IntVar(&i, []string{"-integer", "-number"}, -1, "a simple integer")
20 20
 	flag.StringVar(&str, []string{"s", "#hidden", "-string"}, "", "a simple string") //-s -hidden and --string will work, but -hidden won't be in the usage
21 21
 	flag.BoolVar(&h, []string{"h", "#help", "-help"}, false, "display the help")
22
+	flag.StringVar(&str, []string{"mode"}, "mode1", "set the mode\nmode1: use the mode1\nmode2: use the mode2\nmode3: use the mode3")
22 23
 	flag.Parse()
23 24
 }
24 25
 func main() {
25 26
 	if h {
26 27
 		flag.PrintDefaults()
28
+	} else {
29
+		fmt.Printf("s/#hidden/-string: %s\n", str)
30
+		fmt.Printf("b: %b\n", b)
31
+		fmt.Printf("-bool: %b\n", b2)
32
+		fmt.Printf("s/#hidden/-string(via lookup): %s\n", flag.Lookup("s").Value.String())
33
+		fmt.Printf("ARGS: %v\n", flag.Args())
27 34
 	}
28
-	fmt.Printf("s/#hidden/-string: %s\n", str)
29
-	fmt.Printf("b: %b\n", b)
30
-	fmt.Printf("-bool: %b\n", b2)
31
-	fmt.Printf("s/#hidden/-string(via lookup): %s\n", flag.Lookup("s").Value.String())
32
-	fmt.Printf("ARGS: %v\n", flag.Args())
33 35
 }
... ...
@@ -83,6 +83,7 @@ import (
83 83
 	"sort"
84 84
 	"strconv"
85 85
 	"strings"
86
+	"text/tabwriter"
86 87
 	"time"
87 88
 )
88 89
 
... ...
@@ -419,11 +420,12 @@ func Set(name, value string) error {
419 419
 // PrintDefaults prints, to standard error unless configured
420 420
 // otherwise, the default values of all defined flags in the set.
421 421
 func (f *FlagSet) PrintDefaults() {
422
+	writer := tabwriter.NewWriter(f.out(), 20, 1, 3, ' ', 0)
422 423
 	f.VisitAll(func(flag *Flag) {
423
-		format := "  -%s=%s: %s\n"
424
+		format := "  -%s=%s"
424 425
 		if _, ok := flag.Value.(*stringValue); ok {
425 426
 			// put quotes on the value
426
-			format = "  -%s=%q: %s\n"
427
+			format = "  -%s=%q"
427 428
 		}
428 429
 		names := []string{}
429 430
 		for _, name := range flag.Names {
... ...
@@ -432,9 +434,18 @@ func (f *FlagSet) PrintDefaults() {
432 432
 			}
433 433
 		}
434 434
 		if len(names) > 0 {
435
-			fmt.Fprintf(f.out(), format, strings.Join(names, ", -"), flag.DefValue, flag.Usage)
435
+			fmt.Fprintf(writer, format, strings.Join(names, ", -"), flag.DefValue)
436
+			for i, line := range strings.Split(flag.Usage, "\n") {
437
+				if i != 0 {
438
+					line = "  " + line
439
+				}
440
+				fmt.Fprintln(writer, "\t", line)
441
+			}
442
+			//			start := fmt.Sprintf(format, strings.Join(names, ", -"), flag.DefValue)
443
+			//			fmt.Fprintln(f.out(), start, strings.Replace(flag.Usage, "\n", "\n"+strings.Repeat(" ", len(start)+1), -1))
436 444
 		}
437 445
 	})
446
+	writer.Flush()
438 447
 }
439 448
 
440 449
 // PrintDefaults prints to standard error the default values of all defined command-line flags.