Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <TAB>` 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 <TAB>` 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
Expand Down
47 changes: 47 additions & 0 deletions contrib/_mbpoll
Original file line number Diff line number Diff line change
@@ -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]'
48 changes: 48 additions & 0 deletions contrib/mbpoll.bash
Original file line number Diff line number Diff line change
@@ -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