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

zsh: recurse over subcommand #73

Merged
merged 3 commits into from
Apr 19, 2022
Merged

Conversation

skshetry
Copy link
Member

@skshetry skshetry commented Apr 13, 2022

I was just able to make it work with DVC, still unsure if recursing through ArgumentParser is correct here (consider this as a WIP for now, more testing is required).

You can try it out with dvc. Closes #33.

Example output for dvc metrics
#compdef dvc

# AUTOMATCALLY GENERATED by `shtab`

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

  _arguments -C $_shtab_dvc_options \
    ': :_shtab_dvc_commands' \
    '*::: :->dvc'

  case $state in
    dvc)
      words=($line[1] "${words[@]}")
      (( CURRENT += 1 ))
      curcontext="${curcontext%:*:*}:_shtab_dvc-$line[1]:"
      case $line[1] in
        metrics) _shtab_dvc_metrics ;;
        params) _shtab_dvc_params ;;
        plots) _shtab_dvc_plots ;;
      esac
  esac
}

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

  _arguments -C $_shtab_dvc_metrics_options \
    ': :_shtab_dvc_metrics_commands' \
    '*::: :->metrics'

  case $state in
    metrics)
      words=($line[1] "${words[@]}")
      (( CURRENT += 1 ))
      curcontext="${curcontext%:*:*}:_shtab_dvc_metrics-$line[1]:"
      case $line[1] in
        diff) _arguments -C $_shtab_dvc_metrics_diff_options ;;
        show) _arguments -C $_shtab_dvc_metrics_show_options ;;
      esac
  esac
}

_shtab_dvc_commands() {
  local _commands=(
    "metrics:Commands to display and compare metrics."
    "params:Commands to display params."
    "plots:Commands to visualize and compare plot metrics in structured files (JSON, YAML, CSV, TSV)."
  )
  _describe 'dvc commands' _commands
}


_shtab_dvc_metrics_commands() {
  local _commands=(
    "diff:Show changes in metrics between commits in the DVC repository, or between a commit and the workspace."
    "show:Print metrics, with optional formatting."
  )
  _describe 'dvc metrics commands' _commands
}

_shtab_dvc_options=(
  "*"{-q,--quiet}"[Be quiet.]"
  "*"{-v,--verbose}"[Be verbose.]"
  "(- :)"{-h,--help}"[Show this help message and exit.]"
  {-V,--version}"[Show program\'s version.]:version:"
  "--cd[Change to directory before executing.]:cd:"
)

_shtab_dvc_metrics_options=(
  "(- :)"{-h,--help}"[show this help message and exit]"
  "*"{-q,--quiet}"[Be quiet.]"
  "*"{-v,--verbose}"[Be verbose.]"
)

_shtab_dvc_metrics_diff_options=(
  "(- :)"{-h,--help}"[show this help message and exit]"
  "*"{-q,--quiet}"[Be quiet.]"
  "*"{-v,--verbose}"[Be verbose.]"
  "--targets[Specific metrics file(s) to compare (even if not found as \`metrics\` in \`dvc.yaml\`). Using -R, directories to search metrics files in can also be given.Shows all tracked metrics by default.]:targets:_files"
  {-R,--recursive}"[If any target is a directory, recursively search and process metrics files.]"
  "--all[Show unchanged metrics as well.]"
  {--json,--show-json}"[Show output in JSON format.]"
  {--md,--show-md}"[Show tabulated output in the Markdown format (GFM).]"
  "--no-path[Don\'t show metric path.]"
  "--precision[Round metrics to \`n\` digits precision after the decimal point. Rounds to 5 digits by default.]:precision:"
  ":Old Git commit to compare (defaults to HEAD):"
  ":New Git commit to compare (defaults to the current workspace):"
)

_shtab_dvc_metrics_show_options=(
  "(- :)"{-h,--help}"[show this help message and exit]"
  "*"{-q,--quiet}"[Be quiet.]"
  "*"{-v,--verbose}"[Be verbose.]"
  {-a,--all-branches}"[Show metrics for all branches.]"
  {-T,--all-tags}"[Show metrics for all tags.]"
  {-A,--all-commits}"[Show metrics for all commits.]"
  {--json,--show-json}"[Show output in JSON format.]"
  {--md,--show-md}"[Show tabulated output in the Markdown format (GFM).]"
  {-R,--recursive}"[If any target is a directory, recursively search and process metrics files.]"
  "--precision[Round metrics to \`n\` digits precision after the decimal point. Rounds to 5 digits by default.]:precision:"
  "(*)::Limit command scope to these metrics files. Using -R, directories to search metrics files in can also be given.:_files"
)

# Custom Preamble

# End Custom Preamble

typeset -A opt_args
_shtab_dvc "$@"

@skshetry skshetry requested a review from casperdcl April 13, 2022 13:20
@casperdcl casperdcl added enhancement New feature or request shell-zsh p1-important High priority labels Apr 19, 2022
shtab/__init__.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@casperdcl casperdcl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just a minor patch

@casperdcl casperdcl merged commit cf19980 into iterative:master Apr 19, 2022
@skshetry skshetry deleted the zsh-subcommand branch April 19, 2022 12:08
@casperdcl
Copy link
Collaborator

/tag v1.5.4 cf19980

@efiop
Copy link
Contributor

efiop commented Apr 19, 2022

@skshetry Outstanding work 🔥

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

Successfully merging this pull request may close these issues.

zsh sub-subcommand recursion
3 participants