aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-10-28 21:21:34 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2013-10-28 21:21:34 +0100
commit91984f8d551650b5ed79b9428c1098a10e922e4e (patch)
tree3e2a457eeeca84e1717c7280ddda8c15923dcff2
parentf47f556cd49582d34122802c120ce00cf4da2e5a (diff)
downloadapp.sh-91984f8d551650b5ed79b9428c1098a10e922e4e.tar.gz
app.sh-91984f8d551650b5ed79b9428c1098a10e922e4e.tar.bz2
app.sh-91984f8d551650b5ed79b9428c1098a10e922e4e.tar.xz
app.sh-91984f8d551650b5ed79b9428c1098a10e922e4e.zip
o Working a bit on the style guide.
-rw-r--r--STYLE-GUIDE.txt (renamed from STYLE-GUIDE.md)48
1 files changed, 40 insertions, 8 deletions
diff --git a/STYLE-GUIDE.md b/STYLE-GUIDE.txt
index 207b988..1f0a9cf 100644
--- a/STYLE-GUIDE.md
+++ b/STYLE-GUIDE.txt
@@ -1,13 +1,13 @@
-Style Guide
------------
+Bash Style Guide
+================
Basic
-=====
+-----
* Indent: two spaces. Spaces >> tabs.
Creating `usage()` and `help()`
-===============================
+-------------------------------
The general semantics
---------------------
@@ -34,9 +34,11 @@ Formatting of usage output
* Enclose required arguments in angle brackets: `-v <version>`
* Enclose optional arguments in square brackets: `[-h hook]`
- usage_text() {
- echo "usage: $0 -v <version> [-h hook]"
- }
+----------------------------------------------------------------------
+usage_text() {
+ echo "usage: $0 -v <version> [-h hook]"
+}
+----------------------------------------------------------------------
Executing Commands
==================
@@ -49,9 +51,39 @@ Parsing options
Applications should always check for extra options or define how
they're handled.
+By always shifting out processed arguments `$@` will be an array with
+the remaining arguments. If only command options are allowed they can
+be shifted in one go with the expression `shift $(($OPTIND - 1))`.
+
+----------------------------------------------------------------------
+while getopts "f:" opt
+do
+ case $opt in
+ f)
+ file="$OPTARG"
+ shift 2;
+ ;;
+ *)
+ usage "Unknown argument: $OPTARG"
+ ;;
+ esac
+done
+
+shift $(($OPTIND - 1))
+----------------------------------------------------------------------
+
+After all arguments have been processed, check for extra arguments:
+
+----------------------------------------------------------------------
+if [[ $# != 0 ]]
+then
+ usage "Extra arguments"
+fi
+----------------------------------------------------------------------
+
Resources
---------
* Parameter expansion: <http://wiki.bash-hackers.org/syntax/pe>
-<!-- vim: set ft=markdown: -->
+// vim: set ft=asciidoc: