Skip to content

Instantly share code, notes, and snippets.

View jefferys's full-sized avatar

Stuart R. Jefferys jefferys

  • UNC Chapel Hill
View GitHub Profile
@jefferys
jefferys / Perl_OO_class_model.pm
Created May 15, 2013 21:36
A template for a new perl class using hash based objects and inline pod.
package Some::Class;
# See end of file for copyright info.
use strict;
use warnings;
# Comment describing how I'm using Some::Dependency.
# use Some::Dependency;
@jefferys
jefferys / roxygen2OneFunction.gist.R
Last active September 3, 2022 22:12
R function documentation with roxygen2 - single function
#=====================================
# Demo function roxygen2 documentation
#=====================================
# This shows how things work, including weird corner cases, not how they
# should be done. Most of the information comes from
# http://r-pkgs.had.co.nz/man.html
#' Brief description/title of the page describing this function.
@jefferys
jefferys / roxygen2PackageDocumentation.gist.R
Last active September 3, 2022 22:13
R package documentation with roxygen2
#====================================
# Demo package roxygen2 documentation
#====================================
# This shows how things do work, including wierd corner cases, not
# how they should be done. Most of the information comes from
# http://r-pkgs.had.co.nz/man.html
#' Brief page title describing this package.
#'
@jefferys
jefferys / roxygen2MultiFunctionDocumentation.gist.R
Created June 21, 2015 17:29
R function documentation with roxygen2 - multiple functions
#===================================================
# Demo multi-function roxygen2 - three ways to do it
#===================================================
# This shows how things do work, including wierd corner cases, not
# how they should be done. Most of the information comes from
# http://r-pkgs.had.co.nz/man.html
#====================================================
# Demo multi-function roxygen2 page using @describeIn
@jefferys
jefferys / roxygen2Markup.gist.R
Created June 21, 2015 17:34
R text markup with roxygen2 - style, lists, links, tables and equations
#==========================
# Demo roxygen2 text markup
#==========================
# This shows how things do work, including wierd corner cases, not
# how they should be done. Most of the information comes from
# http://r-pkgs.had.co.nz/man.html
#' Demo page for roxygen2 text markup
#'
@jefferys
jefferys / bash_get_options_template.sh
Created November 14, 2017 21:43
A template for a bash function parsing CLI options and arguments into global variables, including short, long, repeated, counted, and --.
###############################################################################
# DESC: Parses arguments with the bash built-in getopts, but allows for
# long options too. This only works when each long option has a short option,
# although short options need not have long ones. Designed to create global
# variables named opt_<option> containing the parsed command line options, and
# an array variable called opt_args with any additional non-option arguments.
# This is all hard coded, but simple to modify for local use. See the ###
# sections for what needs to be changed to create your own option variable set.
# Supports bundled options and the use of "--" to signal end of options.
# Does not support negated options. You can always just declare "myFlag" and
# Append potentially empty string, but with separator if not empty
# "one" -> "one" if "$extra" is empty, "one" -> "one, two" if not
result="one${extra:+", ${extra}"}"
# Examples
extra=;[ "one${extra:+", ${extra}"}" == 'one' ] || echo "FALSE"
extra=''; [ "one${extra:+", ${extra}"}" == 'one' ] || echo "FALSE"
extra='two'; [ "one${extra:+", ${extra}"}" == 'one, two' ] || echo "FALSE"
# Useful for path changes adding possibly empty dir to path
@jefferys
jefferys / parseCli.sh
Created December 17, 2020 19:39
Bash option parsing example without using getopt and using globals instead of a hash
# Avoids string-keyed arrays as not available in old bash on Macs. Supports:
# * arguments and values with spaces if quoted.
# * arguments embedded in options (e.g. "commands").
# * arguments that look like option (start with a -) after a lone "--"
# * options with values, without values, and with optional values.
# * repeated options with values and counted flags.
# * both "key=value"" and "key value" style options
function parseCli() {
# globals
OPT_DOWNLOAD=0
@jefferys
jefferys / logSay.sh
Created December 17, 2020 19:46
Bash program messages - terminal and log file printing
# USAGE:
# say $LEVEL_INFO "This is an" "informational message."
# #> "This is an informational message."
#
# sayError "This is an" "error message."
# #> "ERROR: This is an error message."
#
# LEVELS:
# In order from highest priority message to lowest:
# $LEVEL_ALL $LEVEL_ERROR $LEVEL_WARN $LEVEL_INFO $LEVEL_VERBOSE
@jefferys
jefferys / mydir.bash
Created December 28, 2020 20:46
Get the directory of a path. Example use is to get a bash script's location for loading other bash scripts as modules.
#!/usr/bin/env bash
# Prints error messages to stderr
# @PARAM Concatenates parameters with IFS (default space) to form an error
# message.
# @OUT Prints error message preceeded by "ERROR: " to standard error,
# translating escapes.
# @REQUIRES: echo -e
function sayErr() {
echo -e "ERROR: $@" >&2