From 5384d8e2dae2f154d67e9744c75b243b8cb76fa3 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Wed, 2 May 2018 22:46:42 -0700 Subject: [PATCH] add help option to cli app --- app/cli/main.go | 70 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/app/cli/main.go b/app/cli/main.go index 5d08528..b641df7 100644 --- a/app/cli/main.go +++ b/app/cli/main.go @@ -12,28 +12,46 @@ import ( var app app2.Application +var suggestions = []prompt.Suggest{ + {Text: "new-profile", Description: "create a new profile"}, + {Text: "load-profile", Description: "load a new profile"}, + {Text: "quit", Description: "quit cwtch"}, + {Text: "servers", Description: "retrieve a list of servers and their connection status"}, + {Text: "peers", Description: "retrieve a list of peers and their connection status"}, + {Text: "contacts", Description: "retrieve a list of contacts"}, + {Text: "groups", Description: "retrieve a list of groups"}, + {Text: "send", Description: "send a message to a group"}, + {Text: "timeline", Description: "read the timeline of a given group"}, + {Text: "accept-invite", Description: "accept the invite of a group"}, + {Text: "invite", Description: "invite a new contact"}, + {Text: "invite-to-group", Description: "invite an existing contact to join an existing group"}, + {Text: "new-group", Description: "create a new group"}, + {Text: "help", Description: "print list of commands"}, +} + +var usages = map[string]string { + "new-profile": "newprofile [name] [filename]", + "load-profile": "loadprofile [filename]", + "quit": "", + "servers": "", + "peers": "", + "contacts": "", + "groups": "", + "send": "send [groupid] [message]", + "timeline": "timeline [groupid]", + "accept-invite": "accept-invite [groupid]", + "invite": "invite [onion]", + "invite-to-group": "invite-to-group [onion] [groupid]", + "new-group": "new-group [server]", + "help": "", +} func completer(d prompt.Document) []prompt.Suggest { s := []prompt.Suggest{} if d.FindStartOfPreviousWord() == 0 { - s := []prompt.Suggest{ - {Text: "new-profile", Description: "create a new profile"}, - {Text: "load-profile", Description: "load a new profile"}, - {Text: "quit", Description: "quit cwtch"}, - {Text: "servers", Description: "retrieve a list of servers and their connection status"}, - {Text: "peers", Description: "retrieve a list of peers and their connection status"}, - {Text: "contacts", Description: "retrieve a list of contacts"}, - {Text: "groups", Description: "retrieve a list of groups"}, - {Text: "send", Description: "send a message to a group"}, - {Text: "timeline", Description: "read the timeline of a given group"}, - {Text: "accept-invite", Description: "accept the invite of a group"}, - {Text: "invite", Description: "invite a new contact"}, - {Text: "invite-to-group", Description: "invite an existing contact to join an existing group"}, - {Text: "new-group", Description: "create a new group"}, - } - return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true) + return prompt.FilterHasPrefix(suggestions, d.GetWordBeforeCursor(), true) } w := d.CurrentLine() @@ -154,7 +172,7 @@ func main() { fmt.Printf("Error creating profile for %v: %v\n", commands[1], err) } } else { - fmt.Printf("Error creating NewProfile, usage: newprofile [name] [filename]\n") + fmt.Printf("Error creating NewProfile, usage: %s\n", usages["new-profile"]) } case "load-profile": if len(commands) == 2 { @@ -166,7 +184,7 @@ func main() { fmt.Printf("Error loading profile for %v: %v\n", commands[1], err) } } else { - fmt.Printf("Error Loading profile, usage: loadprofile [filename]\n") + fmt.Printf("Error Loading profile, usage: %s\n", usages["load-profile"]) } case "info": @@ -180,7 +198,7 @@ func main() { fmt.Printf("Inviting cwtch:%v\n", commands[1]) app.PeerRequest(commands[1]) } else { - fmt.Printf("Error inviting peer, usage: invite [onion]\n") + fmt.Printf("Error inviting peer, usage: %s\n", usages["invite"]) } case "peers": peers := app.Peer.GetPeers() @@ -208,7 +226,7 @@ func main() { fmt.Printf("Error: %v\n", err) } } else { - fmt.Printf("Error accepting invite, usage: accept-invite [groupid]\n") + fmt.Printf("Error accepting invite, usage: %s\n", usages["accept-invite"]) } case "invite-to-group": if len(commands) == 3 { @@ -218,7 +236,7 @@ func main() { fmt.Printf("Error: %v\n", err) } } else { - fmt.Printf("Error inviting peer to group, usage: invitetogroup [onion] [groupid]\n") + fmt.Printf("Error inviting peer to group, usage: %s\n", usages["invite-to-group"]) } case "new-group": if len(commands) == 2 { @@ -227,7 +245,7 @@ func main() { fmt.Printf("New Group [%v] created for server %v\n", id, commands[1]) app.Peer.Save(profilefile) } else { - fmt.Printf("Error inviting peer, usage: newgroup [server]\n") + fmt.Printf("Error inviting peer, usage: %s\n", usages["new-group"]) } case "send": if len(commands) > 2 { @@ -237,7 +255,7 @@ func main() { fmt.Printf("Error: %v\n", err) } } else { - fmt.Printf("Error sending message to group, usage: send [groupid] [message]\n") + fmt.Printf("Error sending message to group, usage: %s\n", usages["send"]) } case "timeline": if len(commands) == 2 { @@ -254,10 +272,14 @@ func main() { } } } else { - fmt.Printf("Error reading timeline from group, usage: timeline [groupid]\n") + fmt.Printf("Error reading timeline from group, usage: %s\n", usages["timeline"]) } case "save": app.Peer.Save(profilefile) + case "help": + for _, command := range suggestions { + fmt.Printf("%-18s%-56s%s\n", command.Text, command.Description, usages[command.Text]) + } } }