This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

cli: Make `usage` dynamically generate the list of subcommands

Previously, the quilt usage string used a hardcoded list of subcommands to
display to the user. This makes usage generate the list of subcommands
at runtime from those that are defined in cli.go.
  • Loading branch information...
aegamesi authored and kklin committed Oct 3, 2017
1 parent 73e6890 commit f1daffe4e252188441c2dac1c1c1252d1f6147ae
View
@@ -3,6 +3,7 @@ package cli
import (
"flag"
"os"
"sort"
"github.com/kelda/kelda/cli/command"
"github.com/kelda/kelda/cli/command/inspect"
@@ -60,6 +61,16 @@ func HasSubcommand(name string) bool {
return ok
}
// GetSubcommands returns a slice containing the names of all subcommands
func GetSubcommands() []string {
var result []string
for cmd := range commands {
result = append(result, cmd)
}
sort.Strings(result)
return result
}
func parseSubcommand(name string, cmd command.SubCommand, args []string) (
command.SubCommand, error) {
View
@@ -14,6 +14,7 @@ import (
"google.golang.org/grpc/grpclog"
"github.com/mitchellh/go-wordwrap"
log "github.com/sirupsen/logrus"
)
@@ -25,13 +26,10 @@ To see the help text for a given command:
kelda COMMAND --help
Commands:
counters, daemon, debug-logs, init, inspect, logs, minion, show, run, ssh,
stop, version`
`
func main() {
flag.Usage = func() {
util.PrintUsageString(keldaCommands, keldaExplanation, nil)
}
flag.Usage = printUsageString
var logLevelInfo = "logging level (debug, info, warn, error, fatal, or panic)"
var debugInfo = "turn on debug logging"
@@ -79,6 +77,17 @@ func main() {
}
}
// printUsageString generates and prints a usage string based off of the
// subcommands defined in cli.go.
func printUsageString() {
subcommands := strings.Join(cli.GetSubcommands(), ", ")
subcommands = wordwrap.WrapString(subcommands, 78)
subcommands = strings.Replace(subcommands, "\n", "\n ", -1)
explanation := keldaExplanation + " " + subcommands
util.PrintUsageString(keldaCommands, explanation, nil)
}
func usage() {
flag.Usage()
os.Exit(1)

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -754,6 +754,12 @@
"revision": "b8bc1bf767474819792c23f32d8286a45736f1c6",
"revisionTime": "2016-12-03T19:45:07Z"
},
{
"checksumSHA1": "L3leymg2RT8hFl5uL+5KP/LpBkg=",
"path": "github.com/mitchellh/go-wordwrap",
"revision": "ad45545899c7b13c020ea92b2072220eefad42b8",
"revisionTime": "2015-03-14T17:03:34Z"
},
{
"checksumSHA1": "EHjhpHipgm+XGccrRAms9AW3Ewk=",
"path": "github.com/mitchellh/mapstructure",

0 comments on commit f1daffe

Please sign in to comment.