Skip to content

Commit

Permalink
cmd refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Sep 12, 2023
1 parent 8f653e3 commit e455fba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
33 changes: 20 additions & 13 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ var (
)

// Cmd represents the base command when called without any subcommands.
var Cmd = &cobra.Command{ //nolint:gochecknoglobals
Use: meta.Bin,
Short: fmt.Sprintf("Use %s to print plain, BBS era and ANSI textfiles", meta.Name),
Long: meta.Name + " takes legacy codepage and ANSI encoded textfiles and\n" +
"prints them to a modern Unicode terminal.",
Example: fmt.Sprint(example.Cmd),
RunE: func(cmd *cobra.Command, args []string) error {
// Do nothing other than print the help.
// This func must remain otherwise root command flags are ignored by Cobra.
return flag.Help(cmd)
},
var Cmd = base()

func base() *cobra.Command {
s := fmt.Sprintf("Use %s to print plain, BBS era and ANSI textfiles", meta.Name)
l := meta.Name + " takes legacy codepage and ANSI encoded textfiles and\n" +
"prints them to a modern Unicode terminal."
expl := strings.Builder{}
example.Cmd.String(&expl)
return &cobra.Command{
Use: meta.Bin,
Short: s,
Long: l,
Example: expl.String(),
RunE: func(cmd *cobra.Command, args []string) error {
// Do nothing other than print the help.
// This func must remain otherwise root
// command flags are ignored by Cobra.
return flag.Help(cmd)
},
}
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down Expand Up @@ -59,11 +68,9 @@ func Execute() error {

func Init() {
Cmd = Tester(Cmd)

Cmd.AddGroup(&cobra.Group{ID: "listCmds", Title: "Codepages:"})
Cmd.AddGroup(&cobra.Group{ID: "fileCmds", Title: "Files:"})
Cmd.AddGroup(&cobra.Group{ID: "exaCmds", Title: "Testers:"})

// create a version flag that only works on root.
Cmd.LocalNonPersistentFlags().BoolP("version", "v", false, "")
// hide the cobra introduced help command.
Expand Down
6 changes: 3 additions & 3 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func InfoCommand() *cobra.Command {
}

func InfoInit() *cobra.Command {
ic := InfoCommand()
infoc := InfoCommand()
infos := format.Format().Info
s := &strings.Builder{}
_, _ = term.Options(s, "print format or syntax", true, true, infos[:]...)
ic.Flags().StringVarP(&flag.Info.Format, "format", "f", "color", s.String())
return ic
infoc.Flags().StringVarP(&flag.Info.Format, "format", "f", "color", s.String())
return infoc
}

//nolint:gochecknoinits
Expand Down

0 comments on commit e455fba

Please sign in to comment.