Merge pull request #1338 from harshavardhana/help-rewrite

help: Re-write help style now each command has --help
This commit is contained in:
Harshavardhana
2015-11-16 02:23:47 -08:00
30 changed files with 193 additions and 394 deletions
+11
View File
@@ -26,11 +26,19 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
accessFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of access",
}
)
// Set access permissions.
var accessCmd = cli.Command{
Name: "access",
Usage: "Set or get access permissions.",
Action: mainAccess,
Flags: []cli.Flag{accessFlagHelp},
CustomHelpTemplate: `Name:
mc {{.Name}} - {{.Usage}}
@@ -41,6 +49,9 @@ USAGE:
PERMISSION:
Allowed permissions are: [private, readonly, public, authorized].
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Set bucket to "private" on Amazon S3 cloud storage.
$ mc {{.Name}} set private https://s3.amazonaws.com/burningman2011
+16 -8
View File
@@ -30,17 +30,28 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
catFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of cat",
}
)
// Display contents of a file.
var catCmd = cli.Command{
Name: "cat",
Usage: "Display contents of a file.",
Action: mainCat,
Flags: []cli.Flag{catFlagHelp},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} SOURCE [SOURCE...]
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Stream an object from Amazon S3 cloud storage to mplayer standard input.
$ mc {{.Name}} https://s3.amazonaws.com/ferenginar/klingon_opera_aktuh_maylotah.ogg | mplayer -
@@ -51,19 +62,16 @@ EXAMPLES:
3. Concantenate multiple files to one.
$ mc {{.Name}} part.* > complete.img
4. Behave like operating system ‘cat’ tool. Useful for aliasing.
$ alias {{.Name}}='mc --mimic {{.Name}}'
$ {{.Name}} /etc/issue.txt
`,
}
// checkCatSyntax performs command-line input validation for cat command.
func checkCatSyntax(ctx *cli.Context) {
if !ctx.Args().Present() && !globalMimicFlag {
cli.ShowCommandHelpAndExit(ctx, "cat", 1) // last argument is exit code
args := ctx.Args()
if !args.Present() {
args = []string{"-"}
}
for _, arg := range ctx.Args() {
for _, arg := range args {
if strings.HasPrefix(arg, "-") && len(arg) > 1 {
fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Unknown flag ‘%s’ passed.", arg))
}
@@ -116,7 +124,7 @@ func mainCat(ctx *cli.Context) {
checkCatSyntax(ctx)
stdinMode := false
if globalMimicFlag && !ctx.Args().Present() {
if !ctx.Args().Present() {
stdinMode = true
}
+8 -1
View File
@@ -16,7 +16,6 @@
package main
// listAliases - list alias
import (
"encoding/json"
"fmt"
@@ -29,10 +28,18 @@ import (
"github.com/minio/minio-xl/pkg/quick"
)
var (
configAliasFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of config alias",
}
)
var configAliasCmd = cli.Command{
Name: "alias",
Usage: "List, modify and remove aliases in configuration file.",
Action: mainConfigAlias,
Flags: []cli.Flag{configAliasFlagHelp},
CustomHelpTemplate: `NAME:
mc config {{.Name}} - {{.Usage}}
+8
View File
@@ -14,9 +14,17 @@ import (
"github.com/minio/minio-xl/pkg/quick"
)
var (
configHostFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of config host",
}
)
var configHostCmd = cli.Command{
Name: "host",
Usage: "List, modify and remove hosts in configuration file.",
Flags: []cli.Flag{configHostFlagHelp},
Action: mainConfigHost,
CustomHelpTemplate: `NAME:
mc config {{.Name}} - {{.Usage}}
+19 -6
View File
@@ -28,24 +28,36 @@ import "github.com/minio/cli"
// so to avoid taking credentials over cli arguments. It is a security precaution
// ----
//
var (
configFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of config",
}
)
var configCmd = cli.Command{
Name: "config",
Usage: "Manage configuration file.",
Action: mainConfig,
Flags: []cli.Flag{configFlagHelp},
Subcommands: []cli.Command{
configAliasCmd,
configHostCmd,
configVersionCmd,
},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
{{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} COMMAND
{{.Name}} COMMAND
COMMANDS:
{{range .Subcommands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
FLAGS:
{{range .Flags}}{{.}}
{{end}}
COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}
`,
}
@@ -53,8 +65,9 @@ COMMANDS:
func mainConfig(ctx *cli.Context) {
if ctx.Args().First() != "" { // command help.
cli.ShowCommandHelp(ctx, ctx.Args().First())
} else { // mc help.
cli.ShowSubcommandHelp(ctx)
} else {
// command with Subcommands is an App.
cli.ShowAppHelp(ctx)
}
// sub-commands like "upload" and "download" have their own main.
+8
View File
@@ -24,11 +24,19 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
configVersionFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of config version",
}
)
// Print config version.
var configVersionCmd = cli.Command{
Name: "version",
Usage: "Print config version.",
Action: mainConfigVersion,
Flags: []cli.Flag{configVersionFlagHelp},
CustomHelpTemplate: `NAME:
mc config {{.Name}} - {{.Usage}}
+11
View File
@@ -34,17 +34,28 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
cpFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of cp",
}
)
// Copy command.
var cpCmd = cli.Command{
Name: "cp",
Usage: "Copy one or more objects to a target.",
Action: mainCopy,
Flags: []cli.Flag{cpFlagHelp},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} SOURCE [SOURCE...] TARGET
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Copy a list of objects from local file system to Amazon S3 cloud storage.
$ mc {{.Name}} Music/*.ogg https://s3.amazonaws.com/jukebox/
-5
View File
@@ -54,11 +54,6 @@ var (
Usage: "Enable debugging output.",
}
mimicFlag = cli.BoolFlag{
Name: "mimic",
Usage: "Behave like operating system tools. Use with shell aliases.",
}
// Add your new flags starting here
)
-1
View File
@@ -19,7 +19,6 @@ package main
var (
globalQuietFlag = false // Quiet flag set via command line
globalMimicFlag = false // Unix flag set via command line
globalJSONFlag = false // Json flag set via command line
globalDebugFlag = false // Debug flag set via command line
)
-48
View File
@@ -1,48 +0,0 @@
/*
* Minio Client (C) 2014, 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import "github.com/minio/cli"
// Help command.
var helpCmd = cli.Command{
Name: "help",
Usage: "Show help.",
Action: mainHelp,
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} [COMMAND]
EXAMPLES:
1. Show mc usage, commands and global flags.
$ mc {{.Name}}
2. Show help for a command.
$ mc {{.Name}} share
`,
}
// main for help command.
func mainHelp(ctx *cli.Context) {
if ctx.Args().First() != "" { // command help.
cli.ShowCommandHelp(ctx, ctx.Args().First())
} else { // mc help.
cli.ShowAppHelp(ctx)
}
}
+8 -13
View File
@@ -31,6 +31,10 @@ var (
Name: "incomplete, I",
Usage: "Remove incomplete uploads.",
}
lsFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of ls",
}
)
// list files and folders.
@@ -38,7 +42,7 @@ var lsCmd = cli.Command{
Name: "ls",
Usage: "List files and folders.",
Action: mainList,
Flags: []cli.Flag{lsFlagIncomplete},
Flags: []cli.Flag{lsFlagIncomplete, lsFlagHelp},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
@@ -64,11 +68,7 @@ EXAMPLES:
5. List folders with space separated names on Amazon S3 cloud storage.
$ mc {{.Name}} 's3/miniocloud/Community Files/'
6. Behave like operating system ‘ls’ tool. Useful for aliasing.
$ alias ls='mc --mimic ls'
$ {{.Name}}
7. List incomplete (previously failed) uploads of objects on Amazon S3.
6. List incomplete (previously failed) uploads of objects on Amazon S3.
$ mc {{.Name}} --incomplete s3/mybucket
`,
}
@@ -77,13 +77,8 @@ EXAMPLES:
func checkListSyntax(ctx *cli.Context) {
args := ctx.Args()
if !ctx.Args().Present() {
if globalMimicFlag {
args = []string{"."}
} else {
cli.ShowCommandHelpAndExit(ctx, "ls", 1) // last argument is exit code
}
args = []string{"."}
}
for _, arg := range args {
if strings.TrimSpace(arg) == "" {
fatalIf(errInvalidArgument().Trace(), "Unable to validate empty argument.")
@@ -106,7 +101,7 @@ func mainList(ctx *cli.Context) {
isIncomplete := ctx.Bool("incomplete")
// mimic operating system tool behavior
if globalMimicFlag && !ctx.Args().Present() {
if !ctx.Args().Present() {
args = []string{"."}
}
-5
View File
@@ -105,7 +105,6 @@ func getSystemData() map[string]string {
func registerBefore(ctx *cli.Context) error {
setMcConfigDir(ctx.GlobalString("config-folder"))
globalQuietFlag = ctx.GlobalBool("quiet")
globalMimicFlag = ctx.GlobalBool("mimic")
globalDebugFlag = ctx.GlobalBool("debug")
globalJSONFlag = ctx.GlobalBool("json")
@@ -139,7 +138,6 @@ func findClosestCommands(command string) []string {
func registerApp() *cli.App {
// Register all the commands (refer flags.go)
registerCmd(helpCmd) // List contents of a bucket.
registerCmd(lsCmd) // List contents of a bucket.
registerCmd(mbCmd) // Make a bucket.
registerCmd(catCmd) // Display contents of a file.
@@ -158,15 +156,12 @@ func registerApp() *cli.App {
// register all the flags (refer flags.go)
registerFlag(configFlag) // Path to configuration folder.
registerFlag(quietFlag) // Suppress chatty console output.
registerFlag(mimicFlag) // Behave like operating system tools. Use with shell aliases.
registerFlag(jsonFlag) // Enable json formatted output.
registerFlag(debugFlag) // Enable debugging output.
registerFlag(colorsFlag) // Choose different styles of console coloring.
app := cli.NewApp()
app.Usage = "Minio Client for cloud storage and filesystems."
// hide --version flag, version is a command
app.HideVersion = true
app.Commands = commands
app.Flags = flags
app.Author = "Minio.io"
+11
View File
@@ -26,17 +26,28 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
mbFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of mb",
}
)
// make a bucket or folder.
var mbCmd = cli.Command{
Name: "mb",
Usage: "Make a bucket or folder.",
Action: mainMakeBucket,
Flags: []cli.Flag{mbFlagHelp},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} TARGET [TARGET ...]
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Create a bucket on Amazon S3 cloud storage.
$ mc {{.Name}} https://s3.amazonaws.com/mynewbucket
+5 -1
View File
@@ -39,6 +39,10 @@ var (
Name: "force",
Usage: "Force overwrite of an existing target(s).",
}
mirrorFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of mirror",
}
)
// Mirror folders recursively from a single source to many destinations
@@ -46,7 +50,7 @@ var mirrorCmd = cli.Command{
Name: "mirror",
Usage: "Mirror folders recursively from a single source to many destinations.",
Action: mainMirror,
Flags: []cli.Flag{mirrorFlagForce},
Flags: []cli.Flag{mirrorFlagForce, mirrorFlagHelp},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
+11
View File
@@ -24,17 +24,28 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
pipeFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of pipe",
}
)
// Display contents of a file.
var pipeCmd = cli.Command{
Name: "pipe",
Usage: "Write contents of stdin to one or more targets. Pipe is the opposite of cat command.",
Action: mainPipe,
Flags: []cli.Flag{pipeFlagHelp},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} TARGET [TARGET...]
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Write contents of stdin to an object on Amazon S3 cloud storage.
$ mc {{.Name}} https://s3.amazonaws.com/personalbuck/meeting-notes.txt
+6 -1
View File
@@ -39,6 +39,11 @@ var (
Name: "incomplete, I",
Usage: "Remove an incomplete upload(s).",
}
rmFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of rm",
}
)
// remove a file or folder.
@@ -46,7 +51,7 @@ var rmCmd = cli.Command{
Name: "rm",
Usage: "Remove file or bucket [WARNING: Use with care].",
Action: mainRm,
Flags: []cli.Flag{rmFlagForce, rmFlagIncomplete},
Flags: []cli.Flag{rmFlagForce, rmFlagIncomplete, rmFlagHelp},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
+11
View File
@@ -28,11 +28,19 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
sessionFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of session",
}
)
// Manage sessions for cp and mirror.
var sessionCmd = cli.Command{
Name: "session",
Usage: "Manage saved sessions of cp and mirror operations.",
Action: mainSession,
Flags: []cli.Flag{sessionFlagHelp},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
@@ -44,6 +52,9 @@ USAGE:
SESSION-ID:
SESSION - Session can either be $SESSION-ID or "all".
FLAGS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. List sessions.
$ mc {{.Name}} list
+8 -1
View File
@@ -25,12 +25,19 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
shareFlagDownloadHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of share download",
}
)
// Share documents via URL.
var shareDownload = cli.Command{
Name: "download",
Usage: "Generate URLs for download access.",
Action: mainShareDownload,
Flags: []cli.Flag{shareFlagExpire},
Flags: []cli.Flag{shareFlagExpire, shareFlagDownloadHelp},
CustomHelpTemplate: `NAME:
mc share {{.Name}} - {{.Usage}}
+1
View File
@@ -31,6 +31,7 @@ var shareList = cli.Command{
Action: mainShareList,
CustomHelpTemplate: `NAME:
mc share {{.Name}} COMMAND - {{.Usage}}
COMMAND:
upload: list previously shared access to uploads.
download: list previously shared access to downloads.
+16 -5
View File
@@ -25,25 +25,36 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
shareFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of share",
}
)
// Share documents via URL.
var shareCmd = cli.Command{
Name: "share",
Usage: "Generate URL for sharing.",
Action: mainShare,
Flags: []cli.Flag{shareFlagHelp},
Subcommands: []cli.Command{
shareDownload,
shareUpload,
shareList,
},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
{{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}} [COMMAND]
{{.Name}} [COMMAND]
COMMANDS:
{{range .Subcommands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
FLAGS:
{{range .Flags}}{{.}}
{{end}}
COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}
`,
}
@@ -68,7 +79,7 @@ func mainShare(ctx *cli.Context) {
if ctx.Args().First() != "" { // command help.
cli.ShowCommandHelp(ctx, ctx.Args().First())
} else { // mc help.
cli.ShowSubcommandHelp(ctx)
cli.ShowAppHelp(ctx)
}
// sub-commands like "upload" and "download" have their own main.
+8 -1
View File
@@ -27,12 +27,19 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
shareFlagUploadHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help of share download",
}
)
// Share documents via URL.
var shareUpload = cli.Command{
Name: "upload",
Usage: "Generate ‘curl’ command to upload objects without requiring access/secret keys.",
Action: mainShareUpload,
Flags: []cli.Flag{shareFlagExpire, shareFlagContentType},
Flags: []cli.Flag{shareFlagExpire, shareFlagContentType, shareFlagUploadHelp},
CustomHelpTemplate: `NAME:
mc share {{.Name}} - {{.Usage}}
+6 -1
View File
@@ -34,6 +34,11 @@ var (
Name: "experimental, E",
Usage: "Check experimental update.",
}
updateFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help for update.",
}
)
// Check for new software updates.
@@ -41,7 +46,7 @@ var updateCmd = cli.Command{
Name: "update",
Usage: "Check for a new software update.",
Action: mainUpdate,
Flags: []cli.Flag{updateFlagExperimental},
Flags: []cli.Flag{updateFlagExperimental, updateFlagHelp},
CustomHelpTemplate: `Name:
mc {{.Name}} - {{.Usage}}
+2 -33
View File
@@ -31,10 +31,6 @@ type App struct {
Flags []Flag
// Boolean to enable bash completion commands
EnableBashCompletion bool
// Boolean to hide built-in help command
HideHelp bool
// Boolean to hide built-in version flag
HideVersion bool
// An action to execute when the bash-completion flag is set
BashComplete func(context *Context)
// An action to execute before any subcommands are run, but after the context is ready
@@ -85,8 +81,8 @@ func NewApp() *App {
Name: os.Args[0],
Usage: "A new cli application",
Version: "0.0.0",
BashComplete: DefaultAppComplete,
Action: helpCommand.Action,
BashComplete: DefaultAppComplete,
Compiled: mustCompileTime(),
Writer: os.Stdout,
}
@@ -119,10 +115,6 @@ func (a *App) getNewContext(arguments []string) (*Context, error) {
// Run - Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
func (a *App) Run(arguments []string) (err error) {
// Comment this out as its not going to be used
// if a.Author != "" || a.Email != "" {
// a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
//}
if HelpPrinter == nil {
defer func() {
HelpPrinter = nil
@@ -152,23 +144,11 @@ func (a *App) Run(arguments []string) (err error) {
}
}
// append help to commands
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
a.Commands = append(a.Commands, helpCommand)
if (HelpFlag != BoolFlag{}) {
a.appendFlag(HelpFlag)
}
}
//append version/help flags
// append version/help flags
if a.EnableBashCompletion {
a.appendFlag(BashCompletionFlag)
}
if !a.HideVersion {
a.appendFlag(VersionFlag)
}
context, err := a.getNewContext(arguments)
if err != nil {
return err
@@ -217,16 +197,6 @@ func (a *App) RunAndExitOnError() {
// RunAsSubcommand - Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
// append help to commands
if len(a.Commands) > 0 {
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
a.Commands = append(a.Commands, helpCommand)
if (HelpFlag != BoolFlag{}) {
a.appendFlag(HelpFlag)
}
}
}
// append flags
if a.EnableBashCompletion {
a.appendFlag(BashCompletionFlag)
@@ -300,7 +270,6 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
// Run default Action
a.Action(context)
return nil
}
-88
View File
@@ -1,7 +1,6 @@
package cli_test
import (
"flag"
"fmt"
"os"
"testing"
@@ -66,38 +65,6 @@ func ExampleAppSubcommand() {
// Hello, Jeremy
}
func ExampleAppHelp() {
// set args for examples sake
os.Args = []string{"greet", "h", "describeit"}
app := cli.NewApp()
app.Name = "greet"
app.Flags = []cli.Flag{
cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
}
app.Commands = []cli.Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
Action: func(c *cli.Context) {
fmt.Printf("i like to describe things")
},
},
}
app.Run(os.Args)
// Output:
// NAME:
// describeit - use it to see a description
//
// USAGE:
// command describeit [arguments...]
//
// DESCRIPTION:
// This is how we describe describeit the function
}
func TestApp_Run(t *testing.T) {
s := ""
@@ -477,61 +444,6 @@ func TestApp_AfterFunc(t *testing.T) {
}
}
func TestAppNoHelpFlag(t *testing.T) {
oldFlag := cli.HelpFlag
defer func() {
cli.HelpFlag = oldFlag
}()
cli.HelpFlag = cli.BoolFlag{}
app := cli.NewApp()
err := app.Run([]string{"test", "-h"})
if err != flag.ErrHelp {
t.Errorf("expected error about missing help flag, but got: %s (%T)", err, err)
}
}
func TestAppHelpPrinter(t *testing.T) {
oldPrinter := cli.HelpPrinter
defer func() {
cli.HelpPrinter = oldPrinter
}()
var wasCalled = false
cli.HelpPrinter = func(template string, data interface{}) {
wasCalled = true
}
app := cli.NewApp()
app.Run([]string{"-h"})
if wasCalled == false {
t.Errorf("Help printer expected to be called, but was not")
}
}
func TestAppVersionPrinter(t *testing.T) {
oldPrinter := cli.VersionPrinter
defer func() {
cli.VersionPrinter = oldPrinter
}()
var wasCalled = false
cli.VersionPrinter = func(c *cli.Context) {
wasCalled = true
}
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
cli.ShowVersion(ctx)
if wasCalled == false {
t.Errorf("Version printer expected to be called, but was not")
}
}
func TestAppCommandNotFound(t *testing.T) {
beforeRun, subcommandRun := false, false
app := cli.NewApp()
-14
View File
@@ -34,8 +34,6 @@ type Command struct {
Flags []Flag
// Treat all flags as normal arguments if true
SkipFlagParsing bool
// Boolean to hide built-in help command
HideHelp bool
// Boolean to hide this command from help or completion
Hide bool
// CustomHelpTemplate the text template for the command help topic.
@@ -50,14 +48,6 @@ func (c Command) Run(ctx *Context) error {
return c.startApp(ctx)
}
if !c.HideHelp && (HelpFlag != BoolFlag{}) {
// append help to flags
c.Flags = append(
c.Flags,
HelpFlag,
)
}
if ctx.App.EnableBashCompletion {
c.Flags = append(c.Flags, BashCompletionFlag)
}
@@ -162,14 +152,11 @@ func (c Command) startApp(ctx *Context) error {
// set CommandNotFound
app.CommandNotFound = ctx.App.CommandNotFound
app.CustomAppHelpTemplate = c.CustomHelpTemplate
app.HideVersion = ctx.App.HideVersion
// set the flags and commands
app.Commands = c.Subcommands
app.Flags = c.Flags
app.HideHelp = c.HideHelp
// bash completion
app.EnableBashCompletion = ctx.App.EnableBashCompletion
@@ -185,6 +172,5 @@ func (c Command) startApp(ctx *Context) error {
} else {
app.Action = helpSubcommand.Action
}
return app.RunAsSubcommand(ctx)
}
-15
View File
@@ -14,21 +14,6 @@ var BashCompletionFlag = BoolFlag{
Hide: true,
}
// VersionFlag - This flag prints the version for the application
var VersionFlag = BoolFlag{
Name: "version, v",
Usage: "print the version",
}
// HelpFlay - This flag prints the help for all commands and subcommands
// Set to the zero value (BoolFlag{}) to disable flag -- keeps subcommand
// unless HideHelp is set to true) it is hidden by default
var HelpFlag = BoolFlag{
Name: "help, h",
Usage: "show help",
Hide: true,
}
// Flag is a common interface related to parsing flags in cli.
// For more advanced flag parsing techniques, it is recomended that
// this interface be implemented.
-144
View File
@@ -10,39 +10,15 @@ import (
"github.com/minio/cli"
)
var boolFlagTests = []struct {
name string
expected string
}{
{"help", "--help\t"},
{"h", "-h\t"},
}
func TestBoolFlagHelpOutput(t *testing.T) {
for _, test := range boolFlagTests {
flag := cli.BoolFlag{Name: test.name}
output := flag.String()
if output != test.expected {
t.Errorf("%s does not match %s", output, test.expected)
}
}
}
var stringFlagTests = []struct {
name string
value string
expected string
}{
{"help", "", "--help \t"},
{"h", "", "-h \t"},
{"h", "", "-h \t"},
{"test", "Something", "--test \"Something\"\t"},
}
func TestStringFlagHelpOutput(t *testing.T) {
for _, test := range stringFlagTests {
flag := cli.StringFlag{Name: test.name, Value: test.value}
output := flag.String()
@@ -71,21 +47,6 @@ var stringSliceFlagTests = []struct {
value *cli.StringSlice
expected string
}{
{"help", func() *cli.StringSlice {
s := &cli.StringSlice{}
s.Set("")
return s
}(), "--help [--help option --help option]\t"},
{"h", func() *cli.StringSlice {
s := &cli.StringSlice{}
s.Set("")
return s
}(), "-h [-h option -h option]\t"},
{"h", func() *cli.StringSlice {
s := &cli.StringSlice{}
s.Set("")
return s
}(), "-h [-h option -h option]\t"},
{"test", func() *cli.StringSlice {
s := &cli.StringSlice{}
s.Set("Something")
@@ -94,7 +55,6 @@ var stringSliceFlagTests = []struct {
}
func TestStringSliceFlagHelpOutput(t *testing.T) {
for _, test := range stringSliceFlagTests {
flag := cli.StringSliceFlag{Name: test.name, Value: test.value}
output := flag.String()
@@ -118,80 +78,11 @@ func TestStringSliceFlagWithEnvVarHelpOutput(t *testing.T) {
}
}
var intFlagTests = []struct {
name string
expected string
}{
{"help", "--help \"0\"\t"},
{"h", "-h \"0\"\t"},
}
func TestIntFlagHelpOutput(t *testing.T) {
for _, test := range intFlagTests {
flag := cli.IntFlag{Name: test.name}
output := flag.String()
if output != test.expected {
t.Errorf("%s does not match %s", output, test.expected)
}
}
}
func TestIntFlagWithEnvVarHelpOutput(t *testing.T) {
os.Clearenv()
os.Setenv("APP_BAR", "2")
for _, test := range intFlagTests {
flag := cli.IntFlag{Name: test.name, EnvVar: "APP_BAR"}
output := flag.String()
if !strings.HasSuffix(output, " [$APP_BAR]") {
t.Errorf("%s does not end with [$APP_BAR]", output)
}
}
}
var durationFlagTests = []struct {
name string
expected string
}{
{"help", "--help \"0\"\t"},
{"h", "-h \"0\"\t"},
}
func TestDurationFlagHelpOutput(t *testing.T) {
for _, test := range durationFlagTests {
flag := cli.DurationFlag{Name: test.name}
output := flag.String()
if output != test.expected {
t.Errorf("%s does not match %s", output, test.expected)
}
}
}
func TestDurationFlagWithEnvVarHelpOutput(t *testing.T) {
os.Clearenv()
os.Setenv("APP_BAR", "2h3m6s")
for _, test := range durationFlagTests {
flag := cli.DurationFlag{Name: test.name, EnvVar: "APP_BAR"}
output := flag.String()
if !strings.HasSuffix(output, " [$APP_BAR]") {
t.Errorf("%s does not end with [$APP_BAR]", output)
}
}
}
var intSliceFlagTests = []struct {
name string
value *cli.IntSlice
expected string
}{
{"help", &cli.IntSlice{}, "--help [--help option --help option]\t"},
{"h", &cli.IntSlice{}, "-h [-h option -h option]\t"},
{"h", &cli.IntSlice{}, "-h [-h option -h option]\t"},
{"test", func() *cli.IntSlice {
i := &cli.IntSlice{}
i.Set("9")
@@ -200,7 +91,6 @@ var intSliceFlagTests = []struct {
}
func TestIntSliceFlagHelpOutput(t *testing.T) {
for _, test := range intSliceFlagTests {
flag := cli.IntSliceFlag{Name: test.name, Value: test.value}
output := flag.String()
@@ -224,39 +114,6 @@ func TestIntSliceFlagWithEnvVarHelpOutput(t *testing.T) {
}
}
var float64FlagTests = []struct {
name string
expected string
}{
{"help", "--help \"0\"\t"},
{"h", "-h \"0\"\t"},
}
func TestFloat64FlagHelpOutput(t *testing.T) {
for _, test := range float64FlagTests {
flag := cli.Float64Flag{Name: test.name}
output := flag.String()
if output != test.expected {
t.Errorf("%s does not match %s", output, test.expected)
}
}
}
func TestFloat64FlagWithEnvVarHelpOutput(t *testing.T) {
os.Clearenv()
os.Setenv("APP_BAZ", "99.4")
for _, test := range float64FlagTests {
flag := cli.Float64Flag{Name: test.name, EnvVar: "APP_BAZ"}
output := flag.String()
if !strings.HasSuffix(output, " [$APP_BAZ]") {
t.Errorf("%s does not end with [$APP_BAZ]", output)
}
}
}
var genericFlagTests = []struct {
name string
value cli.Generic
@@ -267,7 +124,6 @@ var genericFlagTests = []struct {
}
func TestGenericFlagHelpOutput(t *testing.T) {
for _, test := range genericFlagTests {
flag := cli.GenericFlag{Name: test.name, Value: test.value, Usage: "test flag"}
output := flag.String()
+5 -1
View File
@@ -160,7 +160,11 @@ func ShowCommandHelp(c *Context, command string) {
app.Commands = append(app.Commands, command)
}
}
HelpPrinter(DefaultSubcommandHelpTemplate, app)
if app.CustomAppHelpTemplate != "" {
HelpPrinter(app.CustomAppHelpTemplate, app)
} else {
HelpPrinter(DefaultSubcommandHelpTemplate, app)
}
return
}
+2 -2
View File
@@ -24,8 +24,8 @@
},
{
"path": "github.com/minio/cli",
"revision": "deb83e228f8fd55ca51942e0de0ec09665b3f63c",
"revisionTime": "2015-11-06T16:14:14-08:00"
"revision": "df3a039fccb1829c39db22b5d3b0fd10effbac2d",
"revisionTime": "2015-11-15T19:40:41-08:00"
},
{
"path": "github.com/minio/minio-go",
+12
View File
@@ -26,16 +26,28 @@ import (
"github.com/minio/minio-xl/pkg/probe"
)
var (
versionFlagHelp = cli.BoolFlag{
Name: "help, h",
Usage: "Help for version.",
}
)
// Print version.
var versionCmd = cli.Command{
Name: "version",
Usage: "Print version.",
Action: mainVersion,
Flags: []cli.Flag{versionFlagHelp},
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
USAGE:
mc {{.Name}}
FLAGS:
{{range .Flags}}{{.}}
{{end}}
`,
}