Skip to content

Commit 3412442

Browse files
Merge help-command and add :serverlist to help text
2 parents e1b1dcd + 4bea3f2 commit 3412442

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ switches are most important to you to have implemented next in the new sqlcmd.
154154
- `:Connect` now has an optional `-G` parameter to select one of the authentication methods for Azure SQL Database - `SqlAuthentication`, `ActiveDirectoryDefault`, `ActiveDirectoryIntegrated`, `ActiveDirectoryServicePrincipal`, `ActiveDirectoryManagedIdentity`, `ActiveDirectoryPassword`. If `-G` is not provided, either Integrated security or SQL Authentication will be used, dependent on the presence of a `-U` username parameter.
155155
- The new `--driver-logging-level` command line parameter allows you to see traces from the `go-mssqldb` client driver. Use `64` to see all traces.
156156
- Sqlcmd can now print results using a vertical format. Use the new `--vertical` command line option to set it. It's also controlled by the `SQLCMDFORMAT` scripting variable.
157+
- `:help` displays a list of available sqlcmd commands.
157158

158159
```
159160
1> select session_id, client_interface_name, program_name from sys.dm_exec_sessions where session_id=@@spid

pkg/sqlcmd/commands.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,13 @@ func newCommands() Commands {
113113
action: xmlCommand,
114114
name: "XML",
115115
},
116+
"HELP": {
117+
regex: regexp.MustCompile(`(?im)^[ \t]*:HELP(?:[ \t]+(.*$)|$)`),
118+
action: helpCommand,
119+
name: "HELP",
120+
},
116121
"SERVERLIST": {
117-
regex: regexp.MustCompile(`(?im)^[\t ]*?:SERVERLIST(?:[ \t]+(.*$)|$)`),
122+
regex: regexp.MustCompile(`(?im)^[ \t]*:SERVERLIST(?:[ \t]+(.*$)|$)`),
118123
action: serverlistCommand,
119124
name: "SERVERLIST",
120125
},
@@ -601,6 +606,53 @@ func xmlCommand(s *Sqlcmd, args []string, line uint) error {
601606
return nil
602607
}
603608

609+
// helpCommand displays the list of available sqlcmd commands
610+
func helpCommand(s *Sqlcmd, args []string, line uint) error {
611+
helpText := `:!! [<command>]
612+
- Executes a command in the operating system shell.
613+
:connect server[\instance] [-l timeout] [-U user [-P password]]
614+
- Connects to a SQL Server instance.
615+
:ed
616+
- Edits the current or last executed statement cache.
617+
:error <dest>
618+
- Redirects error output to a file, stderr, or stdout.
619+
:exit
620+
- Quits sqlcmd immediately.
621+
:exit()
622+
- Execute statement cache; quit with no return value.
623+
:exit(<query>)
624+
- Execute the specified query; returns numeric result.
625+
go [<n>]
626+
- Executes the statement cache (n times).
627+
:help
628+
- Shows this list of commands.
629+
:list
630+
- Prints the content of the statement cache.
631+
:listvar
632+
- Lists the set sqlcmd scripting variables.
633+
:on error [exit|ignore]
634+
- Action for batch or sqlcmd command errors.
635+
:out <filename>|stderr|stdout
636+
- Redirects query output to a file, stderr, or stdout.
637+
:quit
638+
- Quits sqlcmd immediately.
639+
:r <filename>
640+
- Append file contents to the statement cache.
641+
:reset
642+
- Discards the statement cache.
643+
:serverlist
644+
- Lists local SQL Server instances.
645+
:setvar {variable}
646+
- Removes a sqlcmd scripting variable.
647+
:setvar <variable> <value>
648+
- Sets a sqlcmd scripting variable.
649+
:xml [on|off]
650+
- Sets XML output mode.
651+
`
652+
_, err := s.GetOutput().Write([]byte(helpText))
653+
return err
654+
}
655+
604656
func serverlistCommand(s *Sqlcmd, args []string, line uint) error {
605657
if len(args) > 0 && args[0] != "" {
606658
return InvalidCommandError("SERVERLIST", line)

pkg/sqlcmd/commands_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func TestCommandParsing(t *testing.T) {
5454
{`:XML ON `, "XML", []string{`ON `}},
5555
{`:RESET`, "RESET", []string{""}},
5656
{`RESET`, "RESET", []string{""}},
57+
{`:HELP`, "HELP", []string{""}},
58+
{`:help`, "HELP", []string{""}},
5759
}
5860

5961
for _, test := range commands {
@@ -458,3 +460,24 @@ func TestExitCommandAppendsParameterToCurrentBatch(t *testing.T) {
458460
}
459461

460462
}
463+
464+
func TestHelpCommand(t *testing.T) {
465+
s, buf := setupSqlCmdWithMemoryOutput(t)
466+
defer buf.Close()
467+
s.SetOutput(buf)
468+
469+
err := helpCommand(s, []string{""}, 1)
470+
assert.NoError(t, err, "helpCommand should not error")
471+
472+
output := buf.buf.String()
473+
// Verify key commands are listed
474+
assert.Contains(t, output, ":connect", "help should list :connect")
475+
assert.Contains(t, output, ":exit", "help should list :exit")
476+
assert.Contains(t, output, ":help", "help should list :help")
477+
assert.Contains(t, output, ":setvar", "help should list :setvar")
478+
assert.Contains(t, output, ":listvar", "help should list :listvar")
479+
assert.Contains(t, output, ":out", "help should list :out")
480+
assert.Contains(t, output, ":error", "help should list :error")
481+
assert.Contains(t, output, ":r", "help should list :r")
482+
assert.Contains(t, output, "go", "help should list go")
483+
}

0 commit comments

Comments
 (0)