"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/handlers_meta.go" (28 May 2021, 2170 Bytes) of package /linux/misc/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package main
    2 
    3 import (
    4     "fmt"
    5     "github.com/prasmussen/gdrive/cli"
    6     "os"
    7     "runtime"
    8     "strings"
    9     "text/tabwriter"
   10 )
   11 
   12 func printVersion(ctx cli.Context) {
   13     fmt.Printf("%s: %s\n", Name, Version)
   14     fmt.Printf("Golang: %s\n", runtime.Version())
   15     fmt.Printf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
   16 }
   17 
   18 func printHelp(ctx cli.Context) {
   19     w := new(tabwriter.Writer)
   20     w.Init(os.Stdout, 0, 0, 3, ' ', 0)
   21 
   22     fmt.Fprintf(w, "%s usage:\n\n", Name)
   23 
   24     for _, h := range ctx.Handlers() {
   25         fmt.Fprintf(w, "%s %s\t%s\n", Name, h.Pattern, h.Description)
   26     }
   27 
   28     w.Flush()
   29 }
   30 
   31 func printCommandHelp(ctx cli.Context) {
   32     args := ctx.Args()
   33     printCommandPrefixHelp(ctx, args.String("command"))
   34 }
   35 
   36 func printSubCommandHelp(ctx cli.Context) {
   37     args := ctx.Args()
   38     printCommandPrefixHelp(ctx, args.String("command"), args.String("subcommand"))
   39 }
   40 
   41 func printCommandPrefixHelp(ctx cli.Context, prefix ...string) {
   42     handler := getHandler(ctx.Handlers(), prefix)
   43 
   44     if handler == nil {
   45         ExitF("Command not found")
   46     }
   47 
   48     w := new(tabwriter.Writer)
   49     w.Init(os.Stdout, 0, 0, 3, ' ', 0)
   50 
   51     fmt.Fprintf(w, "%s\n", handler.Description)
   52     fmt.Fprintf(w, "%s %s\n", Name, handler.Pattern)
   53     for _, group := range handler.FlagGroups {
   54         fmt.Fprintf(w, "\n%s:\n", group.Name)
   55         for _, flag := range group.Flags {
   56             boolFlag, isBool := flag.(cli.BoolFlag)
   57             if isBool && boolFlag.OmitValue {
   58                 fmt.Fprintf(w, "  %s\t%s\n", strings.Join(flag.GetPatterns(), ", "), flag.GetDescription())
   59             } else {
   60                 fmt.Fprintf(w, "  %s <%s>\t%s\n", strings.Join(flag.GetPatterns(), ", "), flag.GetName(), flag.GetDescription())
   61             }
   62         }
   63     }
   64 
   65     w.Flush()
   66 }
   67 
   68 func getHandler(handlers []*cli.Handler, prefix []string) *cli.Handler {
   69     for _, h := range handlers {
   70         pattern := stripOptionals(h.SplitPattern())
   71 
   72         if len(prefix) > len(pattern) {
   73             continue
   74         }
   75 
   76         if equal(prefix, pattern[:len(prefix)]) {
   77             return h
   78         }
   79     }
   80 
   81     return nil
   82 }
   83 
   84 // Strip optional groups (<...>) from pattern
   85 func stripOptionals(pattern []string) []string {
   86     newArgs := []string{}
   87 
   88     for _, arg := range pattern {
   89         if strings.HasPrefix(arg, "[") && strings.HasSuffix(arg, "]") {
   90             continue
   91         }
   92         newArgs = append(newArgs, arg)
   93     }
   94     return newArgs
   95 }