Skip to content

Commit d6aab70

Browse files
committed
argparse: exit with usage if positional arg missing
If a positional (string) arg is missing, the 'Arg()' function will return an empty string without indicating that the value is missing. Since (for the moment) positional args are always required, add an explicit check that the arg is present and, if not, exit with usage. Signed-off-by: Victoria Dye <[email protected]>
1 parent 7ea9882 commit d6aab70

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

internal/argparse/argparse.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ func (a *argParser) Parse(ctx context.Context, args []string) {
168168
// First, try single string case
169169
sPtr, isStr := arg.value.(*string)
170170
if isStr {
171+
if a.NArg() == 0 {
172+
a.Usage(ctx, "No value specified for required argument '%s'", arg.name)
173+
}
174+
171175
*sPtr = a.Arg(0)
172176
a.argOffset++
173177
continue

0 commit comments

Comments
 (0)