iterative / shtab

↔️ Automagic shell tab completion for Python CLI applications
https://docs.iterative.ai/shtab
Other
362 stars 35 forks source link

Complete only positional arguments after double dash ("--") #163

Closed usidedown closed 5 months ago

usidedown commented 6 months ago

All arguments following "--" in argparse are treated as positional arguments. e.g.

compgen -W "foo bar baz" -- b

As far as I can tell shtab doesn't treat double dash in a special manner. It would be pretty nice if shtab recognized this as delimiter between the options and the positional arguments.

I couldn't find a reference to this behavior in argparse documentation, but it's evident from code & usage.

for i, arg_string in enumerate(arg_strings_iter):  
  # all args after -- are non-options
  if arg_string == '--':
...

This is a common practice, as can be seen in POSIX:

POSIX.1–2017 standard

12.2 Utility Syntax Guidelines

Guideline 10:

The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the ‘-’ character.

it solves some ambiguity when using optional arguments that accept multiple values.

git checkout master     # Checkout the master branch
git checkout -- master  # Checkout the file named master
rm -- -file.txt  # delete file with dash in name 

By taking double dash into account, cases such as above would be completed correctly.