diff --git a/ChangeLog b/ChangeLog index 6d4322f..eeee836 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,10 @@ 2.8.1 (TBA) ------------------ +Incompatible changes (minor): + +* The `DEFINE_SCRIPT_DIR` macro doesn't resolve symlinks as it started to do so in `2.8.0`, use `DEFINE_SCRIPT_DIR_GNU` if you need the functionality. + New features: * The help message now contains reference for one-of argument types (#76). @@ -9,6 +13,7 @@ Bugfixes: * The environment variables help message has been fixed (#79). * The manpage generation works when long description is supplied (#78). +* The `DEFINE_SCRIPT_DIR` macro doesn't use `readlink -e` any more (#74). 2.8.0 (2019-01-26) diff --git a/bin/argbash b/bin/argbash index 807a98b..4cb0590 100755 --- a/bin/argbash +++ b/bin/argbash @@ -5,7 +5,7 @@ # SC2059 Don't use variables in the printf format string. -# DEFINE_SCRIPT_DIR() +# DEFINE_SCRIPT_DIR()_DEFINE_SCRIPT_DIR([],[cd "$(dirname "${BASH_SOURCE[0]}")" && pwd]) # ARG_POSITIONAL_SINGLE([input],[The input template file (pass '-' for stdin)]) # ARG_OPTIONAL_SINGLE([output],[o],[Name of the output file (pass '-' for stdout)],[-]) # ARG_OPTIONAL_SINGLE([type],[t],[Output type to generate],[bash-script]) @@ -232,7 +232,7 @@ handle_passed_args_count assign_positional_args 1 "${_positionals[@]}" # OTHER STUFF GENERATED BY Argbash -script_dir="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)" || die "Couldn't determine the script's running directory, which probably matters, bailing out" 2 +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || die "Couldn't determine the script's running directory, which probably matters, bailing out" 2 # Validation of values @@ -460,7 +460,7 @@ then # match against suspicious, then inverse match against correct stuff: # #\(allowed\|another allowed\|...\) end of line> # Then, extract all matches (assumed to be alnum chars + '_') from grep and put them in the error msg. - grep_output="$(printf "%s" "$output" | grep '^#\s*\(ARG_\|ARGBASH\)' | grep -v '^#\s*\(ARGBASH_SET_INDENT\|ARG_OPTIONAL_SINGLE\|ARG_VERSION\|ARG_VERSION_AUTO\|ARG_HELP\|ARG_OPTIONAL_INCREMENTAL\|ARG_OPTIONAL_REPEATED\|ARG_VERBOSE\|ARG_OPTIONAL_BOOLEAN\|ARG_OPTIONAL_ACTION\|ARG_POSITIONAL_SINGLE\|ARG_POSITIONAL_INF\|ARG_POSITIONAL_MULTI\|ARG_POSITIONAL_DOUBLEDASH\|ARG_OPTION_STACKING\|ARG_RESTRICT_VALUES\|ARG_DEFAULTS_POS\|ARG_LEFTOVERS\|ARGBASH_WRAP\|INCLUDE_PARSING_CODE\|DEFINE_SCRIPT_DIR\|ARGBASH_SET_DELIM\|ARGBASH_GO\|ARGBASH_PREPARE\|ARG_TYPE_GROUP\|ARG_TYPE_GROUP_SET\|ARG_USE_ENV\|ARG_USE_PROG\)\s*\((\|$\)' | sed -e 's/#\s*\([[:alnum:]_]*\).*/\1 /' | tr -d '\n\r')" + grep_output="$(printf "%s" "$output" | grep '^#\s*\(ARG_\|ARGBASH\)' | grep -v '^#\s*\(ARGBASH_SET_INDENT\|ARG_OPTIONAL_SINGLE\|ARG_VERSION\|ARG_VERSION_AUTO\|ARG_HELP\|ARG_OPTIONAL_INCREMENTAL\|ARG_OPTIONAL_REPEATED\|ARG_VERBOSE\|ARG_OPTIONAL_BOOLEAN\|ARG_OPTIONAL_ACTION\|ARG_POSITIONAL_SINGLE\|ARG_POSITIONAL_INF\|ARG_POSITIONAL_MULTI\|ARG_POSITIONAL_DOUBLEDASH\|ARG_OPTION_STACKING\|ARG_RESTRICT_VALUES\|ARG_DEFAULTS_POS\|ARG_LEFTOVERS\|ARGBASH_WRAP\|INCLUDE_PARSING_CODE\|DEFINE_SCRIPT_DIR\|DEFINE_SCRIPT_DIR_GNU\|_DEFINE_SCRIPT_DIR\|ARGBASH_SET_DELIM\|ARGBASH_GO\|ARGBASH_PREPARE\|ARG_TYPE_GROUP\|ARG_TYPE_GROUP_SET\|ARG_USE_ENV\|ARG_USE_PROG\)\s*\((\|$\)' | sed -e 's/#\s*\([[:alnum:]_]*\).*/\1 /' | tr -d '\n\r')" test -n "$grep_output" && die "Your script contains possible misspelled Argbash macros: $grep_output" 1 fi if test "$outfname" != '-' diff --git a/doc/_static/wrapper-output-action.txt b/doc/_static/wrapper-output-action.txt index f8540c2..ec99f01 100644 --- a/doc/_static/wrapper-output-action.txt +++ b/doc/_static/wrapper-output-action.txt @@ -9,7 +9,7 @@ Contents of '../src' matching '*.m4': collectors.m4: 20 kiB constants.m4: 0 kiB default_settings.m4: 0 kiB - docopt.m4: 2 kiB + docopt.m4: 3 kiB env_vars.m4: 1 kiB function_generators.m4: 7 kiB list.m4: 5 kiB @@ -23,8 +23,8 @@ Contents of '../src' matching '*.m4': output-strip-none.m4: 0 kiB output-strip-user-content.m4: 0 kiB progs.m4: 2 kiB - stuff.m4: 44 kiB - utilities.m4: 10 kiB + stuff.m4: 46 kiB + utilities.m4: 11 kiB value_validators.m4: 5 kiB Contents of '../resources/examples' matching '*.m4': minimal.m4: 0 kiB diff --git a/doc/guide.rst b/doc/guide.rst index 0f41031..3a7a3fa 100644 --- a/doc/guide.rst +++ b/doc/guide.rst @@ -429,10 +429,20 @@ Plus, there are convenience macros: * Add a line where the directory where the script is running is stored in an environmental variable: :: - DEFINE_SCRIPT_DIR([variable name (optional, default is script_dir)]) + DEFINE_SCRIPT_DIR([variable name (optional, default is 'script_dir')]) You can use this variable to e.g. source ``bash`` snippets that are in a known location relative to the script's parent directory. + :: + + DEFINE_SCRIPT_DIR_GNU([variable name (optional, default is 'script_dir')]) + + Does the same as ``DEFINE_SCRIPT_DIR``, but it uses the ``readlink -e`` to determine the real script directory by resolving symlinks. + + .. warning:: + This command is available only on GNU systems, so be very careful with its usage --- it won't work for OSX users, and for users on non-GNU based Linux distributions (s.a. Alpine Linux). + Don't use it unless you need the functionality AND you are sure that the script will be used only on systems with GNU coreutils. + .. _parsing_code: * Include a file (let's say a ``parse.sh`` file) that is in the same directory during runtime. diff --git a/docker/Dockerfile.in b/docker/Dockerfile.in index f8cf145..0145cdd 100644 --- a/docker/Dockerfile.in +++ b/docker/Dockerfile.in @@ -19,7 +19,6 @@ ENV PROGRAM=argbash # and coreutils for readlink RUN apk add --no-cache \ autoconf \ - coreutils \ bash # Install argbash from sources diff --git a/resources/Makefile b/resources/Makefile index f408cab..0df6713 100644 --- a/resources/Makefile +++ b/resources/Makefile @@ -3,7 +3,9 @@ NUL = # SC2015: Note that A && B || C is not if-then-else. C may run when A is true. SHELLCHECK_EXCLUDE_CHECKS_ARGUMENT = -e 2015 -SHELLCHECK=$(shell shellcheck -V > /dev/null && echo "shellcheck -x $(SHELLCHECK_EXCLUDE_CHECKS_ARGUMENT)") +SHELLCHECK = $(shell shellcheck -V > /dev/null && echo "shellcheck -x $(SHELLCHECK_EXCLUDE_CHECKS_ARGUMENT)") + +DEFINE_SCRIPT_DIR_FORM ?= DEFINE_SCRIPT_DIR$(shell readlink -e /dev/null > /dev/null && echo "_GNU") M4_SRC = \ ../src/output-strip-none.m4 \ @@ -64,6 +66,10 @@ MANPAGE = argbash.1.gz $(GENPARSE): ../src/argbash.m4 $(M4_SRC) ARGBASH_INTENDED_DESTINATION="$@" bash $(GENPARSE) $< -o argbash.temp && mv argbash.temp $@ +# TODO: Figure out how to use the GNU version of DEFINE_SCRIPT_DIR based on autodetection. +../src/argbash.m4.script-dir: ../src/argbash.m4 + sed "s/\\bDEFINE_SCRIPT_DIR\\b/$(DEFINE_SCRIPT_DIR_FORM)/" $< > $@ + $(A_INIT): ../src/argbash-init.m4 $(GENPARSE) $(GENPARSE) $< -o $@ diff --git a/src/stuff.m4 b/src/stuff.m4 index bc727a2..e7f136d 100644 --- a/src/stuff.m4 +++ b/src/stuff.m4 @@ -88,11 +88,36 @@ dnl dnl $1: Name of the holding variable dnl Taken from: http://stackoverflow.com/a/246128/592892 argbash_api([DEFINE_SCRIPT_DIR], [m4_do( + [[$0($@)]], + [dnl Taken from: http://stackoverflow.com/a/246128/592892 +], + [_DEFINE_SCRIPT_DIR([$1], [cd "$(dirname "${BASH_SOURCE[0]}")" && pwd])], +)]) + + +dnl +dnl Does the same as DEFINE_SCRIPT_DIR, but uses 'readlink -e' to follow symlinks. +dnl Works only on GNU systems. +dnl +dnl $1: Name of the holding variable +dnl Taken from: http://stackoverflow.com/a/246128/592892 +argbash_api([DEFINE_SCRIPT_DIR_GNU], [m4_do( + [[$0($@)]], + [dnl Taken from: http://stackoverflow.com/a/246128/592892 +], + [_DEFINE_SCRIPT_DIR([$1], [cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd])], +)]) + + +dnl +dnl $1: Name of the holding variable +dnl $2: Command to find the script dir +argbash_api([_DEFINE_SCRIPT_DIR], [m4_do( [[$0($@)]], [m4_define([SCRIPT_DIR_DEFINED])], [m4_pushdef([_sciptdir], m4_ifnblank([$1], [[$1]], _DEFAULT_SCRIPTDIR))], [m4_list_append([_OTHER], - m4_quote(_sciptdir[="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)" || die "Couldn't determine the script's running directory, which probably matters, bailing out" 2]))], + m4_quote(_sciptdir[="$($2)" || die "Couldn't determine the script's running directory, which probably matters, bailing out" 2]))], [m4_popdef([_sciptdir])], )])