Change how long options are handled

This method is more concise, doesn't require the checks the previous
method did, allows for both the --option=value and --option value
syntaxes, and still uses getopts. I'm not sure if there is some good
reason to not pre-parse the arguments in this way though.

Addresses #12
long-opts
noctuid 9 years ago
parent b472eda261
commit 8748745132

@ -32,9 +32,9 @@ alt + s
```
### Basic Flags
`-s` should only be used for supported terminals and if the user wants to start a tmux or tmuxinator session. `-a` should be used to set up WM specific rules (which are required for the --width, --height, --xoff, and --yoff flags to work properly; see below for supported WMs). Long options require using `--opt=<arg>` as opposed to leaving a space. Refer to `tdrop --help` and the manpage for more complete instructions.
`-s` should only be used for supported terminals and if the user wants to start a tmux or tmuxinator session. `-a` should be used to set up WM specific rules (which are required for the --width, --height, --xoff, and --yoff flags to work properly; see below for supported WMs). Refer to `tdrop --help` and the manpage for more complete instructions.
Tdrop has basic checks to print errors for malformed commands (for example if a long option is used without `=`). If a tdrop command does not work, please run it in a terminal or check `/tmp/tdrop/log` for error messages and consult the manpage before making an issue.
Tdrop has basic checks to print errors for malformed commands (for example to require the positional argument). If a tdrop command does not work, please run it in a terminal or check `/tmp/tdrop/log` for error messages and consult the manpage before making an issue.
**Changes**:

61
tdrop

@ -61,7 +61,31 @@ clearwid=false
cancel_auto_show=true
auto_detect_wm=false
monitor_aware=false
while getopts :h:w:x:y:s:n:p:P:M:O:d:f:-:am opt
# convert all long options to short options
# this method is concise and allows --opt val (as opposed to only --opt=val)
args=$(echo "$*" | sed -r -e 's/--height(=| )/-h /' \
-e 's/--width(=| )/-w /' \
-e 's/--x-offset(=| )/-x /' \
-e 's/--y-offset(=| )/-y /' \
-e 's/--session(=| )/-s /' \
-e 's/--number(=| )/-n /' \
-e 's/--pre-command(=| )/-p /' \
-e 's/--post-command(=| )/-P /' \
-e 's/--post-unmap(=| )/-M /' \
-e 's/--oneshot-post(=| )/-O /' \
-e 's/--decoration-fix(=| )/-d /' \
-e 's/--program-flags(=| )/-f /' \
-e 's/--auto-detect-wm/-a/' \
-e 's/--monitor-aware/-m/' \
-e 's/--clear/-c/' \
-e 's/--no-cancel/-C/' \
-e 's/--help/-H/')
# reset arguments with new ones
# args is intentionally unquoted
set -- $args
while getopts :h:w:x:y:s:n:p:P:M:O:d:f:-:amcCH opt
do
case $opt in
h) height=$OPTARG;;
@ -78,38 +102,11 @@ do
f) program_flags=$OPTARG;;
a) auto_detect_wm=true;;
m) monitor_aware=true;;
-)
if [[ $OPTARG =~ ^(auto-detect-wm|monitor-aware|clear|no-cancel|help)$ ]] || \
[[ $OPTARG == *=* ]]; then
OPTION=${OPTARG%%=*}
OPTARG=${OPTARG#*=}
else
echo "Long options with args must be written as --opt=val." | \
tee -a /tmp/tdrop/log
exit 1
fi
case $OPTION in
height) height=$OPTARG;;
width) width=$OPTARG;;
x-offset) xoff=$OPTARG;;
y-offset) yoff=$OPTARG;;
session) session_name=$OPTARG;;
number) num=$OPTARG;;
pre-command) map_pre=$OPTARG;;
post-command) map_post=$OPTARG;;
post-unmap) unmap_post=$OPTARG;;
oneshot-post) oneshot_post=$OPTARG;;
decoration-fix) dec_fix=$OPTARG;;
program-flags) program_flags=$OPTARG;;
auto-detect-wm) auto_detect_wm=true;;
monitor-aware) monitor_aware=true;;
clear) clearwid=true;;
no-cancel) cancel_auto_show=false;;
help) print_help;;
*) print_help illegal_opt;;
esac;;
c) clearwid=true;;
C) cancel_auto_show=false;;
H) print_help;;
*) print_help illegal_opt;;
esac
esac
done
shift "$((OPTIND-1))"
program=$1

@ -60,13 +60,13 @@ If there are available settings for the detected window manager for the -p, -P,
\fB\-m\fR, \fB \-\-monitor-aware\fR
This option only applies for dropdowns (not auto-hiding and auto-showing). Specify that width and height percentages should be relative to the current monitor. If the monitor changes, this option will cause a dropdown to be resized to fit the given percentages. A negative argument is also allowed for '-w' and '-y' with this option (e.g. '-m -w -4') in which case the determined value will be that many pixels less than 100% of the screen size. This fixes the problem where 100% width may actually go over the screen due to window borders/decoration. Note that this option assumes xrandr is being used and requires xrandr to work. (default: false)
.TP
\fB \-\-clear\fR
\fB\-c\fR, \fB \-\-clear\fR
Used to clear a saved window id for the given program or 'current' instead of creating a dropdown; takes no argument.
.TP
\fB \-\-no-cancel\fR
\fB\-C\fR, \fB \-\-no-cancel\fR
Specifies that manually re-showing an auto-hidden window with tdrop should not cancel an auto_show; takes no argument. See examples.
.TP
\fB \-\-help\fR
\fB\-H\fR, \fB \-\-help\fR
Print help that is less awesome than this manpage; takes no argument.
.SH EXAMPLES
.SS Making Dropdowns

Loading…
Cancel
Save