Skip to content

Commit ebd4b2f

Browse files
Implement :help command
Add the :help command to display available sqlcmd commands. This improves compatibility with legacy ODBC sqlcmd. Changes: - Added HELP command to command registry - Added helpCommand function with full command list - Added tests for command parsing and functionality - Updated README.md
1 parent 758fca9 commit ebd4b2f

3 files changed

Lines changed: 78 additions & 0 deletions

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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ 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
}
117122
}
118123

@@ -596,6 +601,55 @@ func xmlCommand(s *Sqlcmd, args []string, line uint) error {
596601
return nil
597602
}
598603

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

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)