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

add default_complete option #83

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

add default_complete option #83

wants to merge 1 commit into from

Conversation

casperdcl
Copy link
Collaborator

Fixes #65 /CC @tshu-w

@casperdcl casperdcl added the enhancement New feature or request label Jun 21, 2022
@casperdcl casperdcl self-assigned this Jun 21, 2022
@sourcery-ai

This comment was marked as resolved.

@tshu-w
Copy link

tshu-w commented Jun 22, 2022

@casperdcl Hi, I try this commit but it doesn't work on my side.
Steps:

  1. install shtab
pip install git+https://github.com/iterative/shtab.git@default_complete
  1. change code to use DEFAULT_FUNCTIONS:
shtab.add_argument_to(self.parser, ["-s", "--print-completion"], default_complete=DEFAULT_FUNCTIONS)
  1. generate completion script:
run -s zsh

However, the script doesn't contains _default either.

……
_shtab_run_tune_options=(
  "(- :)"{-h,--help}"[show this help message and exit]"
  {-c,--config}"[Path to a configuration file in json or yaml format.]:config:"
  "--print_config[Print the configuration after applying all other arguments and exit.]:print_config:"
  "--seed_everything[Set to an int to run seed_everything with this value before classes instantiation]:seed_everything:"
  "--trainer[Path to a configuration file.]:trainer:"
……

@tshu-w
Copy link

tshu-w commented Aug 7, 2024

Here is the minimal reproduction:

  1. pip install git+https://github.com/iterative/shtab.git@default_complete

  2. create test.py

#!/usr/bin/env python
import argparse
import shtab  # for completion magic

def get_main_parser():
    parser = argparse.ArgumentParser(prog="pathcomplete")
    shtab.add_argument_to(parser, ["-s", "--print-completion"], default_complete="_default")  # magic!
    # file & directory tab complete
    parser.add_argument("file", nargs="?")
    parser.add_argument("--dir", default=".")
    return parser

if __name__ == "__main__":
    parser = get_main_parser()
    args = parser.parse_args()
    print("received <file>=%r --dir=%r" % (args.file, args.dir))
  1. print completion
λ python test.py -s zsh
#compdef pathcomplete

# AUTOMATCALLY GENERATED by `shtab`


_shtab_pathcomplete_commands() {
  local _commands=(

  )
  _describe 'pathcomplete commands' _commands
}

_shtab_pathcomplete_options=(
  "(- :)"{-h,--help}"[show this help message and exit]"
  {-s,--print-completion}"[print shell completion script]:print_completion:(bash zsh tcsh)"
  "--dir[]:dir:"
)


_shtab_pathcomplete() {
  local context state line curcontext="$curcontext"

  _arguments -C $_shtab_pathcomplete_options \
    ': :_shtab_pathcomplete_commands' \
    '*::: :->pathcomplete'

  case $state in
    pathcomplete)
      words=($line[1] "${words[@]}")
      (( CURRENT += 1 ))
      curcontext="${curcontext%:*:*}:_shtab_pathcomplete-$line[1]:"
      case $line[1] in

      esac
  esac
}



typeset -A opt_args
_shtab_pathcomplete "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use _default instead of nothing as dest in complete_zsh
2 participants