Skip to content

fix -h switch #73

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

Merged
merged 3 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions cmd/sqlcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ type SQLCmdArguments struct {
ErrorLevel int `short:"m" help:"Controls which error messages are sent to stdout. Messages that have severity level greater than or equal to this level are sent."`
Format string `short:"F" help:"Specifies the formatting for results." default:"horiz" enum:"horiz,horizontal,vert,vertical"`
ErrorsToStderr int `short:"r" help:"Redirects the error message output to the screen (stderr). A value of 0 means messages with severity >= 11 will b redirected. A value of 1 means all error message output including PRINT is redirected." enum:"-1,0,1" default:"-1"`
Help bool `short:"?" help:"show syntax summary"`
Headers int `short:"h" help:"Specifies the number of rows to print between the column headings. Use -h-1 to specify that headers not be printed."`
}

// Validate accounts for settings not described by Kong attributes
func (a *SQLCmdArguments) Validate() error {
if a.PacketSize != 0 && (a.PacketSize < 512 || a.PacketSize > 32767) {
return fmt.Errorf(`'-a %d': Packet size has to be a number between 512 and 32767.`, a.PacketSize)
}

if a.Headers < -1 {
return fmt.Errorf(`'-h %d': header value must be either -1 or a value between -1 and 2147483647`, a.Headers)
}
return nil
}

Expand Down Expand Up @@ -96,7 +100,11 @@ func (a SQLCmdArguments) authenticationMethod(hasPassword bool) string {
}

func main() {
kong.Parse(&args)
ctx := kong.Parse(&args, kong.NoDefaultHelp())
if args.Help {
_ = ctx.PrintUsage(false)
os.Exit(0)
}
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
setVars(vars, &args)

Expand Down Expand Up @@ -138,7 +146,7 @@ func setVars(vars *sqlcmd.Variables, args *SQLCmdArguments) {
},
sqlcmd.SQLCMDUSER: func(a *SQLCmdArguments) string { return a.UserName },
sqlcmd.SQLCMDSTATTIMEOUT: func(a *SQLCmdArguments) string { return "" },
sqlcmd.SQLCMDHEADERS: func(a *SQLCmdArguments) string { return "" },
sqlcmd.SQLCMDHEADERS: func(a *SQLCmdArguments) string { return fmt.Sprint(a.Headers) },
sqlcmd.SQLCMDCOLSEP: func(a *SQLCmdArguments) string { return "" },
sqlcmd.SQLCMDCOLWIDTH: func(a *SQLCmdArguments) string { return "" },
sqlcmd.SQLCMDMAXVARTYPEWIDTH: func(a *SQLCmdArguments) string { return "" },
Expand Down
5 changes: 5 additions & 0 deletions cmd/sqlcmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func newKong(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
t.Helper()
options = append([]kong.Option{
kong.Name("test"),
kong.NoDefaultHelp(),
kong.Exit(func(int) {
t.Helper()
t.Fatalf("unexpected exit()")
Expand Down Expand Up @@ -76,6 +77,9 @@ func TestValidCommandLineToArgsConversion(t *testing.T) {
{[]string{"-r", "1"}, func(args SQLCmdArguments) bool {
return args.ErrorsToStderr == 1
}},
{[]string{"-h", "2", "-?"}, func(args SQLCmdArguments) bool {
return args.Help && args.Headers == 2
}},
}

for _, test := range commands {
Expand Down Expand Up @@ -104,6 +108,7 @@ func TestInvalidCommandLine(t *testing.T) {
{[]string{"-a", "100"}, "test: '-a 100': Packet size has to be a number between 512 and 32767."},
{[]string{"-F", "what"}, "--format must be one of \"horiz\",\"horizontal\",\"vert\",\"vertical\" but got \"what\""},
{[]string{"-r", "5"}, `--errors-to-stderr must be one of "-1","0","1" but got '\x05'`},
{[]string{"-h-4"}, "test: '-h -4': header value must be either -1 or a value between -1 and 2147483647"},
}

for _, test := range commands {
Expand Down