Last active
October 1, 2025 11:15
-
-
Save hcartiaux/b605af737d9835f8371ca79ead4f92f2 to your computer and use it in GitHub Desktop.
Bash completion file for the OAR batch scheduler user commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # shellcheck shell=bash | |
| ################################################################################ | |
| # oar_completion.sh - bash completion script for OAR commands. | |
| ################################################################################ | |
| # Copyright (C) 2025 Hyacinthe Cartiaux <hyacinthe.cartiaux@uni.lu> | |
| # | |
| # This program is free software: you can redistribute it and/or modify | |
| # it under the terms of the GNU General Public License as published by | |
| # the Free Software Foundation, either version 3 of the License, or | |
| # (at your option) any later version. | |
| # | |
| # This program is distributed in the hope that it will be useful, | |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| # GNU General Public License for more details. | |
| # | |
| # You should have received a copy of the GNU General Public License | |
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| # Get existing queues | |
| _oar_get_queues() { | |
| # oarnotify -l | |
| echo admin besteffort default testing | |
| } | |
| # Get job types | |
| _oar_get_job_types() { | |
| echo besteffort timesharing idempotent cosystem deploy noop container inner token exotic | |
| } | |
| # Get current user's job IDs | |
| _oar_get_job_ids() { | |
| oarstat -u 2>/dev/null | awk 'NR > 2 && NF > 0 && $1 ~ /^[0-9]+$/ { print $1 }' | |
| } | |
| # Get current active users | |
| _oar_get_users() { | |
| oarstat | awk 'NR > 2 && NF >= 3 {print $(NF-4)}' | sort -u | |
| } | |
| # Get all resource ids | |
| _oar_get_resource_ids() { | |
| oarnodes | awk '/^resource_id:/ {print $2}' | sort -n | |
| } | |
| # Get all node names | |
| _oar_get_node_names() { | |
| oarnodes -l | |
| } | |
| # Get node names for the current job only | |
| _oar_get_node_names_current() { | |
| test -n "$OAR_NODEFILE" && uniq "$OAR_NODEFILE" | |
| } | |
| _oar_get_signals() { | |
| kill -l | awk '{for(i=1;i<=NF;i+=2){gsub(/\)/,"",$i); print $i}}' | |
| } | |
| _oar_path_completion() { | |
| while IFS= read -r item; do | |
| if [[ -d "${item}" ]]; then | |
| COMPREPLY+=("${item}/") | |
| else | |
| COMPREPLY+=("${item}") | |
| fi | |
| done < <(compgen $1 -- "${cur}") | |
| if [[ ${#COMPREPLY[@]} -eq 1 && -d "${COMPREPLY[0]}" ]]; then | |
| compopt -o nospace | |
| fi | |
| } | |
| _oar_file_completion() { | |
| _oar_path_completion -f | |
| } | |
| _oar_dir_completion() { | |
| _oar_path_completion -d | |
| } | |
| # Completion function for oarstat (bonus) | |
| _oarstat_completion() { | |
| local cur prev opts | |
| COMPREPLY=() | |
| cur="${COMP_WORDS[COMP_CWORD]}" | |
| prev="${COMP_WORDS[COMP_CWORD-1]}" | |
| if [[ "${cur}" =~ ^-.*$ ]] ; then | |
| opts="-j --job -f --full -s --state -u --user --array | |
| -c --compact -g --gantt -e --events -p --properties --accounting | |
| --sql --format -D --dumper -X --xml -Y --yaml -J --json -V | |
| --version -h --help" | |
| mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "${cur}") | |
| return 0 | |
| fi | |
| case ${prev} in | |
| -j|--job|--array) | |
| # Complete with job IDs after -j, --job or --array | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_job_ids)" -- "${cur}") | |
| ;; | |
| -u|--user) | |
| # Complete with current active users after -u or --user | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_users)" -- "${cur}") | |
| ;; | |
| --format) | |
| COMPREPLY=(1 2 3) | |
| ;; | |
| esac | |
| return 0 | |
| } | |
| # Completion function for oarhold/oarresume | |
| _oarhold_resume_completion() { | |
| local cur prev cmd opts | |
| COMPREPLY=() | |
| cur="${COMP_WORDS[COMP_CWORD]}" | |
| prev="${COMP_WORDS[COMP_CWORD-1]}" | |
| cmd="${COMP_WORDS[0]}" | |
| # If current word looks like a job ID or previous was the command itself | |
| if [[ "${cur}" =~ ^[0-9]+$ ]] || [[ "${prev}" == "${cmd}" ]]; then | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_job_ids)" -- "${cur}") | |
| return 0 | |
| fi | |
| # Provide command options | |
| case "${cmd}" in | |
| oarhold) | |
| opts="-r --running --array --sql -h --help -V --version" | |
| ;; | |
| oarresume) | |
| opts="--array --sql -h --help -V --version" | |
| ;; | |
| esac | |
| if [[ "${cur}" =~ ^-.*$ ]] ; then | |
| mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "${cur}") | |
| fi | |
| return 0 | |
| } | |
| # Completion function for oardel | |
| _oardel_completion() { | |
| local cur prev opts | |
| COMPREPLY=() | |
| cur="${COMP_WORDS[COMP_CWORD]}" | |
| prev="${COMP_WORDS[COMP_CWORD-1]}" | |
| if [[ "${cur}" =~ ^-.*$ ]] ; then | |
| opts="-c --checkpoint -s --signal -b --besteffort | |
| --array --sql --force-terminate-finishing-job | |
| -h --help -V --version" | |
| mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "${cur}") | |
| return 0 | |
| fi | |
| case ${prev} in | |
| oardel) | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_job_ids)" -- "${cur}") | |
| ;; | |
| -s|--signal) | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_signals)" -- "${cur}") | |
| ;; | |
| *) | |
| if [[ "${cur}" =~ ^[0-9]+$ ]] ; then | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_job_ids)" -- "${cur}") | |
| fi | |
| ;; | |
| esac | |
| return 0 | |
| } | |
| # Completion function for oarnodes | |
| _oarnodes_completion() { | |
| local cur prev opts | |
| COMPREPLY=() | |
| cur="${COMP_WORDS[COMP_CWORD]}" | |
| prev="${COMP_WORDS[COMP_CWORD-1]}" | |
| if [[ "${cur}" =~ ^-.*$ ]] ; then | |
| opts="-s --state -j --jobs -l --list -e --events | |
| -r --resource | |
| --sql -D --dumper -X --xml -Y --yaml -J --json | |
| -h --help -V --version" | |
| mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "${cur}") | |
| return 0 | |
| fi | |
| case ${prev} in | |
| -r|--resource) | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_resource_ids)" -- "${cur}") | |
| ;; | |
| --) | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_node_names)" -- "${cur}") | |
| ;; | |
| *) | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_node_names)" -- "${cur}") | |
| ;; | |
| esac | |
| return 0 | |
| } | |
| # Completion function for oarcp/oarsh | |
| _oarcp_oarsh_completion() { | |
| local cur prev | |
| COMPREPLY=() | |
| cur="${COMP_WORDS[COMP_CWORD]}" | |
| prev="${COMP_WORDS[COMP_CWORD-1]}" | |
| if [[ "${prev}" == "oarcp" ]] ; then | |
| _oar_file_completion | |
| return 0 | |
| fi | |
| if [[ "${cur}" =~ ^[^-]+ ]] ; then | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_node_names_current)" -- "${cur}") | |
| return 0 | |
| fi | |
| } | |
| # Completion function for oarsub | |
| _oarsub_completion() { | |
| local cur prev opts | |
| COMPREPLY=() | |
| cur="${COMP_WORDS[COMP_CWORD]}" | |
| prev="${COMP_WORDS[COMP_CWORD-1]}" | |
| if [[ "${cur}" =~ ^-.*$ ]] ; then | |
| opts="-I -C --connect --interactive -l --resource | |
| --array --array-param-file -S --scanscript -q --queue -p --property | |
| -r --reservation --checkpoint -s --signal -t --type -d --directory | |
| --project -n --name -a --anterior --notify --resubmit -k --use-job-key | |
| -e --export-job-key-to-file -i --import-job-key-from-file | |
| --import-job-key-inline -O -E --hold | |
| -D --dumper -X --xml -Y --yaml -J --json | |
| -h --help -V --version" | |
| mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "${cur}") | |
| return 0 | |
| fi | |
| case ${prev} in | |
| -C|--connect) | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_job_ids)" -- "${cur}") | |
| ;; | |
| -q|--queue) | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_queues)" -- "${cur}") | |
| ;; | |
| -s|--signal) | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_signals)" -- "${cur}") | |
| ;; | |
| -t|--type) | |
| mapfile -t COMPREPLY < <(compgen -W "$(_oar_get_job_types)" -- "${cur}") | |
| ;; | |
| -O|-E|-S|--scanscript|-e|--export-job-key-to-file|-i|--import-job-key-from-file|--array-param-file) | |
| _oar_file_completion | |
| ;; | |
| -d|--directory) | |
| _oar_dir_completion | |
| ;; | |
| *) | |
| _oar_file_completion | |
| ;; | |
| esac | |
| return 0 | |
| } | |
| # Register completion functions | |
| complete -F _oarstat_completion oarstat | |
| complete -F _oarhold_resume_completion oarhold | |
| complete -F _oarhold_resume_completion oarresume | |
| complete -F _oardel_completion oardel | |
| complete -F _oarnodes_completion oarnodes | |
| complete -F _oarcp_oarsh_completion oarcp | |
| complete -F _oarcp_oarsh_completion oarsh | |
| complete -F _oarsub_completion oarsub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment