Skip to content

Commit

Permalink
chore: command help improvement (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsloth55 authored Jan 17, 2025
1 parent 283ee62 commit 551de68
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 52 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ $ curl -L https://github.com/guyfedwards/nom/releases/download/v2.1.4/nom_2.1.4_
## Usage
```sh
$ nom # start TUI
$ nom list -n 20 # list feed items in $PAGER, optionally show more
$ nom add <feed_url>
$ nom refresh # refresh feed(s) without opening TUI
$ nom config # shows nom config
$ nom --feed <feed_url> # preview feed without adding to config
$ nom -h # see all available command and options
```

## Config
Expand Down
137 changes: 90 additions & 47 deletions cmd/nom/main.go
Original file line number Diff line number Diff line change
@@ -1,97 +1,140 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/jessevdk/go-flags"

"github.com/guyfedwards/nom/v2/internal/commands"
"github.com/guyfedwards/nom/v2/internal/config"
"github.com/guyfedwards/nom/v2/internal/store"
"github.com/jessevdk/go-flags"
)

type Options struct {
Verbose bool `short:"v" long:"verbose" description:"Show verbose logging"`
Number int `short:"n" long:"number" description:"Number of results to show"`
Pager string `short:"p" long:"pager" description:"Pager to use for longer output. Set to false for no pager"`
ConfigPath string `long:"config-path" description:"Location of config.yml"`
PreviewFeeds []string `short:"f" long:"feed" description:"Feed(s) URL(s) for preview"`
Version bool `long:"version" description:"Display version information"`
}

var (
version = "dev"
ErrNotEnoughArgs = errors.New("not enough args")
options Options
version = "dev"
)

func run(args []string, opts Options) error {
cfg, err := config.New(opts.ConfigPath, opts.Pager, opts.PreviewFeeds, version)
// Setup subcommands

type Add struct {
Positional struct {
Url string `positional-arg-name:"URL" required:"yes"`
} `positional-args:"yes"`
}

func (r *Add) Execute(args []string) error {
cmds, err := getCmds()
if err != nil {
return err
}
return cmds.Add(r.Positional.Url)
}

if err = cfg.Load(); err != nil {
type Config struct{}

func (r *Config) Execute(args []string) error {
cmds, err := getCmds()
if err != nil {
return err
}
return cmds.ShowConfig()
}

s, err := store.NewSQLiteStore(cfg.ConfigDir)
type List struct{}

func (r *List) Execute(args []string) error {
cmds, err := getCmds()
if err != nil {
return fmt.Errorf("main.go: %w", err)
return err
}
cmds := commands.New(cfg, s)

if opts.Version || (len(args) > 0 && args[0] == "version") {
fmt.Printf("%s\n", version)
return nil
return cmds.List()
}

type Version struct{}

func (r *Version) Execute(args []string) error {
_, err := getCmds()
if err != nil {
return err
}

// no subcommand, run the TUI
if len(args) == 0 {
return cmds.TUI()
fmt.Printf("%s\n", version)
return nil
}

type Refresh struct{}

func (r *Refresh) Execute(args []string) error {
cmds, err := getCmds()
if err != nil {
return err
}
return cmds.Refresh()
}

switch args[0] {
case "list":
return cmds.List(opts.Number)
case "refresh":
return cmds.Refresh()
case "config":
return cmds.ShowConfig()
case "add":
if len(args) != 2 {
return ErrNotEnoughArgs
}
func getCmds() (*commands.Commands, error) {
cfg, err := config.New(options.ConfigPath, options.Pager, options.PreviewFeeds, version)
if err != nil {
return nil, err
}

return cmds.Add(args[1])
if err = cfg.Load(); err != nil {
return nil, err
}

return nil
s, err := store.NewSQLiteStore(cfg.ConfigDir)
if err != nil {
return nil, fmt.Errorf("main.go: %w", err)
}
cmds := commands.New(cfg, s)
return &cmds, nil
}

func main() {
var opts Options
parser := flags.NewParser(&options, flags.Default)
// allow nom to be run without any subcommands
parser.SubcommandsOptional = true

// add commands
parser.AddCommand("add", "Add feed", "Add a new feed", &Add{})
parser.AddCommand("config", "Show config", "Show configuration", &Config{})
parser.AddCommand("list", "List feeds", "List all feeds", &List{})
parser.AddCommand("version", "Show Vesion", "Display version information", &Version{})
parser.AddCommand("refresh", "Refresh feeds", "refresh feed(s) without opening TUI", &Refresh{})

parser := flags.NewParser(&opts, flags.Default)
// parse the command line arguments
_, err := parser.Parse()

args, err := parser.Parse()
// check for help flag
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Printf("%v\n", err)
os.Exit(1)
if flagErr, ok := err.(*flags.Error); ok && flagErr.Type != flags.ErrHelp {
parser.WriteHelp(os.Stdout)
}
return

os.Exit(0)
}

if err := run(args, opts); err != nil {
if opts.Verbose || errors.Is(err, config.ErrFeedAlreadyExists) {
fmt.Printf("%v\n", err)
// no subcommand or help flag, run the TUI
if parser.Active == nil {
cmds, err := getCmds()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

parser.WriteHelp(os.Stderr)
os.Exit(1)
err = cmds.TUI()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return
}
}
2 changes: 1 addition & 1 deletion internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func GetWslHostName() string {
return strings.TrimSpace(string(out))
}

func (c Commands) List(numResults int) error {
func (c Commands) List() error {
its, err := c.GetAllFeeds()
if err != nil {
return fmt.Errorf("commands List: %w", err)
Expand Down

0 comments on commit 551de68

Please sign in to comment.