Skip to content

Commit

Permalink
Don't list all available commands with descriptions and options in he…
Browse files Browse the repository at this point in the history
…lp screen, just list commands and general opts instead. Equalize "-h" invocation with invocation without any args
  • Loading branch information
disq committed Mar 28, 2018
1 parent 0ca87cf commit c7df42e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ s5cmd ls s3://my-bucket/my-prefix/some-object<tab>
## Usage ##

```
$ ./s5cmd --help
$ ./s5cmd
Usage: ./s5cmd [OPTION]... [COMMAND [PARAMS...]]
Expand Down Expand Up @@ -84,7 +84,10 @@ Usage: ./s5cmd [OPTION]... [COMMAND [PARAMS...]]
-version
Prints current version
To list available commands, run without arguments.
Commands:
!, cp, du, exit, get, ls, mv, rm
To get help on a specific command, run "s5cmd <command> -h"
```

## Commands File ##
Expand Down Expand Up @@ -116,7 +119,7 @@ There are three main commands: `cp`, `mv` and `rm`. Arguments can be either S3 u
- Batch local-to-local `cp` and `mv` commands also accept the `-R` option for recursive operation.
- The `ls` command accepts the `-e` option to show ETags in listing.
- The `du` command only takes S3 arguments (prefix or wildcard)
- `ls` and `du` commands both accept the `-h` option to show human-readable object sizes.
- `ls` and `du` commands both accept the `-H` option to show human-readable object sizes.
- `du` command also accepts the `-g` option to group by storage class.


Expand Down
29 changes: 25 additions & 4 deletions core/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func (c *CommandMap) String(optsOverride ...opt.OptionType) (s string) {
return
}

// GetCommandList returns a text of accepted Commands with their options and arguments, list of accepted options, and a count of command alternates
func GetCommandList(filter string) (string, []opt.OptionType, int) {
// GetCommandHelps returns a text of accepted Commands with their options and arguments, list of accepted options, and a count of command alternates
func GetCommandHelps(filter string) (string, []opt.OptionType, int) {
list := make(map[string][]string)
overrides := map[op.Operation]string{
op.Abort: "exit [exit code]",
Expand Down Expand Up @@ -189,6 +189,27 @@ func GetCommandList(filter string) (string, []opt.OptionType, int) {
return ret, optsUsed, len(list)
}

func PrintUsageLine() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]... [COMMAND [PARAMS...]]\n\n", os.Args[0])
// GetCommandList returns a list of accepted Commands
func GetCommandList() []string {
l := make(map[string]struct{})
for _, c := range Commands {
if c.Operation.IsInternal() {
continue
}
l[c.Keyword] = struct{}{}
}

var list []string

for k := range l {
list = append(list, k)
}
sort.Strings(list)

return list
}

// UsageLine returns the generic usage line for s5cmd
func UsageLine() string {
return fmt.Sprintf("Usage: %s [OPTION]... [COMMAND [PARAMS...]]", os.Args[0])
}
6 changes: 3 additions & 3 deletions core/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ func (j *Job) Run(wp *WorkerParams) error {
//log.Printf("Running %v", j)

if j.opts.Has(opt.Help) {
PrintUsageLine()
fmt.Fprintf(os.Stderr, "%v\n\n", UsageLine())

cl, opts, cnt := GetCommandList(j.command)
cl, opts, cnt := GetCommandHelps(j.command)

if ol := opt.GetOptionHelps(opts); ol != "" {
fmt.Fprintf(os.Stderr, "\"%v\" command options:\n", j.command)
Expand All @@ -180,7 +180,7 @@ func (j *Job) Run(wp *WorkerParams) error {
fmt.Fprintf(os.Stderr, "Help for \"%v\" commands:\n", j.command)
}
fmt.Fprintf(os.Stderr, cl)
fmt.Fprintf(os.Stderr, "\nTo list available general options, run \"%v -h\"\n", os.Args[0])
fmt.Fprint(os.Stderr, "\nTo list available general options, run without arguments.\n")

return ErrDisplayedHelp
}
Expand Down
23 changes: 9 additions & 14 deletions s5cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

"github.com/peakgames/s5cmd/complete"
"github.com/peakgames/s5cmd/core"
"github.com/peakgames/s5cmd/opt"
"github.com/peakgames/s5cmd/stats"
"github.com/peakgames/s5cmd/version"
)
Expand Down Expand Up @@ -74,9 +73,15 @@ func main() {
verbose := flag.Bool("vv", false, "Verbose output")

flag.Usage = func() {
core.PrintUsageLine()
fmt.Fprintf(os.Stderr, "%v\n\n", core.UsageLine())

fmt.Fprint(os.Stderr, "Options:\n")
flag.PrintDefaults()
fmt.Fprint(os.Stderr, "\nTo list available commands, run without arguments.\n")

cl := core.GetCommandList()
fmt.Fprint(os.Stderr, "\nCommands:")
fmt.Fprintf(os.Stderr, "\n %v\n", strings.Join(cl, ", "))
fmt.Fprintf(os.Stderr, "\nTo get help on a specific command, run \"%v <command> -h\"\n", os.Args[0])
}

//flag.Parse()
Expand All @@ -102,17 +107,7 @@ func main() {
}

if flag.Arg(0) == "" && cmdFile == "" {
core.PrintUsageLine()

cl, opts, _ := core.GetCommandList("")

fmt.Fprint(os.Stderr, "Command options:\n")
fmt.Fprintf(os.Stderr, opt.GetOptionHelps(opts))
fmt.Fprint(os.Stderr, "\n\n")

fmt.Fprint(os.Stderr, "Commands:\n")
fmt.Fprintf(os.Stderr, cl)
fmt.Fprint(os.Stderr, "\nTo list available options, run with the -h option.\n")
flag.Usage()
os.Exit(2)
}

Expand Down

0 comments on commit c7df42e

Please sign in to comment.