2016-02-17 19:30:59 +01:00
|
|
|
// Copyright 2016 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
|
|
|
|
|
// Use of this source code is governed by the MIT-license that can be
|
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
2025-06-14 07:50:42 +02:00
|
|
|
package app
|
2015-06-18 20:48:02 +02:00
|
|
|
|
|
|
|
|
import (
|
2022-02-16 20:58:10 +01:00
|
|
|
"context"
|
2015-06-18 20:48:02 +02:00
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-16 22:40:36 -06:00
|
|
|
var ErrNoCmd = fmt.Errorf("no cmd")
|
2015-06-18 20:48:02 +02:00
|
|
|
|
|
|
|
|
type Command struct {
|
|
|
|
|
Str string
|
|
|
|
|
Aliases []string
|
|
|
|
|
Desc string
|
2025-06-14 07:50:42 +02:00
|
|
|
HandleFunc func(ctx context.Context, app *App, args []string)
|
2015-06-18 20:48:02 +02:00
|
|
|
Usage string
|
|
|
|
|
Options map[string]string
|
|
|
|
|
Example string
|
2015-07-29 19:33:04 +02:00
|
|
|
|
|
|
|
|
LongLived bool
|
|
|
|
|
MayConnect bool
|
2015-06-18 20:48:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cmd Command) PrintUsage() {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "%s - %s\n", cmd.Str, cmd.Desc)
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "\nUsage:\n %s %s\n", cmd.Str, strings.TrimSpace(cmd.Usage))
|
|
|
|
|
|
|
|
|
|
if len(cmd.Options) > 0 {
|
|
|
|
|
fmt.Fprint(os.Stderr, "\nOptions:\n")
|
|
|
|
|
for f, desc := range cmd.Options {
|
|
|
|
|
fmt.Fprintf(os.Stderr, " %-17s %s\n", f, desc)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cmd.Example != "" {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "\nExample:\n %s\n", strings.TrimSpace(cmd.Example))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Fprint(os.Stderr, "\n")
|
|
|
|
|
}
|