Skip to content

Commit

Permalink
Reverted the DEFINE_SCRIPT_DIR to compatible mode.
Browse files Browse the repository at this point in the history
It doesn't resolve symlinks -
that's what DEFINE_SCRIPT_DIR_GNU does.
  • Loading branch information
matejak committed Jun 29, 2019
1 parent 59cfef6 commit f0a2229
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 10 deletions.
5 changes: 5 additions & 0 deletions 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).
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions bin/argbash
Expand Up @@ -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])
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -460,7 +460,7 @@ then
# match against suspicious, then inverse match against correct stuff:
# #<optional whitespace>\(allowed\|another allowed\|...\)<optional whitespace><opening bracket <or> 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" != '-'
Expand Down
6 changes: 3 additions & 3 deletions doc/_static/wrapper-output-action.txt
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion doc/guide.rst
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion docker/Dockerfile.in
Expand Up @@ -19,7 +19,6 @@ ENV PROGRAM=argbash
# and coreutils for readlink
RUN apk add --no-cache \
autoconf \
coreutils \
bash

# Install argbash from sources
Expand Down
8 changes: 7 additions & 1 deletion resources/Makefile
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 $@

Expand Down
27 changes: 26 additions & 1 deletion src/stuff.m4
Expand Up @@ -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])],
)])

Expand Down

0 comments on commit f0a2229

Please sign in to comment.