Merge pull request #210 from cabuda/master

feat: support more env than terminal
This commit is contained in:
Alex Flint 2023-02-08 06:13:50 -08:00 committed by GitHub
commit 5dbdd5d0c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/csv" "encoding/csv"
"errors" "errors"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -119,6 +120,10 @@ type Config struct {
// StrictSubcommands intructs the library not to allow global commands after // StrictSubcommands intructs the library not to allow global commands after
// subcommand // subcommand
StrictSubcommands bool StrictSubcommands bool
OsExit func(int)
Stdout io.Writer
Stderr io.Writer
} }
// Parser represents a set of command line options with destination values // Parser represents a set of command line options with destination values
@ -132,6 +137,10 @@ type Parser struct {
// the following field changes during processing of command line arguments // the following field changes during processing of command line arguments
lastCmd *command lastCmd *command
osExit func(int)
stdout io.Writer
stderr io.Writer
} }
// Versioned is the interface that the destination struct should implement to // Versioned is the interface that the destination struct should implement to
@ -196,6 +205,20 @@ func NewParser(config Config, dests ...interface{}) (*Parser, error) {
p := Parser{ p := Parser{
cmd: &command{name: name}, cmd: &command{name: name},
config: config, config: config,
osExit: osExit,
stdout: stdout,
stderr: stderr,
}
if config.OsExit != nil {
p.osExit = config.OsExit
}
if config.Stdout != nil {
p.stdout = config.Stdout
}
if config.Stderr != nil {
p.stderr = config.Stderr
} }
// make a list of roots // make a list of roots
@ -483,11 +506,11 @@ func (p *Parser) MustParse(args []string) {
err := p.Parse(args) err := p.Parse(args)
switch { switch {
case err == ErrHelp: case err == ErrHelp:
p.writeHelpForSubcommand(stdout, p.lastCmd) p.writeHelpForSubcommand(p.stdout, p.lastCmd)
osExit(0) p.osExit(0)
case err == ErrVersion: case err == ErrVersion:
fmt.Fprintln(stdout, p.version) fmt.Fprintln(p.stdout, p.version)
osExit(0) p.osExit(0)
case err != nil: case err != nil:
p.failWithSubcommand(err.Error(), p.lastCmd) p.failWithSubcommand(err.Error(), p.lastCmd)
} }

View File

@ -39,9 +39,9 @@ func (p *Parser) FailSubcommand(msg string, subcommand ...string) error {
// failWithSubcommand prints usage information for the given subcommand to stderr and exits with non-zero status // failWithSubcommand prints usage information for the given subcommand to stderr and exits with non-zero status
func (p *Parser) failWithSubcommand(msg string, cmd *command) { func (p *Parser) failWithSubcommand(msg string, cmd *command) {
p.writeUsageForSubcommand(stderr, cmd) p.writeUsageForSubcommand(p.stderr, cmd)
fmt.Fprintln(stderr, "error:", msg) fmt.Fprintln(p.stderr, "error:", msg)
osExit(-1) p.osExit(-1)
} }
// WriteUsage writes usage information to the given writer // WriteUsage writes usage information to the given writer