0

Please find the following snippet:

  import argparse

  parser = argparse.ArgumentParser(
      description="Create  plot from data",
      formatter_class=lambda prog: argparse.HelpFormatter(
          prog, max_help_position=27))
  action = parser.add_mutually_exclusive_group(required=True)
  action.add_argument('--foo', help="Create foo plot")     # input is <filename>.foo
  action.add_argument('--bar', help="Create bar plot")     # Input is <filename>.bar

I run this in linux terminal emulator. Is it possible within python that, in the terminal, double tabiing will show only files with extension foo or bar, depending on argument, and not all the files in PWD?

I have found TAB autocomplete python CLI, but that is a decade old. Is there any option now?

Update @Lenormju: I have updated the code as:

action.add_argument('--foo', help="Create foo plot", choices=('agr'))
  action.add_argument(
      '--bar', help="Create bar plot").completer = ChoicesCompleter('.agr')

So, now in terminal,

python ~/bin/code.py --foo [TAB][TAB]

I am expecting this to show files with .agr extensions only. Instead it is still shown all the files present in PWD.

Actually, this should not work, because "choices" means, I have to choose between 'a' 'g' or 'r'. May be I was not clear in the main question, show I have elaborated it.

BaRud
  • 3,055
  • 7
  • 41
  • 89
  • Duplicate of [How to make a python, command-line program autocomplete arbitrary things](https://stackoverflow.com/questions/187621/how-to-make-a-python-command-line-program-autocomplete-arbitrary-things-not-int) – Lenormju Jun 03 '21 at 13:30
  • Old does not mean it is bad. – Lenormju Jun 03 '21 at 13:31
  • Does this answer your question? [How to make a python, command-line program autocomplete arbitrary things NOT interpreter](https://stackoverflow.com/questions/187621/how-to-make-a-python-command-line-program-autocomplete-arbitrary-things-not-int) – Lenormju Jun 03 '21 at 13:31
  • No. I was probably not clear in the question. I want it while giving the cli, not auto complete in Python. – BaRud Jun 04 '21 at 04:42
  • As for example, in linux, `evince [tab][tab]` only shows `pdf` files. – BaRud Jun 04 '21 at 04:48

1 Answers1

1

When you press Tab, your shell expects to receive a list of strings (the possible completions).

  • Either you register a Bash function to do the completion for your Python script (see this answer) so that your Python script is not called until the command line is finished.
  • Or else you do it with your Python script (see this answer for argcomplete which mixes well with argparse). In this case, Your Python script is indeed executed, but after computing the possible completions it will exit, and thus not run anything else. In other words, it will be called repeatedly to provide completion suggestions, then once at the end with the full command line to do its actual work.

Showing only the files with certain extensions depending on the argument is "just" a matter of customizing the completer.
Your question is really just a duplicate of the other, but you seem to not see it. If the misunderstanding is actually on my side, please provide detailed explanations.

Lenormju
  • 4,078
  • 2
  • 8
  • 22