diff options
author | Kate Ward <kate.ward@forestent.com> | 2016-01-10 16:53:04 +0100 |
---|---|---|
committer | Kate Ward <kate.ward@forestent.com> | 2016-01-10 16:53:04 +0100 |
commit | 4b66ecdc0d3f6b89d3132b75f5aca1773a29a1a3 (patch) | |
tree | 9668b6e23a484f660f25ff34c98876db0276ffe6 /examples | |
parent | b11509fad7f5e9e66a734116fcec7c418419ee1f (diff) | |
download | shflags-4b66ecdc0d3f6b89d3132b75f5aca1773a29a1a3.tar.gz |
restructured source for GitHub
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/debug_output.sh | 63 | ||||
-rwxr-xr-x | examples/hello_world.sh | 25 | ||||
-rwxr-xr-x | examples/write_date.sh | 59 |
3 files changed, 147 insertions, 0 deletions
diff --git a/examples/debug_output.sh b/examples/debug_output.sh new file mode 100755 index 0000000..d54635f --- /dev/null +++ b/examples/debug_output.sh @@ -0,0 +1,63 @@ +#!/bin/sh +# +# Copyright 2008 Kate Ward. All Rights Reserved. +# Released under the LGPL (GNU Lesser General Public License) +# +# Author: kate.ward@forestent.com (Kate Ward) +# +# This script does the very simple job of echoing some text. If a '-d' (or +# '--debug') flag is given, additinal "debug" output is enabled. +# +# This script demonstrates the use of a boolean flag to enable custom +# functionality in a script. +# +# Try running these: +# $ ./debug_output.sh speak +# $ ./debug_output.sh sing +# $ ./debug_output.sh --debug sing + +# source shflags +. ../src/shflags + +# define flags +DEFINE_boolean 'debug' false 'enable debug mode' 'd' +FLAGS_HELP=`cat <<EOF +commands: + speak: say something + sing: sing something +EOF` + + +debug() +{ + [ ${FLAGS_debug} -eq ${FLAGS_TRUE} ] || return + echo "DEBUG: $@" >&2 +} + +die() { + [ $# -gt 0 ] && echo "error: $@" >&2 + flags_help + exit 1 +} + + +# parse the command-line +FLAGS "$@" || exit 1 +eval set -- "${FLAGS_ARGV}" + +command=$1 +case ${command} in + '') die ;; + + speak) + debug "I'm getting ready to say something..." + echo 'The answer to the question "What is the meaning of life?" is "42".' + ;; + + sing) + debug "I'm getting ready to sing something..." + echo 'I love to sing! La diddy da dum!' + ;; + + *) die "unrecognized command (${command})" ;; +esac diff --git a/examples/hello_world.sh b/examples/hello_world.sh new file mode 100755 index 0000000..6fb2cb0 --- /dev/null +++ b/examples/hello_world.sh @@ -0,0 +1,25 @@ +#!/bin/sh +# +# Copyright 2008 Kate Ward. All Rights Reserved. +# Released under the LGPL (GNU Lesser General Public License) +# +# Author: kate.ward@forestent.com (Kate Ward) +# +# This is the proverbial 'Hello, world!' script to demonstrate the most basic +# functionality of shFlags. +# +# This script demonstrates accepts a single command-line flag of '-n' (or +# '--name'). If a name is given, it is output, otherwise the default of 'world' +# is output. + +# source shflags +. ../src/shflags + +# define a 'name' command-line string flag +DEFINE_string 'name' 'world' 'name to say hello to' 'n' + +# parse the command-line +FLAGS "$@" || exit 1 +eval set -- "${FLAGS_ARGV}" + +echo "Hello, ${FLAGS_name}!" diff --git a/examples/write_date.sh b/examples/write_date.sh new file mode 100755 index 0000000..85695cf --- /dev/null +++ b/examples/write_date.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# +# Copyright 2008 Kate Ward. All Rights Reserved. +# Released under the LGPL (GNU Lesser General Public License) +# +# Author: kate.ward@forestent.com (Kate Ward) +# +# This script takes a filename as input and writes the current date to the +# file. If the file already exists, it will not be overwritten unless the '-f' +# (or '--force') flag is given. +# +# This script demonstrates several types of shFlags functionality. +# - declaration of the FLAGS_HELP variable to customize the help output +# - direct calling of the flags_help() function for script controlled usage +# output +# - handling of non-flag type command-line arguments that follow the flags +# +# Try the following: +# $ ./write_date.sh now.out +# $ cat now.out +# +# $ ./write_date.sh now.out +# $ cat now.out +# +# $ ./write_date.sh -f now.out +# $ cat now.out + +# source shflags +. ../src/shflags + +# configure shflags +DEFINE_boolean 'force' false 'force overwriting' 'f' +FLAGS_HELP="USAGE: $0 [flags] filename" + + +write_date() +{ + date >"$1" +} + +die() +{ + [ $# -gt 0 ] && echo "error: $@" >&2 + flags_help + exit 1 +} + + +# parse the command-line +FLAGS "$@" || exit 1 +eval set -- "${FLAGS_ARGV}" + +# check for filename +[ $# -gt 0 ] || die 'filename missing' +filename=$1 + +[ -f "${filename}" -a ${FLAGS_force} -eq ${FLAGS_FALSE} ] \ + && die 'filename exists; not overwriting' +write_date "${filename}" |