General cleanup

This commit is contained in:
Kovid Goyal
2022-08-17 13:08:58 +05:30
parent 6c25f0cf4b
commit b3be6792fd
2 changed files with 10 additions and 9 deletions

View File

@@ -258,7 +258,7 @@ func full_command_name(cmd *cobra.Command) string {
return strings.Join(parent_names, " ") return strings.Join(parent_names, " ")
} }
func show_usage(cmd *cobra.Command) error { func show_usage(cmd *cobra.Command, use_pager bool) error {
ws, tty_size_err := GetTTYSize() ws, tty_size_err := GetTTYSize()
var output strings.Builder var output strings.Builder
screen_width := 80 screen_width := 80
@@ -340,7 +340,7 @@ func show_usage(cmd *cobra.Command) error {
} }
output_text := output.String() output_text := output.String()
// fmt.Printf("%#v\n", output_text) // fmt.Printf("%#v\n", output_text)
if cmd.Annotations["use-pager-for-usage"] == "true" && stdout_is_terminal && cmd.Annotations["allow-pager"] != "no" { if use_pager && stdout_is_terminal && cmd.Annotations["allow-pager"] != "no" {
pager := exec.Command(kitty.DefaultPager[0], kitty.DefaultPager[1:]...) pager := exec.Command(kitty.DefaultPager[0], kitty.DefaultPager[1:]...)
pager.Stdin = strings.NewReader(output_text) pager.Stdin = strings.NewReader(output_text)
pager.Stdout = os.Stdout pager.Stdout = os.Stdout
@@ -358,7 +358,7 @@ func CreateCommand(cmd *cobra.Command) *cobra.Command {
cmd.RunE = func(cmd *cobra.Command, args []string) error { cmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(cmd.Commands()) > 0 { if len(cmd.Commands()) > 0 {
if len(args) == 0 { if len(args) == 0 {
return fmt.Errorf("%s. Use %s -h to get a list of available sub-commands", "No sub-command specified", full_command_name(cmd)) return fmt.Errorf("No sub-command specified. Use %s -h to get a list of available sub-commands", full_command_name(cmd))
} }
return fmt.Errorf("Not a valid subcommand: %s. Use %s -h to get a list of available sub-commands", args[0], full_command_name(cmd)) return fmt.Errorf("Not a valid subcommand: %s. Use %s -h to get a list of available sub-commands", args[0], full_command_name(cmd))
} }
@@ -373,10 +373,7 @@ func CreateCommand(cmd *cobra.Command) *cobra.Command {
} }
func show_help(cmd *cobra.Command, args []string) { func show_help(cmd *cobra.Command, args []string) {
if cmd.Annotations != nil { show_usage(cmd, true)
cmd.Annotations["use-pager-for-usage"] = "true"
}
show_usage(cmd)
} }
func PrintError(err error) { func PrintError(err error) {
@@ -391,8 +388,12 @@ func Init(root *cobra.Command) {
stdout_is_terminal = isatty.IsTerminal(os.Stdout.Fd()) stdout_is_terminal = isatty.IsTerminal(os.Stdout.Fd())
RootCmd = root RootCmd = root
root.Version = vs root.Version = vs
root.SetUsageFunc(show_usage) root.SetUsageFunc(func(cmd *cobra.Command) error { return show_usage(cmd, false) })
root.SetHelpFunc(show_help) root.SetHelpFunc(show_help)
root.SetHelpCommand(&cobra.Command{Hidden: true}) root.SetHelpCommand(&cobra.Command{Hidden: true})
root.CompletionOptions.DisableDefaultCmd = true root.CompletionOptions.DisableDefaultCmd = true
} }
func Execute(root *cobra.Command) error {
return root.Execute()
}

View File

@@ -17,7 +17,7 @@ func main() {
root.AddCommand(at.EntryPoint(root)) root.AddCommand(at.EntryPoint(root))
cli.Init(root) cli.Init(root)
if err := root.Execute(); err != nil { if err := cli.Execute(root); err != nil {
cli.PrintError(err) cli.PrintError(err)
os.Exit(1) os.Exit(1)
} }