generate_autocomplete.sh 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/bash
  2. if [ "$1" != "" ]; then
  3. target="$1"
  4. else
  5. target="./xrnlib-cli-autocomplete.sh"
  6. fi
  7. # get the checker file
  8. pathtolexer=$(cat filelist.json | grep "lexcheck.h" | tr -d '"' | tr -d ',' | sed 's/ *//' | sed 's/^\./\.\.\/\.\./')
  9. # get the list of options that need to be followed by a writing path
  10. str_to_eval=$(cat $pathtolexer | grep XARG_PATH_W | sed '1,2d' | awk '{print $2}' | sed 's/^[^_]*[_]//' | sed 's/_/-/g' | tr '[:upper:]' '[:lower:]' | sort | uniq | sed 's/^/\"-/' | sed 's/$/\"/' | tr -s '\n' ' ' | sed 's/^/options_followed_by_w_path=(/' | sed 's/$/)/' )
  11. eval "$str_to_eval"
  12. # get the list of options that need to be followed by a writing path
  13. str_to_eval=$(cat $pathtolexer | grep XARG_PATH_R | sed '1,2d' | awk '{print $2}' | sed 's/^[^_]*[_]//' | sed 's/_/-/g' | tr '[:upper:]' '[:lower:]' | sort | uniq | sed 's/^/\"-/' | sed 's/$/\"/' | tr -s '\n' ' ' | sed 's/^/options_followed_by_r_path=(/' | sed 's/$/)/' )
  14. eval "$str_to_eval"
  15. # get the list of all possible options and arguments
  16. str_to_eval="$(cat $pathtolexer | grep -v XSTR_ALL_ARGS_UNIQ | grep XSTR_ALL_ARGS | sed 's/.*{/{/g' | tail -n 1 | tr -s '{' '(' | tr -s '}' ')' | tr -d ';' | tr -d ',' | sed 's/^/all_possible_args=/')"
  17. eval "$str_to_eval"
  18. #echo "${all_possible_args[@]}"
  19. # create command and strip_command lists
  20. list_strip_commands=()
  21. list_of_commands=()
  22. for word in ${all_possible_args[@]}; do
  23. # check if it is a command
  24. if [[ $word == --* ]]; then
  25. list_of_commands+=("$word")
  26. if [ "$word" != "--help" ] && [ "$word" != "--example" ] && [ "$word" != "--script" ] && [ "$word" != "--version" ] ; then
  27. if [ "$word" != "--decoding-conf" ] && [ "$word" != "--encoding-conf" ] && [ "$word" != "--permutation-conf" ] && [ "$word" != "--sequence-conf" ] ; then
  28. if [ "$word" != "--arithmetic-conf" ] && [ "$word" != "--logging-conf" ] ; then
  29. list_strip_commands+=("${word#??}")
  30. fi
  31. fi
  32. fi
  33. fi
  34. done
  35. all_possible_args_str=$(echo "${all_possible_args[@]}" | sed 's/ /" "/g' | sed 's/^/all_possible_args=("/' | sed 's/$/")/' )
  36. #echo $all_possible_args_str
  37. tree_of_commands_str=$(echo "${list_strip_commands[@]}" | tr -s ' ' '\n' | xargs -I _ sh -c "xrnlib-cli --help _ | tail -n +3 | sed -E 's/^(.{1,50}).*$/\1/' | sed '/^a valid example/q' | head -n -1 | grep -v ' -' | tr -s '\n' ' ' | sed 's/$/\n/' | sed 's/^ //' " | tr -s '\n' '+' | sed 's/+$//' | sed 's/^/--logging-conf --help +--logging-conf --example +--logging-conf --script +/' | sed 's/^/tree_of_commands="/' | sed 's/$/"/' )
  38. #echo $tree_of_commands_str
  39. eval "$tree_of_commands_str"
  40. possible_first_commands_str=$( echo "$tree_of_commands" | tr -s '+' '\n' | sed 's/--logging-conf//' | awk '{ print $1 }' | sed 's/^/--logging-conf /' | tr -s ' ' '\n' | sort | uniq | tr '\n' '+' | sed 's/+/" "/g' | sed 's/^/--example --scripts --help --version /' | sed 's/^/possible_first_commands=( "/' | sed 's/"$/)/' )
  41. #echo $possible_first_commands_str
  42. options_followed_by_w_path_str=$( echo ${options_followed_by_w_path[@]} | tr -s ' ' '\n' | sed 's/^/\"/' | sed 's/$/\"/' | tr -s '\n' ' ' | sed 's/$/)/' | sed 's/^/options_followed_by_w_path=(/' )
  43. #echo $options_followed_by_w_path_str
  44. options_followed_by_r_path_str=$( echo ${options_followed_by_r_path[@]} | tr -s ' ' '\n' | sed 's/^/\"/' | sed 's/$/\"/' | tr -s '\n' ' ' | sed 's/$/)/' | sed 's/^/options_followed_by_r_path=(/' )
  45. #echo $options_followed_by_r_path_str
  46. cat ../../archives/resources/miscellaneus/xrnlib-cli-autocomplete-template.txt | sed "s/#autocompleteall_possible_args/$all_possible_args_str/" \
  47. | sed "s/#autocompletetree_of_commands/$tree_of_commands_str/" \
  48. | sed "s/#autocompleteoptions_followed_by_w_path/$options_followed_by_w_path_str/" \
  49. | sed "s/#autocompleteoptions_followed_by_r_path/$options_followed_by_r_path_str/" \
  50. | sed "s/#autocompletepossible_first_commands/$possible_first_commands_str/" > \
  51. $target