Skip to content

Commit 69b3635

Browse files
committed
More cleanup of metacmd descriptions
1 parent c3ab5f1 commit 69b3635

File tree

2 files changed

+94
-60
lines changed

2 files changed

+94
-60
lines changed

metacmd/cmds.go

+92-53
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import (
2424
// Cmd is a command implementation.
2525
type Cmd struct {
2626
Section Section
27-
Desc Desc
28-
Aliases []Desc
27+
Descs []Desc
2928
Process func(*Params) error
3029
}
3130

@@ -42,8 +41,8 @@ func init() {
4241
cmds = []Cmd{
4342
Question: {
4443
Section: SectionHelp,
45-
Desc: Desc{"?", "[commands]", "show help on backslash commands"},
46-
Aliases: []Desc{
44+
Descs: []Desc{
45+
{"?", "[commands]", "show help on backslash commands"},
4746
{"?", "options", "show help on " + text.CommandName + " command-line options"},
4847
{"?", "variables", "show help on special " + text.CommandName + " variables"},
4948
},
@@ -80,16 +79,20 @@ func init() {
8079
},
8180
Quit: {
8281
Section: SectionGeneral,
83-
Desc: Desc{"q", "", "quit " + text.CommandName},
84-
Aliases: []Desc{{"quit", "", ""}},
82+
Descs: []Desc{
83+
{"q", "", "quit " + text.CommandName},
84+
{"quit", "", ""},
85+
},
8586
Process: func(p *Params) error {
8687
p.Option.Quit = true
8788
return nil
8889
},
8990
},
9091
Copyright: {
9192
Section: SectionGeneral,
92-
Desc: Desc{"copyright", "", "show " + text.CommandName + " usage and distribution terms"},
93+
Descs: []Desc{
94+
{"copyright", "", "show " + text.CommandName + " usage and distribution terms"},
95+
},
9396
Process: func(p *Params) error {
9497
stdout := p.Handler.IO().Stdout()
9598
if typ := env.TermGraphics(); typ.Available() {
@@ -101,7 +104,9 @@ func init() {
101104
},
102105
ConnectionInfo: {
103106
Section: SectionConnection,
104-
Desc: Desc{"conninfo", "", "display information about the current database connection"},
107+
Descs: []Desc{
108+
{"conninfo", "", "display information about the current database connection"},
109+
},
105110
Process: func(p *Params) error {
106111
s := text.NotConnected
107112
if db, u := p.Handler.DB(), p.Handler.URL(); db != nil && u != nil {
@@ -113,7 +118,9 @@ func init() {
113118
},
114119
Drivers: {
115120
Section: SectionGeneral,
116-
Desc: Desc{"drivers", "", "display information about available database drivers"},
121+
Descs: []Desc{
122+
{"drivers", "", "display information about available database drivers"},
123+
},
117124
Process: func(p *Params) error {
118125
stdout, stderr := p.Handler.IO().Stdout(), p.Handler.IO().Stderr()
119126
var cmd *exec.Cmd
@@ -158,8 +165,8 @@ func init() {
158165
},
159166
Connect: {
160167
Section: SectionConnection,
161-
Desc: Desc{"c", "DSN", "connect to database url"},
162-
Aliases: []Desc{
168+
Descs: []Desc{
169+
{"c", "DSN", "connect to database url"},
163170
{"c", "DRIVER PARAMS...", "connect to database with driver and parameters"},
164171
{"connect", "", ""},
165172
},
@@ -175,8 +182,8 @@ func init() {
175182
},
176183
SetConnVar: {
177184
Section: SectionConnection,
178-
Desc: Desc{"cset", "[NAME [DSN]]", "set named connection, or list all if no parameters"},
179-
Aliases: []Desc{
185+
Descs: []Desc{
186+
{"cset", "[NAME [DSN]]", "set named connection, or list all if no parameters"},
180187
{"cset", "NAME DRIVER PARAMS...", "define named connection for database driver"},
181188
},
182189
Process: func(p *Params) error {
@@ -203,16 +210,20 @@ func init() {
203210
},
204211
Disconnect: {
205212
Section: SectionConnection,
206-
Desc: Desc{"Z", "", "close database connection"},
207-
Aliases: []Desc{{"disconnect", "", ""}},
213+
Descs: []Desc{
214+
{"Z", "", "close database connection"},
215+
{"disconnect", "", ""},
216+
},
208217
Process: func(p *Params) error {
209218
return p.Handler.Close()
210219
},
211220
},
212221
Password: {
213222
Section: SectionConnection,
214-
Desc: Desc{"password", "[USERNAME]", "change the password for a user"},
215-
Aliases: []Desc{{"passwd", "", ""}},
223+
Descs: []Desc{
224+
{"password", "[USERNAME]", "change the password for a user"},
225+
{"passwd", "", ""},
226+
},
216227
Process: func(p *Params) error {
217228
username, err := p.Get(true)
218229
if err != nil {
@@ -231,8 +242,8 @@ func init() {
231242
},
232243
Exec: {
233244
Section: SectionQueryExecute,
234-
Desc: Desc{"g", "[(OPTIONS)] [FILE] or ;", "execute query (and send results to file or |pipe)"},
235-
Aliases: []Desc{
245+
Descs: []Desc{
246+
{"g", "[(OPTIONS)] [FILE] or ;", "execute query (and send results to file or |pipe)"},
236247
{"G", "[(OPTIONS)] [FILE]", "as \\g, but forces vertical output mode"},
237248
{"gx", "[(OPTIONS)] [FILE]", "as \\g, but forces expanded output mode"},
238249
{"gexec", "", "execute query and execute each value of the result"},
@@ -309,7 +320,9 @@ func init() {
309320
},
310321
Bind: {
311322
Section: SectionQueryExecute,
312-
Desc: Desc{"bind", "[PARAM]...", "set query parameters"},
323+
Descs: []Desc{
324+
{"bind", "[PARAM]...", "set query parameters"},
325+
},
313326
Process: func(p *Params) error {
314327
bind, err := p.GetAll(true)
315328
if err != nil {
@@ -328,8 +341,10 @@ func init() {
328341
},
329342
Edit: {
330343
Section: SectionQueryBuffer,
331-
Desc: Desc{"e", "[FILE] [LINE]", "edit the query buffer (or file) with external editor"},
332-
Aliases: []Desc{{"edit", "", ""}},
344+
Descs: []Desc{
345+
{"e", "[FILE] [LINE]", "edit the query buffer (or file) with external editor"},
346+
{"edit", "", ""},
347+
},
333348
Process: func(p *Params) error {
334349
// get last statement
335350
s, buf := p.Handler.Last(), p.Handler.Buf()
@@ -357,8 +372,8 @@ func init() {
357372
},
358373
Print: {
359374
Section: SectionQueryBuffer,
360-
Desc: Desc{"p", "", "show the contents of the query buffer"},
361-
Aliases: []Desc{
375+
Descs: []Desc{
376+
{"p", "", "show the contents of the query buffer"},
362377
{"print", "", ""},
363378
{"raw", "", "show the raw (non-interpolated) contents of the query buffer"},
364379
},
@@ -393,8 +408,10 @@ func init() {
393408
},
394409
Reset: {
395410
Section: SectionQueryBuffer,
396-
Desc: Desc{"r", "", "reset (clear) the query buffer"},
397-
Aliases: []Desc{{"reset", "", ""}},
411+
Descs: []Desc{
412+
{"r", "", "reset (clear) the query buffer"},
413+
{"reset", "", ""},
414+
},
398415
Process: func(p *Params) error {
399416
p.Handler.Reset(nil)
400417
p.Handler.Print(text.QueryBufferReset)
@@ -403,8 +420,8 @@ func init() {
403420
},
404421
Echo: {
405422
Section: SectionInputOutput,
406-
Desc: Desc{"echo", "[-n] [STRING]", "write string to standard output (-n for no newline)"},
407-
Aliases: []Desc{
423+
Descs: []Desc{
424+
{"echo", "[-n] [STRING]", "write string to standard output (-n for no newline)"},
408425
{"qecho", "[-n] [STRING]", "write string to \\o output stream (-n for no newline)"},
409426
{"warn", "[-n] [STRING]", "write string to standard error (-n for no newline)"},
410427
},
@@ -440,8 +457,10 @@ func init() {
440457
},
441458
Write: {
442459
Section: SectionQueryBuffer,
443-
Desc: Desc{"w", "FILE", "write query buffer to file"},
444-
Aliases: []Desc{{"write", "", ""}},
460+
Descs: []Desc{
461+
{"w", "FILE", "write query buffer to file"},
462+
{"write", "", ""},
463+
},
445464
Process: func(p *Params) error {
446465
// get last statement
447466
s, buf := p.Handler.Last(), p.Handler.Buf()
@@ -457,7 +476,9 @@ func init() {
457476
},
458477
ChangeDir: {
459478
Section: SectionOperatingSystem,
460-
Desc: Desc{"cd", "[DIR]", "change the current working directory"},
479+
Descs: []Desc{
480+
{"cd", "[DIR]", "change the current working directory"},
481+
},
461482
Process: func(p *Params) error {
462483
dir, err := p.Get(true)
463484
if err != nil {
@@ -468,7 +489,9 @@ func init() {
468489
},
469490
GetEnv: {
470491
Section: SectionOperatingSystem,
471-
Desc: Desc{"getenv", "VARNAME ENVVAR", "fetch environment variable"},
492+
Descs: []Desc{
493+
{"getenv", "VARNAME ENVVAR", "fetch environment variable"},
494+
},
472495
Process: func(p *Params) error {
473496
n, err := p.Get(true)
474497
switch {
@@ -490,7 +513,9 @@ func init() {
490513
},
491514
SetEnv: {
492515
Section: SectionOperatingSystem,
493-
Desc: Desc{"setenv", "NAME [VALUE]", "set or unset environment variable"},
516+
Descs: []Desc{
517+
{"setenv", "NAME [VALUE]", "set or unset environment variable"},
518+
},
494519
Process: func(p *Params) error {
495520
n, err := p.Get(true)
496521
if err != nil {
@@ -505,7 +530,9 @@ func init() {
505530
},
506531
Timing: {
507532
Section: SectionOperatingSystem,
508-
Desc: Desc{"timing", "[on|off]", "toggle timing of commands"},
533+
Descs: []Desc{
534+
{"timing", "[on|off]", "toggle timing of commands"},
535+
},
509536
Process: func(p *Params) error {
510537
v, err := p.Get(true)
511538
if err != nil {
@@ -536,15 +563,19 @@ func init() {
536563
},
537564
Shell: {
538565
Section: SectionOperatingSystem,
539-
Desc: Desc{"!", "[COMMAND]", "execute command in shell or start interactive shell"},
566+
Descs: []Desc{
567+
{"!", "[COMMAND]", "execute command in shell or start interactive shell"},
568+
},
540569
Process: func(p *Params) error {
541570
return env.Shell(p.GetRaw())
542571
},
543572
},
544573
Out: {
545574
Section: SectionInputOutput,
546-
Desc: Desc{"o", "[FILE]", "send all query results to file or |pipe"},
547-
Aliases: []Desc{{"out", "", ""}},
575+
Descs: []Desc{
576+
{"o", "[FILE]", "send all query results to file or |pipe"},
577+
{"out", "", ""},
578+
},
548579
Process: func(p *Params) error {
549580
p.Handler.SetOutput(nil)
550581
params, err := p.GetAll(true)
@@ -570,8 +601,8 @@ func init() {
570601
},
571602
Include: {
572603
Section: SectionInputOutput,
573-
Desc: Desc{"i", "FILE", "execute commands from file"},
574-
Aliases: []Desc{
604+
Descs: []Desc{
605+
{"i", "FILE", "execute commands from file"},
575606
{"ir", "FILE", "as \\i, but relative to location of current script"},
576607
{"include", "", ""},
577608
{"include_relative", "", ""},
@@ -590,8 +621,8 @@ func init() {
590621
},
591622
Transact: {
592623
Section: SectionTransaction,
593-
Desc: Desc{"begin", "", "begin a transaction"},
594-
Aliases: []Desc{
624+
Descs: []Desc{
625+
{"begin", "", "begin a transaction"},
595626
{"begin", "[-read-only] [ISOLATION]", "begin a transaction with isolation level"},
596627
{"commit", "", "commit current transaction"},
597628
{"rollback", "", "rollback (abort) current transaction"},
@@ -650,7 +681,9 @@ func init() {
650681
},
651682
Prompt: {
652683
Section: SectionVariables,
653-
Desc: Desc{"prompt", "[-TYPE] VAR [PROMPT]", "prompt user to set variable"},
684+
Descs: []Desc{
685+
{"prompt", "[-TYPE] VAR [PROMPT]", "prompt user to set variable"},
686+
},
654687
Process: func(p *Params) error {
655688
typ := "string"
656689
ok, n, err := p.GetOptional(true)
@@ -683,7 +716,9 @@ func init() {
683716
},
684717
SetVar: {
685718
Section: SectionVariables,
686-
Desc: Desc{"set", "[NAME [VALUE]]", "set internal variable, or list all if no parameters"},
719+
Descs: []Desc{
720+
{"set", "[NAME [VALUE]]", "set internal variable, or list all if no parameters"},
721+
},
687722
Process: func(p *Params) error {
688723
ok, n, err := p.GetOK(true)
689724
switch {
@@ -708,7 +743,9 @@ func init() {
708743
},
709744
Unset: {
710745
Section: SectionVariables,
711-
Desc: Desc{"unset", "NAME", "unset (delete) internal variable"},
746+
Descs: []Desc{
747+
{"unset", "NAME", "unset (delete) internal variable"},
748+
},
712749
Process: func(p *Params) error {
713750
n, err := p.Get(true)
714751
if err != nil {
@@ -719,8 +756,8 @@ func init() {
719756
},
720757
SetPrintVar: {
721758
Section: SectionFormatting,
722-
Desc: Desc{"pset", "[NAME [VALUE]]", "set table output option"},
723-
Aliases: []Desc{
759+
Descs: []Desc{
760+
{"pset", "[NAME [VALUE]]", "set table output option"},
724761
{"a", "", "toggle between unaligned and aligned output mode"},
725762
{"C", "[STRING]", "set table title, or unset if none"},
726763
{"f", "[STRING]", "show or set field separator for unaligned query output"},
@@ -806,8 +843,8 @@ func init() {
806843
},
807844
Describe: {
808845
Section: SectionInformational,
809-
Desc: Desc{"d[S+]", "[NAME]", "list tables, views, and sequences or describe table, view, sequence, or index"},
810-
Aliases: []Desc{
846+
Descs: []Desc{
847+
{"d[S+]", "[NAME]", "list tables, views, and sequences or describe table, view, sequence, or index"},
811848
{"da[S+]", "[PATTERN]", "list aggregates"},
812849
{"df[S+]", "[PATTERN]", "list functions"},
813850
{"di[S+]", "[PATTERN]", "list indexes"},
@@ -857,7 +894,9 @@ func init() {
857894
},
858895
Stats: {
859896
Section: SectionInformational,
860-
Desc: Desc{"ss[+]", "[TABLE|QUERY] [k]", "show stats for a table or a query"},
897+
Descs: []Desc{
898+
{"ss[+]", "[TABLE|QUERY] [k]", "show stats for a table or a query"},
899+
},
861900
Process: func(p *Params) error {
862901
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
863902
defer cancel()
@@ -894,8 +933,8 @@ func init() {
894933
},
895934
Copy: {
896935
Section: SectionInputOutput,
897-
Desc: Desc{"copy", "SRC DST QUERY TABLE", "copy query from source url to table on destination url"},
898-
Aliases: []Desc{
936+
Descs: []Desc{
937+
{"copy", "SRC DST QUERY TABLE", "copy query from source url to table on destination url"},
899938
{"copy", "SRC DST QUERY TABLE(A,...)", "copy query from source url to columns of table on destination url"},
900939
},
901940
Process: func(p *Params) error {
@@ -960,7 +999,7 @@ func init() {
960999
if mc == None {
9611000
continue
9621001
}
963-
name := c.Desc.Name
1002+
name := c.Descs[0].Name
9641003
if pos := strings.IndexRune(name, '['); pos != -1 {
9651004
mods := strings.TrimRight(name[pos+1:], "]")
9661005
name = name[:pos]
@@ -972,7 +1011,7 @@ func init() {
9721011
}
9731012
}
9741013
cmdMap[name] = mc
975-
for _, d := range c.Aliases {
1014+
for _, d := range c.Descs {
9761015
if pos := strings.IndexRune(d.Name, '['); pos != -1 {
9771016
mods := strings.TrimRight(d.Name[pos+1:], "]")
9781017
d.Name = d.Name[:pos]

metacmd/section.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,11 @@ func Listing(w io.Writer) {
6161
var descs [][]string
6262
for _, c := range sectMap[section] {
6363
cmd := cmds[c]
64-
s, opts := optText(cmd.Desc)
65-
descs, plen = add(descs, ` \`+cmd.Desc.Name+opts, s, plen)
66-
// sort aliases
67-
var aliases []int
68-
for i, d := range cmd.Aliases {
64+
for i, d := range cmd.Descs {
6965
if d.Desc == "" && d.Params == "" {
7066
continue
7167
}
72-
aliases = append(aliases, i)
73-
s, opts := optText(cmd.Aliases[i])
68+
s, opts := optText(cmd.Descs[i])
7469
descs, plen = add(descs, ` \`+strings.TrimSpace(d.Name)+opts, s, plen)
7570
}
7671
}

0 commit comments

Comments
 (0)