8000 special help subcmd prints to stdout; small improvements for args/flag errors by zacharysyoung · Pull Request #84 · aotimme/gocsv · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

special help subcmd prints to stdout; small improvements for args/flag errors #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func usageForSubcommand(subcommand Subcommand) string {
return retval
}

func exitTerseUsage() {
fmt.Fprintln(os.Stderr, "usage: gocsv help|version|<subcommand>")
fmt.Fprintln(os.Stderr, "Run 'gocsv help' for a list of subcommands, and 'gocsv <subcommand> -h' for subcommand details.")
os.Exit(2)
}

// Keep this in sync with the README.
func usage() string {
usage := "GoCSV is a command line CSV processing tool.\n"
Expand Down Expand Up @@ -127,20 +133,20 @@ func version() string {
func Main() {
args := os.Args
if len(args) == 1 {
fmt.Fprintln(os.Stderr, "Must provide a valid subcommand.")
fmt.Fprintf(os.Stderr, "%s\n", usage())
os.Exit(1)
return
exitTerseUsage()
}

subcommandName := args[1]
if subcommandName == "version" {
fmt.Println(version())

switch subcommandName {
case "help":
fmt.Println(usage())
return
}
if subcommandName == "help" {
fmt.Fprintf(os.Stderr, "%s\n", usage())
case "version":
fmt.Println(version())
return
}

for _, subcommand := range subcommands {
if MatchesSubcommand(subcommand, subcommandName) {
fs := flag.NewFlagSet(subcommand.Name(), flag.ExitOnError)
Expand All @@ -154,9 +160,9 @@ func Main() {
return
}
}
fmt.Fprintf(os.Stderr, "Invalid subcommand \"%s\"\n", subcommandName)
fmt.Fprintf(os.Stderr, "%s\n", usage())
os.Exit(1)

fmt.Fprintf(os.Stderr, "error: invalid subcommand \"%s\"\n\n", subcommandName)
exitTerseUsage()
}

func MatchesSubcommand(sub Subcommand, name string) bool {
Expand Down
0