-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also improve the documentation somewhat and add more examples.
- Loading branch information
Sam Whited
committed
Sep 4, 2017
1 parent
fcf5af9
commit f671aa3
Showing
6 changed files
with
171 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2017 The Mellium Authors. | ||
// Use of this source code is governed by the BSD 2-clause license that can be | ||
// found in the LICENSE file. | ||
|
||
package cli_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"mellium.im/cli" | ||
) | ||
|
||
// Returns a help article about the config file format. | ||
func articleHelp() *cli.Command { | ||
return &cli.Command{ | ||
Usage: `article`, | ||
Description: `Help article about help articles. | ||
Help articles are "commands" that do not provide any functionality. They | ||
only exist so that their description can be shown using the help command | ||
(or your own help system): | ||
$ ./yourcmd help articlename`, | ||
} | ||
} | ||
|
||
func Example_articles() { | ||
cmds := &cli.CommandSet{ | ||
Name: "git", | ||
Commands: []*cli.Command{ | ||
commitCmd(""), | ||
articleHelp(), | ||
}, | ||
} | ||
cmds.Commands = append(cmds.Commands, cli.Help(cmds)) | ||
fmt.Println("$ git help") | ||
cmds.Run("help") | ||
|
||
fmt.Println("$ git help article") | ||
cmds.Run("help", "article") | ||
|
||
// Output: | ||
// $ git help | ||
// Usage of git: | ||
// | ||
// git [options] command | ||
// | ||
// Commands: | ||
// | ||
// commit Records changes to the repository. | ||
// help Print articles and detailed information about subcommands. | ||
// | ||
// Articles: | ||
// | ||
// article Help article about help articles. | ||
// $ git help article | ||
// | ||
// Help article about help articles. | ||
// | ||
// Help articles are "commands" that do not provide any functionality. They | ||
// only exist so that their description can be shown using the help command | ||
// (or your own help system): | ||
// | ||
// $ ./yourcmd help articlename | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2017 The Mellium Authors. | ||
// Use of this source code is governed by the BSD 2-clause license that can be | ||
// found in the LICENSE file. | ||
|
||
package cli_test | ||
|
||
import ( | ||
"mellium.im/cli" | ||
) | ||
|
||
func ExampleHelp() { | ||
cmds := &cli.CommandSet{ | ||
Name: "git", | ||
} | ||
cmds.Commands = []*cli.Command{ | ||
commitCmd(""), | ||
cli.Help(cmds), | ||
} | ||
cmds.Run("help") | ||
|
||
// Output: | ||
// Usage of git: | ||
// | ||
// git [options] command | ||
// | ||
// Commands: | ||
// | ||
// commit Records changes to the repository. | ||
// help Print articles and detailed information about subcommands. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2017 The Mellium Authors. | ||
// Use of this source code is governed by the BSD 2-clause license that can be | ||
// found in the LICENSE file. | ||
|
||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// Help returns a Command that prints help information about its command set to | ||
// stdout, or about a specific command if one is provided as an argument. | ||
// | ||
// For example, in a program called "git" running: | ||
// | ||
// git help commit | ||
// | ||
// would print information about the "commit" subcommand. | ||
func Help(cs *CommandSet) *Command { | ||
return &Command{ | ||
Usage: "help [command]", | ||
Description: `Print articles and detailed information about subcommands.`, | ||
Run: func(c *Command, args ...string) error { | ||
// If there aren't any arguments, print the main command help. | ||
if len(args) == 0 { | ||
cs.Help(os.Stdout) | ||
return nil | ||
} | ||
|
||
// Print the help for the provided subcommand or help topic. | ||
for _, cmd := range cs.Commands { | ||
if cmd.Name() != args[0] { | ||
continue | ||
} | ||
cmd.Help(os.Stdout) | ||
return nil | ||
} | ||
return fmt.Errorf("No such help topic %s\n", args[0]) | ||
}, | ||
} | ||
} |