Пример #1
0
func listItems(vault *onepass.Vault, items []onepass.Item) {
	rangeutil.Sort(0, len(items), func(i, k int) bool {
		return strings.ToLower(items[i].Title) < strings.ToLower(items[k].Title)
	},
		func(i, k int) {
			items[i], items[k] = items[k], items[i]
		})

	for _, item := range items {
		trashState := ""
		if item.Trashed {
			trashState = " (in trash)"
		}
		fmt.Printf("%s (%s, %s)%s\n", item.Title, item.Type(), item.Uuid[0:4], trashState)
	}
}
Пример #2
0
// PrintHelp prints help output for the command.
// If cmd is empty, prints banner followed by the list of supported
// commands and one-line descriptions for each.
//
// If cmd is non-empty, prints the syntax for that particular command
// along with the text returned by that command's ExtraHelp function.
//
func (p *Parser) PrintHelp(banner string, cmd string) {
	if len(cmd) == 0 {
		fmt.Fprintf(os.Stderr, "Usage: %s <command> <args>\n\n", os.Args[0])
		fmt.Fprintf(os.Stderr, "%s\n\n", banner)
		fmt.Fprintf(os.Stderr, "Supported commands:\n\n")

		sortedCommands := append([]Mode{}, p.Modes...)
		rangeutil.Sort(0, len(sortedCommands), func(i, k int) bool {
			return sortedCommands[i].Command < sortedCommands[k].Command
		},
			func(i, k int) {
				sortedCommands[i], sortedCommands[k] = sortedCommands[k], sortedCommands[i]
			})

		// maximum width for command names before
		// description is moved onto next line
		cmdWidth := 12
		for _, cmd := range sortedCommands {
			if cmd.Internal {
				continue
			}
			fmt.Fprintf(os.Stderr, "  %s", cmd.Command)
			padding := 0
			if len(cmd.Command) > cmdWidth {
				fmt.Fprintf(os.Stderr, "\n")
				padding = 2 + cmdWidth
			} else {
				padding = cmdWidth - len(cmd.Command)
			}
			padding += 2
			fmt.Fprintf(os.Stderr, "  %*.s%s\n", padding, "", cmd.Description)
		}
		fmt.Printf("\nUse '%s help <command>' for more information about using a given command.\n\n", os.Args[0])
	} else {
		found := false
		for _, mode := range p.Modes {
			if mode.Command == cmd {
				found = true

				syntax := fmt.Sprintf("%s %s", os.Args[0], mode.Command)
				for _, arg := range mode.ArgNames {
					if strings.HasPrefix(arg, "[") {
						// optional arg
						syntax = fmt.Sprintf("%s %s", syntax, arg)
					} else {
						// required arg
						syntax = fmt.Sprintf("%s <%s>", syntax, arg)
					}
				}
				fmt.Printf("%s\n\n%s\n\n", syntax, mode.Description)

				if mode.ExtraHelp != nil {
					fmt.Printf("%s\n\n", mode.ExtraHelp())
				}
			}
		}
		if !found {
			fmt.Fprintf(os.Stderr, "No such command: '%s'\n", cmd)
		}
	}
}