Skip to content

Completion only works as first argument #69

Description

@bitozoid

If I understand it correctly, --complete should be the last argument for tome completion to work. However, it seems to ignore the rest of the arguments.

I created a sample script (see below) to test completion, it seems not to work as expected.

To test:

  • create a test directory
[ -d /tmp/tome-test ] || mkdir /tmp/tome-test
  • copy script contents to /tmp/tome-test/testcomp.

  • prepare the script and tome

chmod +x /tmp/tome-test/testcomp
eval "$(tome init test /tmp/tome-test bash)"
  • Check that the script works as expected:
/tmp/tome-test/testcomp --complete
# -m
/tmp/tome-test/testcomp -m --complete
# hello
# hi
# foo
/tmp/tome-test/testcomp -m h --complete
# hello
# hi
  • Try tome completion: always returns -m.

Sample script testcomp:

#!/bin/bash
# COMPLETE

sample_modes="hello hi foo"

complete_modes() {
  local modearg="${1:-}"
  if [ -z "$modearg" ]; then
    echo "$sample_modes" | sed 's/ /\n/g'
  else
    compgen -W "$sample_modes" -- "$modearg"
  fi
  exit 0
}

parse_args() {
  local opt last_opt last_opt_arg

  if [ "$1" = "--complete" ]; then
    echo "-m"
    exit 0
  fi

  while [ "${!OPTIND:-}" != "--complete" ] &&
    getopts "m:" opt; do
    case $opt in
    m)
      mode="$OPTARG"
      [ "$mode" = "--complete" ] && complete_modes ""
      ;;
    esac
    last_opt="$opt"
    # last_opt_arg="$OPTARG"
  done
  shift $((OPTIND - 1))

  # assume all arguments already processed at this point
  if [ "$1" = "--complete" ]; then
    if [ "$#" -gt 1 ]; then
      echo "ERROR: '--complete' must be the last argument."
      exit 1
    fi
    case $last_opt in
    m)
      complete_modes "$mode"
      ;;
    ?)
      exit 1
      ;;
    esac
  fi
}

parse_args "$@"

case "$mode" in
hello)
  echo "Hello World!"
  ;;
hi)
  echo "Hi World!"
  ;;
foo)
  echo "foo bar"
  ;;
*)
  echo "unknown mode: $mode"
  exit 1
  ;;
esac

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions