Skip to content

Commit

Permalink
Add help menu
Browse files Browse the repository at this point in the history
Signed-off-by: dusanb <[email protected]>
  • Loading branch information
dusanb committed Nov 27, 2020
1 parent f100834 commit ddbcb8a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ When running, please provide following format:

| Flag | Description | Default value |
| ---- | ---------------------------------------------- | ---------------- |
| o | observe option - only valid with Get request | false |
| o | observe option - only valid with GET request | false |
| auth | auth option sent as URI Query | "" |
| h | host | "localhost" |
| p | port | "5683" |
Expand Down
38 changes: 33 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ const (
delete = "DELETE"
)

const (
helpCmd = `Use "coap-cli --help" for help.`
helpMsg = `
Usage: coap-cli <method> <URL> [options]
mathod: get, put, post or delete
-o observe option - only valid with GET request (default: false)
-auth auth option sent as URI Query (default: "")
-h host (default: "localhost")
-p port (default: "5683")
-d data to be sent in POST or PUT (default: "")
-cf content format (default: 50 - JSON format))
Examples:
coap-cli get channels/0bb5ba61-a66e-4972-bab6-26f19962678f/messages/subtopic -auth 1e1017e6-dee7-45b4-8a13-00e6afeb66eb -o
coap-cli post channels/0bb5ba61-a66e-4972-bab6-26f19962678f/messages/subtopic -auth 1e1017e6-dee7-45b4-8a13-00e6afeb66eb -d "hello world"
coap-cli post channels/0bb5ba61-a66e-4972-bab6-26f19962678f/messages/subtopic -auth 1e1017e6-dee7-45b4-8a13-00e6afeb66eb -d "hello world" -h 0.0.0.0 -p 1234
`
)

func parseCode(code string) (codes.Code, error) {
switch code {
case get:
Expand All @@ -47,26 +66,35 @@ func printMsg(m *pool.Message) {

func main() {
if len(os.Args) < 2 {
log.Fatal("Message code must be GET, PUT, POST or DELETE")
log.Fatal(helpCmd)
}
help := strings.ToLower(os.Args[1])
if help == "-h" || help == "--help" {
log.Println(helpMsg)
os.Exit(0)
}

code, err := parseCode(strings.ToUpper(os.Args[1]))
if err != nil {
log.Fatal("error: ", err)
log.Fatalf("Can't read request code: %s\n%s", err, helpCmd)
}

if len(os.Args) < 3 {
log.Fatal("Please enter valid CoAP URL")
log.Fatalf("CoAP URL must not be empty.\n%s", helpCmd)
}
path := os.Args[2]
os.Args = os.Args[2:]
if strings.HasPrefix(path, "-") {
log.Fatalf("Please enter a valid CoAP URL.\n%s", helpCmd)
}

os.Args = os.Args[2:]
o := flag.Bool("o", false, "Observe")
h := flag.String("h", "localhost", "Host")
p := flag.String("p", "5683", "Port")
// Default type is JSON.
cf := flag.Int("q", 50, "Content format")
d := flag.String("d", "", "Message data")
a := flag.String("auth", "", "Auth token")

flag.Parse()

client, err := coap.New(*h + ":" + *p)
Expand Down

0 comments on commit ddbcb8a

Please sign in to comment.