@@ -4,13 +4,19 @@ import pkg/cligen/parseopt3
4
4
type
5
5
ActionKind * = enum
6
6
actNil = " nil"
7
+ actCompletion = " completion"
7
8
actFmt = " fmt"
8
9
actGenerate = " generate"
9
10
actInfo = " info"
10
11
actLint = " lint"
11
12
actSync = " sync"
12
13
actUuid = " uuid"
13
14
15
+ Shell * = enum
16
+ sNil = " nil"
17
+ sBash = " bash"
18
+ sFish = " fish"
19
+
14
20
SyncKind * = enum
15
21
skDocs = " docs"
16
22
skFilepaths = " filepaths"
26
32
case kind* : ActionKind
27
33
of actNil, actGenerate, actLint:
28
34
discard
35
+ of actCompletion:
36
+ shell* : Shell
29
37
of actFmt:
30
38
# We can't name these fields `exercise`, `update`, and `yes` because we
31
39
# use those names in `actSync`, and Nim doesn't yet support duplicate
62
70
optTrackDir = " trackDir"
63
71
optVerbosity = " verbosity"
64
72
73
+ # Options for `completion`
74
+ optCompletionShell = " shell"
75
+
65
76
# Options for both `fmt` and `sync`
66
77
optFmtSyncExercise = " exercise"
67
78
optFmtSyncUpdate = " update"
@@ -133,8 +144,10 @@ func genHelpText: string =
133
144
# # Returns a string that describes the allowed values for an enum `T`.
134
145
result = " Allowed values: "
135
146
for val in T:
136
- result .add & " { ($ val)[0 ]} "
137
- result .add & " [{ ($ val)[1 .. ^ 1 ]} ], "
147
+ let s = $ val
148
+ if s != " nil" :
149
+ result .add s[0 ]
150
+ result .add & " [{ s[1 .. ^ 1 ]} ], "
138
151
setLen (result , result .len - 2 )
139
152
140
153
func genSyntaxStrings : tuple [syntax: array [Opt , string ], maxLen: int ] =
@@ -147,6 +160,7 @@ func genHelpText: string =
147
160
case opt
148
161
of optTrackDir: " dir"
149
162
of optVerbosity: " verbosity"
163
+ of optCompletionShell: " shell"
150
164
of optFmtSyncExercise: " slug"
151
165
of optSyncTests: " mode"
152
166
of optUuidNum: " int"
@@ -174,6 +188,7 @@ func genHelpText: string =
174
188
175
189
const actionDescriptions: array [ActionKind , string ] = [
176
190
actNil: " " ,
191
+ actCompletion: " Output a completion script for a given shell" ,
177
192
actFmt: " Format the exercise '.meta/config.json' files" ,
178
193
actGenerate: " Generate Concept Exercise 'introduction.md' files from 'introduction.md.tpl' files" ,
179
194
actInfo: " Print some information about the track" ,
@@ -199,6 +214,8 @@ func genHelpText: string =
199
214
optTrackDir: " Specify a track directory to use instead of the current directory" ,
200
215
optVerbosity: & " The verbosity of output.\n " &
201
216
& " { paddingOpt} { allowedValues (Verbosity )} (default: normal)" ,
217
+ optCompletionShell: & " Choose the shell type (required)\n " &
218
+ & " { paddingOpt} { allowedValues (Shell )} " ,
202
219
optFmtSyncExercise: " Only operate on this exercise" ,
203
220
optFmtSyncUpdate: " Prompt to update the unsynced track data" ,
204
221
optFmtSyncYes: & " Auto-confirm prompts from --{ $ optFmtSyncUpdate} for updating docs, filepaths, and metadata " ,
@@ -330,7 +347,7 @@ func formatOpt(kind: CmdLineKind, key: string, val = ""): string =
330
347
func init * (T: typedesc [Action ], actionKind: ActionKind ,
331
348
scope: set [SyncKind ] = {}): T =
332
349
case actionKind
333
- of actNil, actFmt, actGenerate, actInfo, actLint:
350
+ of actNil, actCompletion, actFmt, actGenerate, actInfo, actLint:
334
351
T (kind: actionKind)
335
352
of actSync:
336
353
T (kind: actionKind, scope: scope)
@@ -463,6 +480,12 @@ proc handleOption(conf: var Conf; kind: CmdLineKind; key, val: string) =
463
480
case conf.action.kind
464
481
of actNil, actGenerate, actLint:
465
482
discard
483
+ of actCompletion:
484
+ case opt
485
+ of optCompletionShell:
486
+ setActionOpt (shell, parseVal [Shell ](kind, key, val))
487
+ else :
488
+ discard
466
489
of actFmt:
467
490
case opt
468
491
of optFmtSyncExercise:
@@ -533,6 +556,9 @@ proc processCmdLine*: Conf =
533
556
case result .action.kind
534
557
of actNil:
535
558
showHelp ()
559
+ of actCompletion:
560
+ if result .action.shell == sNil:
561
+ showError (" Please choose a shell. For example: `configlet completion -s bash`" )
536
562
of actFmt, actGenerate, actInfo, actLint, actUuid:
537
563
discard
538
564
of actSync:
0 commit comments