diff --git a/access-main.go b/access-main.go index 110d56f0..e33cb987 100644 --- a/access-main.go +++ b/access-main.go @@ -91,13 +91,13 @@ func (s accessMessage) JSON() string { } func checkAccessSyntax(ctx *cli.Context) { - if !ctx.Args().Present() || ctx.Args().First() == "help" { + if !ctx.Args().Present() { cli.ShowCommandHelpAndExit(ctx, "access", 1) // last argument is exit code } if len(ctx.Args()) < 2 { cli.ShowCommandHelpAndExit(ctx, "access", 1) // last argument is exit code } - switch ctx.Args().Get(0) { + switch ctx.Args().First() { case "set": if len(ctx.Args().Tail()) < 2 { cli.ShowCommandHelpAndExit(ctx, "access", 1) // last argument is exit code diff --git a/cat-main.go b/cat-main.go index db421716..e5041cbd 100644 --- a/cat-main.go +++ b/cat-main.go @@ -59,7 +59,7 @@ EXAMPLES: // checkCatSyntax performs command-line input validation for cat command. func checkCatSyntax(ctx *cli.Context) { - if (!ctx.Args().Present() && !globalMimicFlag) || ctx.Args().First() == "help" { + if !ctx.Args().Present() && !globalMimicFlag { cli.ShowCommandHelpAndExit(ctx, "cat", 1) // last argument is exit code } diff --git a/config-main.go b/config-main.go index 2e7fdef4..e5ea8894 100644 --- a/config-main.go +++ b/config-main.go @@ -51,7 +51,7 @@ COMMANDS: // mainConfig is the handle for "mc config" command. provides sub-commands which write configuration data in json format to config file. func mainConfig(ctx *cli.Context) { - if !ctx.Args().Present() || ctx.Args().First() == "help" { + if !ctx.Args().Present() { cli.ShowAppHelp(ctx) } } diff --git a/cp-url.go b/cp-url.go index a1d27d89..776215c0 100644 --- a/cp-url.go +++ b/cp-url.go @@ -58,7 +58,7 @@ const ( // C: copy(*, d...) // func checkCopySyntax(ctx *cli.Context) { - if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { + if len(ctx.Args()) < 2 { cli.ShowCommandHelpAndExit(ctx, "cp", 1) // last argument is exit code. } // extract URLs. diff --git a/diff-main.go b/diff-main.go index d5215de4..9755e6fe 100644 --- a/diff-main.go +++ b/diff-main.go @@ -46,7 +46,7 @@ EXAMPLES: } func checkDiffSyntax(ctx *cli.Context) { - if len(ctx.Args()) != 2 || ctx.Args().First() == "help" { + if len(ctx.Args()) != 2 { cli.ShowCommandHelpAndExit(ctx, "diff", 1) // last argument is exit code } for _, arg := range ctx.Args() { diff --git a/help-main.go b/help-main.go new file mode 100644 index 00000000..bac7d088 --- /dev/null +++ b/help-main.go @@ -0,0 +1,58 @@ +/* + * 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 +`, +} + +// Validate command line arguments. +func checkHelpSyntax(ctx *cli.Context) { + if len(ctx.Args()) > 1 { // Accepts a maximum of 1 argument. + exitCode := 1 + cli.ShowCommandHelpAndExit(ctx, "help", exitCode) + } +} + +// main for help command. +func mainHelp(ctx *cli.Context) { + checkHelpSyntax(ctx) + + if ctx.Args().First() != "" { // command help. + cli.ShowCommandHelp(ctx, ctx.Args().First()) + } else { // mc help. + cli.ShowAppHelp(ctx) + } +} diff --git a/ls-main.go b/ls-main.go index 033cd4e8..1588a509 100644 --- a/ls-main.go +++ b/ls-main.go @@ -51,36 +51,25 @@ OPTIONS: EXAMPLES: 1. List buckets on Amazon S3 cloud storage. $ mc {{.Name}} https://s3.amazonaws.com/ - [2015-01-20 15:42:00 PST] 0B rom/ - [2015-01-15 00:05:40 PST] 0B zek/ 2. List buckets and all its contents from Amazon S3 cloud storage recursively. $ mc {{.Name}} https://s3.amazonaws.com/... - 2015-01-15 00:05:40 PST 0B zek/ - 2015-03-31 14:46:33 PDT 55MiB backup.tar.gz - 2015-04-15 20:11:22 PST 0B miniocloud 3. List files recursively on a local filesystem on Microsoft Windows. $ mc {{.Name}} C:\Users\Worf\... - [2015-03-31 14:46:33 PDT] 15.00MiB Gowron\Khitomer Conference Details.pdf - ... 4. List files with non-English characters on Amazon S3 cloud storage. $ mc {{.Name}} s3/andoria/本... - [2015-05-19 17:28:22 PDT] 41B 本語.md 5. List folders with space separated names on Amazon S3 cloud storage. $ mc {{.Name}} 's3/miniocloud/Community Files/' - [2015-05-19 17:28:22 PDT] 41B 本語.md 6. Behave like operating system ‘ls’ tool. Useful for aliasing. $ alias ls='mc --mimic ls' $ {{.Name}} - [2015-05-19 17:28:22 PDT] 41B 本語.md 7. List incomplete (previously failed) uploads of objects on Amazon S3. $ mc {{.Name}} --incomplete s3/mybucket - [2015-10-19 22:28:02 PDT] 41MiB backup/server112-11Nov15.tgz `, } @@ -94,9 +83,7 @@ func checkListSyntax(ctx *cli.Context) { cli.ShowCommandHelpAndExit(ctx, "ls", 1) // last argument is exit code } } - if ctx.Args().First() == "help" { - cli.ShowCommandHelpAndExit(ctx, "ls", 1) // last argument is exit code - } + for _, arg := range args { if strings.TrimSpace(arg) == "" { fatalIf(errInvalidArgument().Trace(), "Unable to validate empty argument.") diff --git a/main.go b/main.go index c7d88db9..48f110b7 100644 --- a/main.go +++ b/main.go @@ -139,6 +139,7 @@ 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. diff --git a/mb-main.go b/mb-main.go index 9f0237ec..5e06f853 100644 --- a/mb-main.go +++ b/mb-main.go @@ -70,7 +70,7 @@ func (s makeBucketMessage) JSON() string { // Validate command line arguments. func checkMakeBucketSyntax(ctx *cli.Context) { - if !ctx.Args().Present() || ctx.Args().First() == "help" { + if !ctx.Args().Present() { cli.ShowCommandHelpAndExit(ctx, "mb", 1) // last argument is exit code } for _, arg := range ctx.Args() { diff --git a/mirror-url.go b/mirror-url.go index fb2bc525..4afb48fb 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -48,7 +48,7 @@ func (m mirrorURLs) isEmpty() bool { // checkMirrorSyntax(URLs []string) func checkMirrorSyntax(ctx *cli.Context) { - if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { + if len(ctx.Args()) < 2 { cli.ShowCommandHelpAndExit(ctx, "mirror", 1) // last argument is exit code. } diff --git a/pipe-main.go b/pipe-main.go index 60d1ac7e..bbbb2030 100644 --- a/pipe-main.go +++ b/pipe-main.go @@ -49,7 +49,7 @@ EXAMPLES: // checkPipeSyntax performs command-line input validation for pipe command. func checkPipeSyntax(ctx *cli.Context) { - if !ctx.Args().Present() || ctx.Args().First() == "help" { + if !ctx.Args().Present() { cli.ShowCommandHelpAndExit(ctx, "pipe", 1) // last argument is exit code } } diff --git a/rm-main.go b/rm-main.go index e1e979d1..f8622934 100644 --- a/rm-main.go +++ b/rm-main.go @@ -104,11 +104,9 @@ func (r rmMessage) JSON() string { // Validate command line arguments. func checkRmSyntax(ctx *cli.Context) { args := ctx.Args() - - ishelp := ctx.GlobalBool("help") isForce := ctx.Bool("force") - if !args.Present() || ishelp { + if !args.Present() { exitCode := 1 cli.ShowCommandHelpAndExit(ctx, "rm", exitCode) } diff --git a/session-main.go b/session-main.go index 7cd273f9..6750af86 100644 --- a/session-main.go +++ b/session-main.go @@ -136,7 +136,7 @@ func sessionExecute(s *sessionV3) { } func checkSessionSyntax(ctx *cli.Context) { - if len(ctx.Args()) < 1 || ctx.Args().First() == "help" { + if len(ctx.Args()) < 1 { cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code } if strings.TrimSpace(ctx.Args().First()) == "" { diff --git a/share-main.go b/share-main.go index c95185c0..24b8fb42 100644 --- a/share-main.go +++ b/share-main.go @@ -42,14 +42,13 @@ USAGE: {{.Name}} command [arguments...] COMMANDS: - {{range .Commands}}{{ .Name }}{{ "\t" }}{{.Usage}} + {{range .Subcommands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}} {{end}} `, } // migrateShare migrate to newest version sequentially func migrateShare() { - if !isShareDirExists() { return } diff --git a/update-main.go b/update-main.go index 56bd1208..594905bd 100644 --- a/update-main.go +++ b/update-main.go @@ -95,7 +95,7 @@ func (u updateMessage) JSON() string { // check valid input arguments. func checkUpdateSyntax(ctx *cli.Context) { - if ctx.Args().First() == "help" || !ctx.Args().Present() { + if !ctx.Args().Present() { cli.ShowCommandHelpAndExit(ctx, "update", 1) // last argument is exit code } arg := strings.TrimSpace(ctx.Args().First()) diff --git a/version-main.go b/version-main.go index a4ed3b98..e279c473 100644 --- a/version-main.go +++ b/version-main.go @@ -41,10 +41,6 @@ USAGE: } func mainVersion(ctx *cli.Context) { - if ctx.Args().First() == "help" { - cli.ShowCommandHelpAndExit(ctx, "version", 1) // last argument is exit code - } - // Additional command speific theme customization. console.SetColor("Version", color.New(color.FgGreen, color.Bold))