Skip to content

Commit 783a099

Browse files
committed
cmk: implement command line flags support and usage doc
Signed-off-by: Rohit Yadav <rohit@apache.org>
1 parent 38972f2 commit 783a099

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

cmd/command.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ func AddCommand(cmd *Command) {
5656
func PrintUsage() {
5757
commandHelp := ""
5858
for _, cmd := range commands {
59-
commandHelp += fmt.Sprintf("%s\t\t%s\n", cmd.Name, cmd.Help)
59+
commandHelp += fmt.Sprintf(" %-8s %s\n", cmd.Name, cmd.Help)
6060
}
61-
fmt.Printf(`Usage: cmk [options] [commands]
61+
fmt.Printf(`usage: cmk [flags] [commands|apis] [-h]
6262
6363
CloudMonkey (cmk) 🐵 is a command line interface for Apache CloudStack.
6464
65+
Allowed flags:
66+
-h Show this help message or API doc when specified after an API
67+
-v Print version
68+
-o API response output format: json, text, table, column, csv
69+
-p Server profile
70+
6571
Default commands:
6672
%s
67-
Try cmk [help] or cmk [action api] -h
6873
`, commandHelp)
6974
}

cmd/set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func init() {
4747
}
4848
subCommand := r.Args[0]
4949
value := strings.Join(r.Args[1:], " ")
50-
r.Config.UpdateConfig(subCommand, value)
50+
r.Config.UpdateConfig(subCommand, value, true)
5151

5252
if subCommand == "profile" && r.Config.HasShell {
5353
fmt.Println("Loaded server profile:", r.Config.Core.ProfileName)

cmk.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,44 @@
1818
package main
1919

2020
import (
21+
"flag"
2122
"fmt"
2223
"os"
2324

2425
"github.com/apache/cloudstack-cloudmonkey/cli"
26+
"github.com/apache/cloudstack-cloudmonkey/cmd"
2527
"github.com/apache/cloudstack-cloudmonkey/config"
2628
)
2729

30+
func init() {
31+
flag.Usage = func() {
32+
cmd.PrintUsage()
33+
}
34+
}
35+
2836
func main() {
29-
args := os.Args[1:]
30-
cli.SetConfig(config.NewConfig())
37+
outputFormat := flag.String("o", "", "output format: json, text, table, column, csv")
38+
showVersion := flag.Bool("v", false, "show version")
39+
profile := flag.String("p", "", "server profile")
40+
flag.Parse()
41+
42+
cfg := config.NewConfig()
43+
44+
if *showVersion {
45+
fmt.Println(cfg.Name(), cfg.Version())
46+
os.Exit(0)
47+
}
48+
49+
if *outputFormat != "" {
50+
cfg.UpdateConfig("output", *outputFormat, false)
51+
}
52+
53+
if *profile != "" {
54+
cfg.UpdateConfig("profile", *profile, false)
55+
}
56+
57+
cli.SetConfig(cfg)
58+
args := flag.Args()
3159
if len(args) > 0 {
3260
if err := cli.ExecCmd(args); err != nil {
3361
fmt.Println("🙈 Error:", err)

0 commit comments

Comments
 (0)