@@ -95,7 +95,7 @@ func run() error {
95
95
}
96
96
for _ , c := range last .Children {
97
97
if c .Name == arg {
98
- cmd = & c
98
+ cmd = c
99
99
break
100
100
}
101
101
}
@@ -118,11 +118,11 @@ func run() error {
118
118
flag .Parse ()
119
119
120
120
cli := & Cli {
121
- flags : parsedFlags ,
122
- positionals : positionals ,
123
- path : cmds ,
124
- Context : ctx ,
125
- cancel : cancel ,
121
+ flags : parsedFlags ,
122
+ arguments : positionals ,
123
+ path : cmds ,
124
+ Context : ctx ,
125
+ cancel : cancel ,
126
126
}
127
127
128
128
configureLog (cli )
@@ -155,7 +155,15 @@ func run() error {
155
155
spin .Stop ()
156
156
157
157
active := cmds [len (cmds )- 1 ]
158
- if cli .Bool ("help" ) || active .Run == nil {
158
+
159
+ required := 0
160
+ for _ , arg := range active .Args {
161
+ if ! arg .Required {
162
+ continue
163
+ }
164
+ required += 1
165
+ }
166
+ if cli .Bool ("help" ) || active .Run == nil || len (cli .arguments ) != required {
159
167
return cli .PrintHelp ()
160
168
} else {
161
169
return active .Run (cli )
@@ -182,7 +190,7 @@ var Root = Command{
182
190
Description : "print help" ,
183
191
},
184
192
},
185
- Children : []Command {
193
+ Children : []* Command {
186
194
{
187
195
Name : "version" ,
188
196
Description : "print the version" ,
@@ -194,7 +202,23 @@ var Root = Command{
194
202
{
195
203
Name : "import" ,
196
204
Description : "import existing resource" ,
197
- Args : "<type> <name> <id>" ,
205
+ Args : []Argument {
206
+ {
207
+ Name : "type" ,
208
+ Required : true ,
209
+ Description : "The type of the resource" ,
210
+ },
211
+ {
212
+ Name : "name" ,
213
+ Required : true ,
214
+ Description : "The name of the resource" ,
215
+ },
216
+ {
217
+ Name : "id" ,
218
+ Required : true ,
219
+ Description : "The id of the resource" ,
220
+ },
221
+ },
198
222
Flags : []Flag {
199
223
{
200
224
Type : "string" ,
@@ -230,25 +254,39 @@ var Root = Command{
230
254
{
231
255
Name : "dev" ,
232
256
Description : "run in development mode" ,
233
- Args : "[target command]" ,
257
+ Args : [] Argument {{ Name : " command" , Description : "The command to run" }} ,
234
258
Run : CmdDev ,
235
259
},
236
260
{
237
261
Name : "secret" ,
238
262
Description : "manage secrets" ,
239
- Children : []Command {
263
+ Children : []* Command {
240
264
{
241
265
Name : "set" ,
242
266
Description : "set a secret" ,
243
- Args : "<name> <value>" ,
244
- Examples : []string {
245
- "sst secret set StripeSecret 123456789" ,
246
- "sst secret set StripeSecret productionsecret --stage=production" ,
267
+ Args : []Argument {
268
+ {
269
+ Name : "name" ,
270
+ Required : true ,
271
+ Description : "The name of the secret" ,
272
+ },
273
+ {
274
+ Name : "value" ,
275
+ Required : true ,
276
+ Description : "The value of the secret" ,
277
+ },
278
+ },
279
+ Examples : []Example {
280
+ {
281
+ Content : "sst secret set StripeSecret 123456789" ,
282
+ Description : "Set the StripeSecret to 123456789" ,
283
+ },
284
+ {
285
+ Content : "sst secret set StripeSecret productionsecret --stage=production" ,
286
+ Description : "Set the StripeSecret to production" ,
287
+ },
247
288
},
248
289
Run : func (cli * Cli ) error {
249
- if cli .PositionalLength () < 2 {
250
- return cli .PrintHelp ()
251
- }
252
290
key := cli .Positional (0 )
253
291
value := cli .Positional (1 )
254
292
p , err := initProject (cli )
@@ -295,7 +333,7 @@ var Root = Command{
295
333
},
296
334
{
297
335
Name : "shell" ,
298
- Args : "[command]" ,
336
+ Args : [] Argument {{ Name : "command" , Description : "The command to run" }} ,
299
337
Description : "run command with all resource linked in environment" ,
300
338
Run : func (cli * Cli ) error {
301
339
p , err := initProject (cli )
@@ -309,7 +347,7 @@ var Root = Command{
309
347
if err != nil {
310
348
return err
311
349
}
312
- args := cli .positionals
350
+ args := cli .arguments
313
351
if len (args ) == 0 {
314
352
args = append (args , "sh" )
315
353
}
@@ -531,7 +569,7 @@ var Root = Command{
531
569
{
532
570
Name : "state" ,
533
571
Description : "manage state of your deployment" ,
534
- Children : []Command {
572
+ Children : []* Command {
535
573
{
536
574
Name : "edit" ,
537
575
Description : "edit the state of your deployment" ,
@@ -574,12 +612,16 @@ var Root = Command{
574
612
},
575
613
}
576
614
615
+ func init () {
616
+ Root .init ()
617
+ }
618
+
577
619
type Cli struct {
578
- flags map [string ]interface {}
579
- positionals []string
580
- path CommandPath
581
- Context context.Context
582
- cancel context.CancelFunc
620
+ flags map [string ]interface {}
621
+ arguments []string
622
+ path CommandPath
623
+ Context context.Context
624
+ cancel context.CancelFunc
583
625
}
584
626
585
627
func (c * Cli ) Cancel () {
@@ -604,25 +646,68 @@ func (c *Cli) PrintHelp() error {
604
646
return c .path .PrintHelp ()
605
647
}
606
648
607
- func (c * Cli ) PositionalLength () int {
608
- return len ( c . positionals )
649
+ func (c * Cli ) Arguments () [] string {
650
+ return c . arguments
609
651
}
610
652
611
653
func (c * Cli ) Positional (index int ) string {
612
- return c .positionals [index ]
654
+ return c .arguments [index ]
613
655
}
614
656
615
657
type Command struct {
616
658
Name string `json:"name"`
617
659
Hidden bool `json:"hidden"`
618
660
Description string `json:"description"`
619
- Args string `json:"args"`
661
+ Args ArgumentList `json:"args"`
620
662
Flags []Flag `json:"flags"`
621
- Examples []string `json:"examples"`
622
- Children []Command `json:"children"`
663
+ Examples []Example `json:"examples"`
664
+ Children []* Command `json:"children"`
623
665
Run func (cli * Cli ) error `json:"-"`
624
666
}
625
667
668
+ func (c * Command ) init () {
669
+ if c .Args == nil {
670
+ c .Args = ArgumentList {}
671
+ }
672
+ if c .Flags == nil {
673
+ c .Flags = []Flag {}
674
+ }
675
+ if c .Examples == nil {
676
+ c .Examples = []Example {}
677
+ }
678
+ if c .Children == nil {
679
+ c .Children = []* Command {}
680
+ }
681
+ for _ , cmd := range c .Children {
682
+ cmd .init ()
683
+ }
684
+ }
685
+
686
+ type Example struct {
687
+ Content string `json:"content"`
688
+ Description string `json:"description"`
689
+ }
690
+
691
+ type Argument struct {
692
+ Name string `json:"name"`
693
+ Description string `json:"description"`
694
+ Required bool `json:"required"`
695
+ }
696
+
697
+ type ArgumentList []Argument
698
+
699
+ func (a ArgumentList ) String () string {
700
+ args := []string {}
701
+ for _ , arg := range a {
702
+ if arg .Required {
703
+ args = append (args , "<" + arg .Name + ">" )
704
+ } else {
705
+ args = append (args , "[" + arg .Name + "]" )
706
+ }
707
+ }
708
+ return strings .Join (args , " " )
709
+ }
710
+
626
711
type Flag struct {
627
712
Name string `json:"name"`
628
713
Type string `json:"type"`
@@ -650,8 +735,8 @@ func (c CommandPath) PrintHelp() error {
650
735
continue
651
736
}
652
737
next := len (child .Name )
653
- if child .Args != "" {
654
- next += len (child .Args ) + 1
738
+ if len ( child .Args ) > 0 {
739
+ next += len (child .Args . String ())
655
740
}
656
741
if next > maxSubcommand {
657
742
maxSubcommand = next
@@ -666,7 +751,7 @@ func (c CommandPath) PrintHelp() error {
666
751
fmt .Printf (
667
752
" %s %s %s\n " ,
668
753
strings .Join (prefix , " " ),
669
- color .New (color .FgWhite , color .Bold ).Sprintf ("%-*s" , maxSubcommand , strings .Join ([]string {child .Name , child .Args }, " " )),
754
+ color .New (color .FgWhite , color .Bold ).Sprintf ("%-*s" , maxSubcommand , strings .Join ([]string {child .Name , child .Args . String () }, " " )),
670
755
child .Description ,
671
756
)
672
757
}
@@ -675,8 +760,8 @@ func (c CommandPath) PrintHelp() error {
675
760
if len (active .Children ) == 0 {
676
761
color .New (color .FgWhite , color .Bold ).Print ("Usage: " )
677
762
color .New (color .FgCyan ).Print (strings .Join (prefix , " " ))
678
- if active .Args != "" {
679
- color .New (color .FgGreen ).Print (" " + active .Args )
763
+ if len ( active .Args ) > 0 {
764
+ color .New (color .FgGreen ).Print (" " + active .Args . String () )
680
765
}
681
766
fmt .Println ()
682
767
fmt .Println ()
@@ -706,7 +791,7 @@ func (c CommandPath) PrintHelp() error {
706
791
fmt .Println ()
707
792
color .New (color .FgWhite , color .Bold ).Print ("Examples:\n " )
708
793
for _ , example := range active .Examples {
709
- fmt .Println (" " + example )
794
+ fmt .Println (" " + example . Content )
710
795
}
711
796
}
712
797
}
0 commit comments