Skip to content

Commit

Permalink
[builtin/complete] Add syntax error when no args, but flags
Browse files Browse the repository at this point in the history
- 'complete' is an alias for 'complete -p'
- 'complete -F f' with no args is a syntax error
  • Loading branch information
Andy C committed Aug 12, 2023
1 parent c793774 commit 2032d64
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
17 changes: 12 additions & 5 deletions osh/builtin_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ def __init__(

self.errfmt = errfmt


def Build(self, argv, attrs, base_opts):
# type: (List[str], _Attributes, Dict[str, bool]) -> UserSpec
"""Given flags to complete/compgen, return a UserSpec."""
Expand Down Expand Up @@ -280,21 +279,29 @@ def Run(self, cmd_val):
commands = arg_r.Rest()

if arg.D:
commands.append(
'__fallback') # if the command doesn't match anything
# if the command doesn't match anything
commands.append('__fallback')
if arg.E:
commands.append('__first') # empty line

if len(commands) == 0:
self.comp_lookup.PrintSpecs()
return 0
if len(cmd_val.argv) == 1: # nothing passed at all
assert cmd_val.argv[0] == 'complete'

# TODO: crashes in C++
self.comp_lookup.PrintSpecs()
return 0
else:
# complete -F f is an error
raise error.Usage('expected 1 or more commands', loc.Missing)

base_opts = dict(attrs.opt_changes)
try:
user_spec = self.spec_builder.Build(cmd_val.argv, attrs, base_opts)
except error.Parse as e:
# error printed above
return 2

for command in commands:
self.comp_lookup.RegisterName(command, base_opts, user_spec)

Expand Down
64 changes: 52 additions & 12 deletions spec/builtin-completion.test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@

## oils_failures_allowed: 2
## compare_shells: bash

#### complete with no args and complete -p both print completion spec

set -e

complete

complete -W 'foo bar' mycommand

complete

complete -F myfunc other

complete -p

## STDOUT:
complete -W 'foo bar' mycommand
complete -W 'foo bar' mycommand
complete -F myfunc other
## END

#### complete -F f is usage error

#complete -F f cmd

# Alias for complete -p
complete > /dev/null # ignore OSH output for now
echo status=$?

# But this is an error
complete -F f
echo status=$?

## STDOUT:
status=0
status=2
## END

#### complete with nonexistent function
complete -F invalidZZ -D
echo status=$?
## stdout: status=2
## BUG bash stdout: status=0

#### complete with no action
complete foo
echo status=$?
## stdout: status=2
## BUG bash stdout: status=0

#### -A function prints functions
add () { expr 4 + 4; }
div () { expr 6 / 2; }
Expand Down Expand Up @@ -218,18 +270,6 @@ while
status=0
## END

#### complete with nonexistent function
complete -F invalidZZ -D
echo status=$?
## stdout: status=2
## BUG bash stdout: status=0

#### complete with no action
complete foo
echo status=$?
## stdout: status=2
## BUG bash stdout: status=0

#### -o filenames and -o nospace have no effect with compgen
# they are POSTPROCESSING.
compgen -o filenames -o nospace -W 'bin build'
Expand Down
4 changes: 1 addition & 3 deletions test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,8 @@ vars-special() {
${REF_SHELLS[@]} $ZSH $OSH_LIST "$@"
}

# This is bash/OSH only
builtin-completion() {
sh-spec spec/builtin-completion.test.sh --oils-failures-allowed 1 \
$BASH $OSH_LIST "$@"
run-file builtin-completion "$@"
}

builtin-special() {
Expand Down

0 comments on commit 2032d64

Please sign in to comment.