Code to show help

This commit is contained in:
Kovid Goyal 2022-10-13 21:07:17 +05:30
parent 9419fbc77c
commit 59edf1d349
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 40 additions and 2 deletions

View File

@ -89,6 +89,10 @@ func (self *Option) FormatOption(output io.Writer, formatter *markup.Context, sc
}
func (self *Command) ShowHelp() {
self.ShowHelpWithCommandString(strings.TrimSpace(self.CommandStringForUsage()))
}
func (self *Command) ShowHelpWithCommandString(cs string) {
formatter := markup.New(tty.IsTerminal(os.Stdout.Fd()))
screen_width := 80
if formatter.EscapeCodesAllowed() {
@ -106,8 +110,7 @@ func (self *Command) ShowHelp() {
}
var output strings.Builder
fmt.Fprintln(&output, formatter.Title("Usage")+":", formatter.Exe(strings.TrimSpace(self.CommandStringForUsage())),
strings.TrimSpace(formatter.Prettify(self.Usage)))
fmt.Fprintln(&output, formatter.Title("Usage")+":", formatter.Exe(cs), strings.TrimSpace(formatter.Prettify(self.Usage)))
fmt.Fprintln(&output)
if self.HelpText != "" {
format_with_indent(&output, formatter.Prettify(prepare_help_text_for_display(self.HelpText)), "", screen_width)

View File

@ -95,6 +95,21 @@ func shell_loop(rl *readline.Readline, kill_if_signaled bool) (int, error) {
return 0, nil
}
func print_basic_help() {
fmt.Println("Control kitty by sending it commands.")
fmt.Println()
fmt.Println(formatter.Title("Commands") + ":")
r := EntryPoint(cli.NewRootCommand())
for _, g := range r.SubCommandGroups {
for _, sc := range g.SubCommands {
fmt.Println(" ", formatter.Green(sc.Name))
fmt.Println(" ", sc.ShortDescription)
}
}
fmt.Println(" ", formatter.Green("exit"))
fmt.Println(" ", "Exit this shell")
}
func exec_command(cmdline string) bool {
parsed_cmdline, err := shlex.Split(cmdline)
if err != nil {
@ -107,6 +122,26 @@ func exec_command(cmdline string) bool {
switch parsed_cmdline[0] {
case "exit":
return false
case "help":
if len(parsed_cmdline) == 1 {
print_basic_help()
return true
}
switch parsed_cmdline[1] {
case "exit":
fmt.Println("Exit this shell")
case "help":
fmt.Println("Show help")
default:
r := EntryPoint(cli.NewRootCommand())
sc := r.FindSubCommand(parsed_cmdline[1])
if sc == nil {
fmt.Fprintln(os.Stderr, "No command named", formatter.BrightRed(parsed_cmdline[1]), ". Type help for a list of commands")
} else {
sc.ShowHelpWithCommandString(sc.Name)
}
}
return true
}
return true
}