From 4b1c2d9b74bc059c470fc634af017501d4f17610 Mon Sep 17 00:00:00 2001 From: buxxket Date: Fri, 1 Aug 2025 18:21:32 +1000 Subject: [PATCH] Added shell completion --- CMakeLists.txt | 11 +++++++++++ README.md | 30 ++++++++++++++++++++++++++++ contrib/_mbpoll | 47 ++++++++++++++++++++++++++++++++++++++++++++ contrib/mbpoll.bash | 48 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 contrib/_mbpoll create mode 100644 contrib/mbpoll.bash diff --git a/CMakeLists.txt b/CMakeLists.txt index d72c5ea..456cd23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,6 +184,17 @@ target_link_libraries(mbpoll ${LINK_OPTIONS}) install(TARGETS mbpoll RUNTIME DESTINATION bin PERMISSIONS ${PROGRAM_PERMISSIONS}) +install( + FILES contrib/_mbpoll + DESTINATION share/zsh/site-functions +) + +# Install Bash completion script +install( + FILES contrib/mbpoll.bash + DESTINATION share/bash-completion/completions +) + if(WIN32) add_custom_command( TARGET mbpoll PRE_BUILD diff --git a/README.md b/README.md index 0199b57..6374a82 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,36 @@ That's all ! For Windows, you can follow the instructions in the [README-WINDOWS.md](README-WINDOWS.md) file. +## Shell Completion + +### Zsh + +A Zsh completion script is available for `mbpoll`, enabling advanced tab completion for all options and arguments. + +- If you use CMake `make install`, the script `contrib/_mbpoll` will be installed to your system's zsh site-functions directory (typically `/usr/share/zsh/site-functions`). +- If you install manually, copy `contrib/_mbpoll` to a directory in your `$fpath`. Then restart your shell or run: + ```sh + autoload -Uz compinit + compinit + ``` +- Type `mbpoll ` in Zsh to enjoy completions! + +### Bash + +A Bash completion script is also provided. + +- If you use CMake `make install`, the script `contrib/mbpoll.bash` will be installed to your system's bash-completion directory (typically `/usr/share/bash-completion/completions/`). +- If you install manually, copy `contrib/mbpoll.bash` to `/etc/bash_completion.d/` or `/usr/share/bash-completion/completions/`. +- Restart your shell or run: + ```sh + source /usr/share/bash-completion/completions/mbpoll + ``` +- Now, typing `mbpoll ` in Bash will complete options and arguments. + +### Note for Distributions + +If your system uses a different completion directory, set `CMAKE_INSTALL_PREFIX` or adjust the install location as needed. + ## Examples The following command is used to read the input registers 1 and 2 of the diff --git a/contrib/_mbpoll b/contrib/_mbpoll new file mode 100644 index 0000000..864e4ee --- /dev/null +++ b/contrib/_mbpoll @@ -0,0 +1,47 @@ +#compdef mbpoll + +__mbpoll_types() { + local -a types + types=( + '0:Discrete output (coil) data type (binary 0 or 1)' + '1:Discrete input data type (binary 0 or 1)' + '3:16-bit input register data type' + '3\:int16:16-bit input register data type with signed int display' + '3\:hex:16-bit input register data type with hex display' + '3\:string:16-bit input register data type with string (char) display' + '3\:int:32-bit integer data type in input register table' + '3\:float:32-bit float data type in input register table' + '4:16-bit output (holding) register data type (default)' + '4\:int16:16-bit output (holding) register data type with signed int display' + '4\:hex:16-bit output (holding) register data type with hex display' + '4\:string:16-bit output (holding) register data type with string (char) display' + '4\:int:32-bit integer data type in output (holding) register table' + '4\:float:32-bit float data type in output (holding) register table' + ) + _describe -t types 'type' types +} + +_arguments \ + '-m[mode (rtu or tcp, TCP is default)]:mode:(rtu tcp)' \ + '-a[slave address (1-255 for rtu, 0-255 for tcp, 1 is default)]:address:' \ + '-r[start reference (1 is default)]:reference:' \ + '-c[number of values to read (1-125, 1 is default)]:count:' \ + '-t[data type]:type:__mbpoll_types' \ + '-l[poll rate in ms, (> 100, 1000 is default)]:rate (ms):' \ + '-o[time-out in seconds (0.01 - 10.00, 1.00 s is default)]:timeout (s):' \ + '-p[TCP port number (502 is default)]:port:' \ + '-b[baudrate (1200-921600, 19200 is default)]:baudrate:' \ + '-d[databits (7 or 8, 8 for RTU)]:databits:(7 8)' \ + '-s[stopbits (1 or 2, 1 is default)]:stopbits:(1 2)' \ + '-P[parity (none, even, odd, even is default)]:parity:(none even odd)' \ + '-0[First reference is 0 (PDU addressing) instead 1]' \ + '-W[Using function 10 for write a single register]' \ + '-B[Big endian word order for 32-bit integer and float]' \ + '-1[Poll only once only, otherwise every poll rate interval]' \ + '-q[Quiet mode. Minimum output only]' \ + '-u[Read device description (RTU only)]' \ + '-R[RS-485 mode (/RTS on (0) after sending)]' \ + '-F[RS-485 mode (/RTS on (0) when sending)]' \ + '-h[Print this help summary page]' \ + '-V[Print version and exit]' \ + '-v[Verbose mode]' diff --git a/contrib/mbpoll.bash b/contrib/mbpoll.bash new file mode 100644 index 0000000..b5289f4 --- /dev/null +++ b/contrib/mbpoll.bash @@ -0,0 +1,48 @@ +# bash completion for mbpoll + +_mbpoll() +{ + local cur prev opts + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + opts="-m -a -r -c -t -l -o -p -b -d -s -P -0 -W -B -1 -q -u -R -F -h -V -v" + + # Completions for -m + if [[ "$prev" == "-m" ]]; then + COMPREPLY=( $(compgen -W "rtu tcp" -- "$cur") ) + return 0 + fi + + # Completions for -d + if [[ "$prev" == "-d" ]]; then + COMPREPLY=( $(compgen -W "7 8" -- "$cur") ) + return 0 + fi + + # Completions for -s + if [[ "$prev" == "-s" ]]; then + COMPREPLY=( $(compgen -W "1 2" -- "$cur") ) + return 0 + fi + + # Completions for -P + if [[ "$prev" == "-P" ]]; then + COMPREPLY=( $(compgen -W "none even odd" -- "$cur") ) + return 0 + fi + + # Completions for -t + if [[ "$prev" == "-t" ]]; then + COMPREPLY=( $(compgen -W "0 1 3 3:int16 3:hex 3:string 3:int 3:float 4 4:int16 4:hex 4:string 4:int 4:float" -- "$cur") ) + return 0 + fi + + # Complete options + if [[ "$cur" == -* ]]; then + COMPREPLY=( $(compgen -W "$opts" -- "$cur") ) + return 0 + fi +} + +complete -F _mbpoll mbpoll