Skip to content

Commit

Permalink
feat: switch to a simplified version of flag
Browse files Browse the repository at this point in the history
Eventually, I want to write a flag-parsing library of my own.  But for
now, I can use flag from the standard library.  However, to simplify my
code, I have removed a lot from flag.

Most importantly, this version of flag no longer special cases "-h" or
"--help" and it does not display usage.
  • Loading branch information
telemachus committed Jan 4, 2025
1 parent 52e1406 commit c354d56
Show file tree
Hide file tree
Showing 11 changed files with 789 additions and 656 deletions.
2 changes: 1 addition & 1 deletion cmd/gitmirror/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import (
)

func main() {
os.Exit(cli.Gitmirror(os.Args))
os.Exit(cli.Gitmirror(os.Args[1:]))
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/telemachus/gitmirror

go 1.23.4

require github.com/google/go-cmp v0.6.0
require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/google/go-cmp v0.6.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
50 changes: 28 additions & 22 deletions internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path/filepath"
"strings"

"github.com/telemachus/gitmirror/internal/optionparser"
"github.com/telemachus/gitmirror/internal/flag"
)

type appEnv struct {
Expand All @@ -18,29 +18,41 @@ type appEnv struct {
home string
storage string
exitVal int
help bool
quiet bool
version bool
}

func appFrom(args []string) (*appEnv, error) {
app := &appEnv{cmd: cmd, exitVal: exitSuccess}

op := optionparser.NewOptionParser()
op.On("-c", "--config FILE", "Use FILE as config file (default ~/.gitmirror.json)", &app.config)
op.On("-q", "--quiet", "Print only error messages", &app.quiet)
op.On("--version", "Print version and exit", version)
op.Command("clone", "Clone git repositories using `git clone --mirror`")
op.Command("update|up", "Update git repositories using `git remote update`")
op.Command("sync", "Run both update and clone (in that order)")
op.Start = 24
op.Banner = "Usage: gitmirror [options] <subcommand>"
op.Coda = "\nFor more information or to file a bug report visit https://github.com/telemachus/gitmirror"
fs := flag.NewFlagSet("gitmirror")
fs.StringVar(&app.config, "config", "", "")
fs.StringVar(&app.config, "c", "", "")
fs.BoolVar(&app.help, "help", false, "")
fs.BoolVar(&app.help, "h", false, "")
fs.BoolVar(&app.quiet, "quiet", false, "")
fs.BoolVar(&app.quiet, "q", false, "")
fs.BoolVar(&app.version, "version", false, "")

// Do not continue if we cannot parse and validate arguments or get the
// user's home directory.
if err := op.ParseFrom(args); err != nil {
if err := fs.Parse(args); err != nil {
return nil, err
}
if err := validate(op.Extra); err != nil {

// Quick and dirty, but why be fancy in these cases?
if app.help {
fmt.Print(_usage)
os.Exit(exitSuccess)
}
if app.version {
fmt.Printf("%s %s\n", cmd, cmdVersion)
os.Exit(exitSuccess)
}

// Do not continue if we cannot parse and validate arguments or get the
// user's home directory.
extraArgs := fs.Args()
if err := validate(extraArgs); err != nil {
return nil, err
}
home, err := os.UserHomeDir()
Expand All @@ -52,7 +64,7 @@ func appFrom(args []string) (*appEnv, error) {
app.config = filepath.Join(home, config)
}
app.storage = filepath.Join(home, storage)
app.subCmd = op.Extra[0]
app.subCmd = extraArgs[0]

return app, nil
}
Expand Down Expand Up @@ -83,9 +95,3 @@ func validate(extra []string) error {

return nil
}

// Quick and dirty, but why be fancy in this case?
func version() {
fmt.Printf("%s %s\n", cmd, cmdVersion)
os.Exit(exitSuccess)
}
21 changes: 21 additions & 0 deletions internal/cli/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cli

import "github.com/MakeNowJust/heredoc"

var _usage = heredoc.Docf(`
usage: gitmirror [options] <subcommand> [options]
Options
-c, --config=FILE Use FILE as config file (default ~/.gitmirror.json)
-q, --quiet Print only error messages
-h, --help Print this help and exit
--version Print version and exit
Subcommands
clone Clone git repositories using %[1]sgit clone --mirror%[1]s
update|up Update git repositories using %[1]sgit remote update%[1]s
sync Run both update and clone (in that order)
For more information or to file a bug report visit https://github.com/telemachus/gitmirror
`, "`")
Loading

0 comments on commit c354d56

Please sign in to comment.