• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.travis.ymlH A D21-Oct-201529

README.mdH A D21-Oct-20152.6 KiB

command.goH A D21-Oct-20155.2 KiB

command_test.goH A D21-Oct-20154.8 KiB

README.md

1# command
2
3[![Build Status](https://travis-ci.org/odeke-em/command.png?branch=master)](https://travis-ci.org/odeke-em/command)
4
5command is a tiny package that helps you to add cli subcommands to your Go program with no effort, and prints a pretty guide if needed.
6
7~~~
8Usage: program <command>
9
10where <command> is one of:
11  version   prints the version
12  command1  some description about command1
13  command2  some description about command2
14
15available flags:
16  -exec-path="": a custom path to executable
17
18program <command> -h for subcommand help
19~~~
20
21## Usage
22
23In order to start, go get this repository:
24
25~~~ sh
26go get github.com/odeke-em/command
27~~~
28
29This package allows you to use flags package as you used to do, and provides additional parsing for subcommands and subcommand flags.
30
31~~~ go
32import "github.com/odeke-em/command"
33
34// register any global flags
35var flagExecPath = flag.String("exec-path", "", "a custom path to executable")
36
37type VersionCommand struct{
38	flagVerbose *bool
39}
40
41func (cmd *VersionCommand) Flags(fs *flag.FlagSet) *flag.FlagSet {
42	// define subcommand's flags
43	cmd.flagVerbose = fs.Bool("v", false, "provides verbose output")
44	return fs
45}
46
47func (cmd *VersionCommand) Run(args []string) {
48	// implement the main body of the subcommand here
49  // required and optional arguments are found in args
50}
51
52// register version as a subcommand
53command.On("version", "prints the version", &VersionCommand{}, []string{"<required-arg>"})
54command.On("command1", "some description about command1", ..., []string{})
55command.On("command2", "some description about command2", ..., []string{})
56command.Parse()
57// ...
58command.Run()
59~~~
60
61The program above will handle the registered commands and invoke the matching command's `Run` or print subcommand help if `-h` is set.
62
63~~~
64$ program -exec-path=/home/user/bin/someexec version -v=true history
65~~~
66
67will output the version of the program in a verbose way requring an argument (history), and will set the exec path to the provided path. If arguments doesn't match any subcommand or illegal arguments are provided, it will print the usage guide.
68
69
70## License
71
72Copyright 2013 Google Inc. All Rights Reserved.
73
74Licensed under the Apache License, Version 2.0 (the "License");
75you may not use this file except in compliance with the License.
76You may obtain a copy of the License at
77
78    http://www.apache.org/licenses/LICENSE-2.0
79
80Unless required by applicable law or agreed to in writing, software
81distributed under the License is distributed on an "AS IS" BASIS,
82WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
83See the License for the specific language governing permissions and
84limitations under the License.
85