iterative / shtab

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

add `default_complete` option #83

Open casperdcl opened 2 years ago

casperdcl commented 2 years ago

Fixes #65 /CC @tshu-w

sourcery-ai[bot] commented 2 years ago

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 0.11%.

Quality metrics Before After Change
Complexity 41.79 ⛔ 42.09 ⛔ 0.30 👎
Method Length 110.09 🙂 112.03 🙂 1.94 👎
Working memory 18.29 ⛔ 18.28 ⛔ -0.01 👍
Quality 29.55% 😞 29.44% 😞 -0.11% 👎
Other metrics Before After Change
Lines 1055 1068 13
Changed files Quality Before Quality After Quality Change
shtab/init.py 29.55% 😞 29.44% 😞 -0.11% 👎

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
shtab/init.py get_bash_commands 78 ⛔ 506 ⛔ 26 ⛔ 2.72% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
shtab/init.py complete_zsh 62 ⛔ 754 ⛔ 25 ⛔ 3.53% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
shtab/init.py get_bash_commands.recurse 58 ⛔ 452 ⛔ 26 ⛔ 4.43% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
shtab/init.py complete_tcsh 57 ⛔ 403 ⛔ 19 ⛔ 8.18% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
shtab/init.py complete_zsh.recurse 18 🙂 214 ⛔ 16 ⛔ 31.98% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

tshu-w commented 2 years ago

@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
  2. change code to use DEFAULT_FUNCTIONS:
    shtab.add_argument_to(self.parser, ["-s", "--print-completion"], default_complete=DEFAULT_FUNCTIONS)
  3. 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 commented 2 months ago

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 =%r --dir=%r" % (args.file, args.dir))

3. print completion
```console
λ 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 "$@"