diff --git a/snippets/c.snippets b/snippets/c.snippets index 72ce3d0..c476de4 100644 --- a/snippets/c.snippets +++ b/snippets/c.snippets @@ -150,6 +150,63 @@ snippet pr # fprintf (again, this isn't as nice as TextMate's version, but it works) snippet fpr 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 # This is kind of convenient diff --git a/snippets/python.snippets b/snippets/python.snippets index 7f58d98..dd2e812 100644 --- a/snippets/python.snippets +++ b/snippets/python.snippets @@ -139,3 +139,20 @@ snippet testcase ${3:# TODO: write code...} snippet fut 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 diff --git a/snippets/sh.snippets b/snippets/sh.snippets index 508056c..9a48eb5 100644 --- a/snippets/sh.snippets +++ b/snippets/sh.snippets @@ -41,3 +41,43 @@ snippet go # Set SCRIPT_DIR variable to directory script is located. snippet sdir 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)) +