Merge pull request #157 from DjBushido/master

Add code for Getopts
This commit is contained in:
Honza Pokorny 2012-12-07 07:52:33 -08:00
commit 61b03b6647
3 changed files with 114 additions and 0 deletions

View File

@ -150,6 +150,63 @@ snippet pr
# fprintf (again, this isn't as nice as TextMate's version, but it works) # fprintf (again, this isn't as nice as TextMate's version, but it works)
snippet fpr snippet fpr
fprintf(${1:stderr}, "${2:%s}\n"${3});${4} fprintf(${1:stderr}, "${2:%s}\n"${3});${4}
# getopt
snippet getopt
int choice;
while (1)
{
static struct option long_options[] =
{
/* Use flags like so:
{"verbose", no_argument, &verbose_flag, 'V'}*/
/* Argument styles: no_argument, required_argument, optional_argument */
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
${1}
{0,0,0,0}
};
int option_index = 0;
/* Argument parameters:
no_argument: " "
required_argument: ":"
optional_argument: "::" */
choice = getopt_long( argc, argv, "vh",
long_options, &option_index);
if (choice == -1)
break;
switch( choice )
{
case 'v':
${2}
break;
case 'h':
${3}
break;
case '?':
/* getopt_long will have already printed an error */
break;
default:
/* Not sure how to get here... */
return EXIT_FAILURE;
}
}
/* Deal with non-option arguments here */
if ( optind < argc )
{
while ( optind < argc )
{
${4}
}
}
## ##
## Miscellaneous ## Miscellaneous
# This is kind of convenient # This is kind of convenient

View File

@ -139,3 +139,20 @@ snippet testcase
${3:# TODO: write code...} ${3:# TODO: write code...}
snippet fut snippet fut
from __future__ import ${1} from __future__ import ${1}
#getopt
snippet getopt
try:
# Short option syntax: "hv:"
# Long option syntax: "help" or "verbose="
opts, args = getopt.getopt(sys.argv[1:], "${1:short_options}", [${2:long_options}])
except getopt.GetoptError, err:
# Print debug info
print str(err)
${3:error_action}
for option, argument in opts:
if option in ("-h", "--help"):
${4}
elif option in ("-v", "--verbose"):
verbose = argument

View File

@ -41,3 +41,43 @@ snippet go
# Set SCRIPT_DIR variable to directory script is located. # Set SCRIPT_DIR variable to directory script is located.
snippet sdir snippet sdir
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# getopt
snippet getopt
__ScriptVersion="${1:version}"
#=== FUNCTION ================================================================
# NAME: usage
# DESCRIPTION: Display usage information.
#===============================================================================
function usage ()
{
cat <<- EOT
Usage : $${0:0} [options] [--]
Options:
-h|help Display this message
-v|version Display script version
EOT
} # ---------- end of function usage ----------
#-----------------------------------------------------------------------
# Handle command line arguments
#-----------------------------------------------------------------------
while getopts ":hv" opt
do
case $opt in
h|help ) usage; exit 0 ;;
v|version ) echo "$${0:0} -- Version $__ScriptVersion"; exit 0 ;;
\? ) echo -e "\n Option does not exist : $OPTARG\n"
usage; exit 1 ;;
esac # --- end of case ---
done
shift $(($OPTIND-1))