Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Prompt Support #722

Merged
merged 4 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions interp/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,19 @@ func (r *Runner) builtinCode(ctx context.Context, pos syntax.Pos, name string, a
}
r.setErr(returnStatus(code))
case "read":
var prompt string
raw := false
fp := flagParser{remaining: args}
for fp.more() {
switch flag := fp.flag(); flag {
case "-r":
raw = true
case "-p":
prompt = fp.value()
if prompt == "" {
r.errf("read: -p: option requires an argument\n")
return 2
}
default:
r.errf("read: invalid option %q\n", flag)
return 2
Expand All @@ -538,6 +545,10 @@ func (r *Runner) builtinCode(ctx context.Context, pos syntax.Pos, name string, a
}
}

if prompt != "" {
r.out(prompt)
}

line, err := r.readLine(raw)
if err != nil {
return 1
Expand Down
16 changes: 16 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,22 @@ set +o pipefail
"IFS=: read a b c <<< '1\\:2:3'; echo \"$a\"; echo $b; echo $c",
"1:2\n3\n\n",
},
{
"read -p",
"read: -p: option requires an argument\nexit status 2 #JUSTERR",
},
{
"read -X -p",
"read: invalid option \"-X\"\nexit status 2 #JUSTERR",
},
{
"read -p 'Display me as a prompt. Continue? (y/n) ' choice <<< 'y'; echo $choice",
"Display me as a prompt. Continue? (y/n) y\n",
},
{
"read -r -p 'Prompt and raw flag together: ' a <<< '\\a\\b\\c'; echo $a",
"Prompt and raw flag together: \\a\\b\\c\n",
},

// getopts
{
Expand Down