~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/lxc/lxd/lxc/help.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main
 
2
 
 
3
import (
 
4
        "bufio"
 
5
        "bytes"
 
6
        "fmt"
 
7
        "os"
 
8
        "sort"
 
9
        "strings"
 
10
 
 
11
        "github.com/lxc/lxd"
 
12
        "github.com/lxc/lxd/shared/gnuflag"
 
13
        "github.com/lxc/lxd/shared/i18n"
 
14
)
 
15
 
 
16
type helpCmd struct {
 
17
        showAll bool
 
18
}
 
19
 
 
20
func (c *helpCmd) showByDefault() bool {
 
21
        return true
 
22
}
 
23
 
 
24
func (c *helpCmd) usage() string {
 
25
        return i18n.G(
 
26
                `Presents details on how to use LXD.
 
27
 
 
28
lxd help [--all]`)
 
29
}
 
30
 
 
31
func (c *helpCmd) flags() {
 
32
        gnuflag.BoolVar(&c.showAll, "all", false, i18n.G("Show all commands (not just interesting ones)"))
 
33
}
 
34
 
 
35
func (c *helpCmd) run(_ *lxd.Config, args []string) error {
 
36
        if len(args) > 0 {
 
37
                for _, name := range args {
 
38
                        cmd, ok := commands[name]
 
39
                        if !ok {
 
40
                                fmt.Fprintf(os.Stderr, i18n.G("error: unknown command: %s")+"\n", name)
 
41
                        } else {
 
42
                                fmt.Fprintf(os.Stderr, cmd.usage()+"\n")
 
43
                        }
 
44
                }
 
45
                return nil
 
46
        }
 
47
 
 
48
        fmt.Println(i18n.G("Usage: lxc [subcommand] [options]"))
 
49
        fmt.Println(i18n.G("Available commands:"))
 
50
        var names []string
 
51
        for name := range commands {
 
52
                names = append(names, name)
 
53
        }
 
54
        sort.Strings(names)
 
55
        for _, name := range names {
 
56
                cmd := commands[name]
 
57
                if c.showAll || cmd.showByDefault() {
 
58
                        fmt.Printf("\t%-10s - %s\n", name, c.summaryLine(cmd.usage()))
 
59
                }
 
60
        }
 
61
        if !c.showAll {
 
62
                fmt.Println()
 
63
                fmt.Println(i18n.G("Options:"))
 
64
                fmt.Println("  --all              " + i18n.G("Print less common commands."))
 
65
                fmt.Println("  --debug            " + i18n.G("Print debug information."))
 
66
                fmt.Println("  --verbose          " + i18n.G("Print verbose information."))
 
67
                fmt.Println()
 
68
                fmt.Println(i18n.G("Environment:"))
 
69
                fmt.Println("  LXD_CONF           " + i18n.G("Path to an alternate client configuration directory."))
 
70
                fmt.Println("  LXD_DIR            " + i18n.G("Path to an alternate server directory."))
 
71
        }
 
72
        return nil
 
73
}
 
74
 
 
75
// summaryLine returns the first line of the help text. Conventionally, this
 
76
// should be a one-line command summary, potentially followed by a longer
 
77
// explanation.
 
78
func (c *helpCmd) summaryLine(usage string) string {
 
79
        usage = strings.TrimSpace(usage)
 
80
        s := bufio.NewScanner(bytes.NewBufferString(usage))
 
81
        if s.Scan() {
 
82
                if len(s.Text()) > 1 {
 
83
                        return s.Text()
 
84
                }
 
85
        }
 
86
        return i18n.G("Missing summary.")
 
87
}