Skip to content

Commit 88c243f

Browse files
committed
Print short script usage for '-h' (excluding global flags)
If you simply invoke rad on a script with -h, we'll give the 'short' version of its usage, which is just that script's specific usage -- none of the global rad arguments. Also, while here, don't print a 'Script args' header if the script doesn't have args -- oversight that's survived this far.
1 parent 48af0fe commit 88c243f

File tree

9 files changed

+74
-36
lines changed

9 files changed

+74
-36
lines changed

core/funcs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ func init() {
13491349
Execute: func(f FuncInvocationArgs) []RslValue {
13501350
// defaults
13511351
config := stid.NewConfig().
1352-
WithTickSize(stid.Decisecond).
1352+
WithTickSize(stid.Decisecond). // todo maybe milli, but reduce num random chars to 4?
13531353
WithNumRandomChars(5).
13541354
WithAlphabet(stid.Base62Alphabet)
13551355

core/printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (p *stdPrinter) RadErrorExit(msg string) {
250250

251251
func (p *stdPrinter) UsageErrorExit(msg string) {
252252
fmt.Fprint(p.stdErr, msg+"\n\n")
253-
p.runner.RunUsage(true)
253+
p.runner.RunUsage(false, true)
254254
p.printShellExitIfEnabled()
255255
p.errorExit(1)
256256
}

core/runner.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ func (r *RadRunner) Run() error {
123123
// determine if we should run help/version or not
124124

125125
if FlagHelp.Value {
126-
r.RunUsageExit()
126+
shortHelp := !lo.Contains(os.Args[1:], "--help")
127+
r.RunUsageExit(shortHelp)
127128
}
128129

129130
if FlagVersion.Value {
@@ -143,7 +144,7 @@ func (r *RadRunner) Run() error {
143144
}
144145

145146
// no flags, effectively, just print the basic usage
146-
r.RunUsageExit()
147+
r.RunUsageExit(false)
147148
}
148149

149150
// from now on, assume we have a script name (or command)
@@ -227,7 +228,7 @@ func (r *RadRunner) Run() error {
227228
if len(missingArgs) > 0 && len(args) == 0 && !atLeastOneFlagConfigured {
228229
// if no args were passed but some are required, treat that as the user not really trying to use the script
229230
// but instead just asking for help
230-
r.RunUsageExit()
231+
r.RunUsageExit(true)
231232
}
232233

233234
// error if not all positional args were used
@@ -274,7 +275,7 @@ func (r *RadRunner) setUpGlobals() {
274275
RFlagSet.ParseErrorsWhitelist.UnknownFlags = true
275276

276277
RFlagSet.Usage = func() {
277-
r.RunUsage(false)
278+
r.RunUsage(false, false)
278279
}
279280

280281
r.globalFlags = CreateAndRegisterGlobalFlags()

core/runner_usage.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
"strings"
88
)
99

10-
func (r *RadRunner) RunUsage(isErr bool) {
10+
func (r *RadRunner) RunUsage(shortHelp, isErr bool) {
1111
if r.scriptData == nil {
1212
r.printScriptlessUsage(isErr)
1313
} else {
14-
r.printScriptUsage(isErr)
14+
r.printScriptUsage(shortHelp, isErr)
1515
}
1616
}
1717

18-
func (r *RadRunner) RunUsageExit() {
19-
r.RunUsage(false)
18+
func (r *RadRunner) RunUsageExit(shortHelp bool) {
19+
r.RunUsage(shortHelp, false)
2020
if FlagShell.Value {
2121
RP.PrintForShellEval("exit 0")
2222
}
@@ -45,7 +45,7 @@ func (r *RadRunner) printScriptlessUsage(isErr bool) {
4545
r.printHelpFromBuffer(buf, isErr)
4646
}
4747

48-
func (r *RadRunner) printScriptUsage(isErr bool) {
48+
func (r *RadRunner) printScriptUsage(shortHelp, isErr bool) {
4949
buf := new(bytes.Buffer)
5050

5151
if r.scriptData.Description != nil {
@@ -70,16 +70,20 @@ func (r *RadRunner) printScriptUsage(isErr bool) {
7070
com.CyanF(buf, fmt.Sprintf(" <%s>", arg.ApiName))
7171
}
7272
}
73-
fmt.Fprintf(buf, "\n\n")
74-
75-
com.GreenBoldF(buf, "Script args:\n")
76-
flagUsage(buf, r.scriptArgs)
7773

7874
fmt.Fprintf(buf, "\n")
7975

80-
// todo probably don't print these if there's a script? Or only minimal ones if --help is passed (not -h) ?
81-
com.GreenBoldF(buf, "Global flags:\n")
82-
flagUsage(buf, r.globalFlags)
76+
if len(r.scriptArgs) > 0 {
77+
fmt.Fprintf(buf, "\n")
78+
com.GreenBoldF(buf, "Script args:\n")
79+
flagUsage(buf, r.scriptArgs)
80+
}
81+
82+
if !shortHelp {
83+
fmt.Fprintf(buf, "\n")
84+
com.GreenBoldF(buf, "Global flags:\n")
85+
flagUsage(buf, r.globalFlags)
86+
}
8387

8488
r.printHelpFromBuffer(buf, isErr)
8589
}

core/testing/args_test.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func TestArgs_ApiRenameUsageString(t *testing.T) {
2828
2929
Script args:
3030
-x, --bar string
31-
32-
` + scriptGlobalFlagHelp
31+
`
3332
assertOnlyOutput(t, stdOutBuffer, expected)
3433
assertNoErrors(t)
3534
}
@@ -47,8 +46,7 @@ args:
4746
Script args:
4847
--mandatory string
4948
--optional int (default 10)
50-
51-
` + scriptGlobalFlagHelp
49+
`
5250
assertOnlyOutput(t, stdOutBuffer, expected)
5351
assertNoErrors(t)
5452
}
@@ -283,7 +281,23 @@ args:
283281
assertError(t, 1, expected)
284282
}
285283

286-
func TestArgs_Help(t *testing.T) {
284+
func TestArgs_FullHelp(t *testing.T) {
285+
rsl := `
286+
args:
287+
name string # The name.
288+
`
289+
setupAndRunCode(t, rsl, "--help", "--color=never")
290+
expected := `Usage:
291+
<name>
292+
293+
Script args:
294+
--name string The name.
295+
296+
` + scriptGlobalFlagHelp
297+
assertOnlyOutput(t, stdOutBuffer, expected)
298+
}
299+
300+
func TestArgs_ShortHelp(t *testing.T) {
287301
rsl := `
288302
args:
289303
name string # The name.
@@ -294,6 +308,28 @@ args:
294308
295309
Script args:
296310
--name string The name.
311+
`
312+
assertOnlyOutput(t, stdOutBuffer, expected)
313+
}
314+
315+
func TestArgs_ShortHelpNoArgs(t *testing.T) {
316+
rsl := `
317+
print("hi")
318+
`
319+
setupAndRunCode(t, rsl, "-h", "--color=never")
320+
expected := `Usage:
321+
322+
`
323+
assertOnlyOutput(t, stdOutBuffer, expected)
324+
}
325+
326+
func TestArgs_FullHelpNoArgs(t *testing.T) {
327+
rsl := `
328+
print("hi")
329+
`
330+
setupAndRunCode(t, rsl, "--help", "--color=never")
331+
expected := `Usage:
332+
297333
298334
` + scriptGlobalFlagHelp
299335
assertOnlyOutput(t, stdOutBuffer, expected)
@@ -324,8 +360,7 @@ Script args:
324360
--intArrayArg int,int (default [2, 3])
325361
--floatArrayArg float,float (default [2.1, 3.1])
326362
--boolArrayArg bool,bool (default [true, false])
327-
328-
` + scriptGlobalFlagHelp
363+
`
329364
assertOnlyOutput(t, stdOutBuffer, expected)
330365
}
331366

core/testing/file_header_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ Usage:
1818
1919
Script args:
2020
--name string
21-
22-
` + scriptGlobalFlagHelp
21+
`
2322
assertOnlyOutput(t, stdOutBuffer, expected)
2423
assertNoErrors(t)
2524
}
@@ -46,8 +45,7 @@ Usage:
4645
4746
Script args:
4847
--name string
49-
50-
` + scriptGlobalFlagHelp
48+
`
5149
assertOnlyOutput(t, stdOutBuffer, expected)
5250
assertNoErrors(t)
5351
}

core/testing/misc_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func Test_Misc_CanShadowGlobalFlag(t *testing.T) {
127127
args:
128128
src string
129129
`
130-
setupAndRunCode(t, rsl, "--color=never", "-h")
130+
setupAndRunCode(t, rsl, "--color=never", "--help")
131131
expectedGlobalFlags := globalFlagHelpWithout("src")
132132
expected := `Usage:
133133
<src>
@@ -145,7 +145,7 @@ func Test_Misc_CanShadowGlobalFlagThatHasShorthand(t *testing.T) {
145145
args:
146146
debug string
147147
`
148-
setupAndRunCode(t, rsl, "--color=never", "-h")
148+
setupAndRunCode(t, rsl, "--color=never", "--help")
149149
expectedGlobalFlags := `Global flags:
150150
-h, --help Print usage string.
151151
-d Enables debug output. Intended for RSL script developers.
@@ -170,7 +170,7 @@ func Test_Misc_CanShadowGlobalShorthand(t *testing.T) {
170170
args:
171171
myquiet q string
172172
`
173-
setupAndRunCode(t, rsl, "--color=never", "-h")
173+
setupAndRunCode(t, rsl, "--color=never", "--help")
174174
expectedGlobalFlags := `Global flags:
175175
-h, --help Print usage string.
176176
-d, --debug Enables debug output. Intended for RSL script developers.
@@ -195,7 +195,7 @@ func Test_Misc_CanShadowGlobalFlagAndShorthand(t *testing.T) {
195195
args:
196196
version v string
197197
`
198-
setupAndRunCode(t, rsl, "--color=never", "-h")
198+
setupAndRunCode(t, rsl, "--color=never", "--help")
199199
expectedGlobalFlags := globalFlagHelpWithout("version")
200200
expected := `Usage:
201201
<version>

core/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package core
22

33
const (
4-
Version = "0.5.30"
4+
Version = "0.5.31"
55
// todo add in commit hash somehow?
66
)

docs-web/docs/reference/functions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,10 @@ uuid_v7() -> string
524524

525525
### gen_stid
526526

527-
Generate a random [Short Time ID](https://github.com/amterp/stid) (STID).
527+
Generate a random [short time ID](https://github.com/amterp/stid) (stid).
528528

529529
```rsl
530-
gen_stid(alphabet: string, time_granularity: int = 100, num_random_chars: int = 5) -> string
530+
gen_stid(alphabet: string, tick_size_ms: int = 100, num_random_chars: int = 5) -> string
531531
```
532532

533533
`alphabet` defaults to base-62 (`[0-9] [A-Z] [a-z]`).

0 commit comments

Comments
 (0)