github.com/richardwilkes/toolbox@v1.121.0/cmdline/help.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package cmdline 11 12 import ( 13 "fmt" 14 15 "github.com/richardwilkes/toolbox/atexit" 16 "github.com/richardwilkes/toolbox/i18n" 17 ) 18 19 type helpCmd struct{} 20 21 // Name implements the Cmd interface. 22 func (c *helpCmd) Name() string { 23 return "help" 24 } 25 26 // Usage implements the Cmd interface. 27 func (c *helpCmd) Usage() string { 28 return i18n.Text("Display help information for a command and exit.") 29 } 30 31 // Run implements the Cmd interface. 32 func (c *helpCmd) Run(cmdLine *CmdLine, args []string) error { 33 cmdLine = cmdLine.parent 34 if len(args) > 0 { 35 if args[0] != "help" { 36 const helpFlag = "-h" 37 if helpFlag != args[0] { 38 for name, cmd := range cmdLine.cmds { 39 if name == args[0] { 40 return cmd.Run(cmdLine.newWithCmd(cmd), []string{helpFlag}) 41 } 42 } 43 } 44 fmt.Fprintf(cmdLine, i18n.Text("'%[1]s' is not a valid %[2]s command\n"), args[0], AppCmdName) 45 } 46 } 47 cmdLine.DisplayUsage() 48 atexit.Exit(1) 49 return nil 50 }