From 6129e08347fb490aba5c916e4646d6c70d170542 Mon Sep 17 00:00:00 2001 From: Renato Silva Date: Wed, 9 Jul 2014 19:27:00 -0300 Subject: [PATCH] Add examples. --- example.rb | 41 +++++++++++++++++++++++++++++++++++++++++ example.sh | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 example.rb create mode 100644 example.sh diff --git a/example.rb b/example.rb new file mode 100644 index 0000000..dcc984d --- /dev/null +++ b/example.rb @@ -0,0 +1,41 @@ +#!/usr/bin/env ruby +# Encoding: UTF-8 + +## EasyOptions Example +## Copyright (C) Someone +## Licensed under XYZ +## +## This program is an example of EasyOptions. It just prints the options and +## arguments provided in command line. Usage: +## +## @script.name [option] ARGUMENTS... +## +## Options: +## -h, --help All client scripts have this, it can be omitted. +## -o, --some-option This is a boolean option. Long version is +## mandatory, and can be specified before or +## after short version. +## --some-boolean This is a boolean option without a short version. +## --some-value=VALUE This is a parameter option. When calling your script +## the equal sign is optional and blank space can be +## used instead. Short version is not available in this +## format. + +require_relative "easyoptions" + +# Boolean options +puts "Option specified: --some-option" if $options[:option] +puts "Option specified: --some-boolean" if $options[:some_boolean] + +# Parameter option +value = $options[:some_value] +if value + value = $options[:some_value] + type = value.is_a?(Fixnum)? "number" : "string" + puts "Option specified: --some-value is #{value} (a #{type})" +end + +# Arguments +$arguments.each do |argument| + puts "Argument specified: #{argument}" +end diff --git a/example.sh b/example.sh new file mode 100644 index 0000000..e688b07 --- /dev/null +++ b/example.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +## EasyOptions Example +## Copyright (C) Someone +## Licensed under XYZ +## +## This program is an example of EasyOptions. It just prints the options and +## arguments provided in command line. Usage: +## +## @script.name [option] ARGUMENTS... +## +## Options: +## -h, --help All client scripts have this, it can be omitted. +## -o, --some-option This is a boolean option. Long version is +## mandatory, and can be specified before or +## after short version. +## --some-boolean This is a boolean option without a short version. +## --some-value=VALUE This is a parameter option. When calling your script +## the equal sign is optional and blank space can be +## used instead. Short version is not available in this +## format. + +# Bash version +source easyoptions.sh "$@" || exit + +# Ruby version (faster) +# eval "$(from="$0" ./easyoptions.rb "$@" || echo exit 1)" + +# Boolean and parameter options +[[ -n "$some_option" ]] && echo "Option specified: --some-option" +[[ -n "$some_boolean" ]] && echo "Option specified: --some-boolean" +[[ -n "$some_value" ]] && echo "Option specified: --some-value is $some_value" + +# Arguments +for argument in "${arguments[@]}"; do + echo "Argument specified: $argument" +done