github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/cmd/syncthing/usage.go (about)

     1  // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
     2  // All rights reserved. Use of this source code is governed by an MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"flag"
    10  	"fmt"
    11  	"io"
    12  	"text/tabwriter"
    13  )
    14  
    15  func optionTable(w io.Writer, rows [][]string) {
    16  	tw := tabwriter.NewWriter(w, 2, 4, 2, ' ', 0)
    17  	for _, row := range rows {
    18  		for i, cell := range row {
    19  			if i > 0 {
    20  				tw.Write([]byte("\t"))
    21  			}
    22  			tw.Write([]byte(cell))
    23  		}
    24  		tw.Write([]byte("\n"))
    25  	}
    26  	tw.Flush()
    27  }
    28  
    29  func usageFor(fs *flag.FlagSet, usage string, extra string) func() {
    30  	return func() {
    31  		var b bytes.Buffer
    32  		b.WriteString("Usage:\n  " + usage + "\n")
    33  
    34  		var options [][]string
    35  		fs.VisitAll(func(f *flag.Flag) {
    36  			var opt = "  -" + f.Name
    37  
    38  			if f.DefValue != "false" {
    39  				opt += "=" + fmt.Sprintf("%q", f.DefValue)
    40  			}
    41  
    42  			options = append(options, []string{opt, f.Usage})
    43  		})
    44  
    45  		if len(options) > 0 {
    46  			b.WriteString("\nOptions:\n")
    47  			optionTable(&b, options)
    48  		}
    49  
    50  		fmt.Println(b.String())
    51  
    52  		if len(extra) > 0 {
    53  			fmt.Println(extra)
    54  		}
    55  	}
    56  }