Skip to content

Commit 54a2fc3

Browse files
authored
Merge pull request #106 from sgallagher/inplace
Add --in-place option
2 parents ae47252 + c994630 commit 54a2fc3

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

bin/argbash

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# DEFINE_SCRIPT_DIR()_DEFINE_SCRIPT_DIR([],[cd "$(dirname "${BASH_SOURCE[0]}")" && pwd])
99
# ARG_POSITIONAL_SINGLE([input],[The input template file (pass '-' for stdin)])
1010
# ARG_OPTIONAL_SINGLE([output],[o],[Name of the output file (pass '-' for stdout)],[-])
11+
# ARG_OPTIONAL_BOOLEAN([in-place],[i],[Update a bash script in-place],[off])
1112
# ARG_OPTIONAL_SINGLE([type],[t],[Output type to generate],[bash-script])
1213
# ARG_OPTIONAL_BOOLEAN([library],[],[Whether the input file if the pure parsing library])
1314
# ARG_OPTIONAL_SINGLE([strip],[],[Determines what to have in the output],[none])
@@ -62,7 +63,7 @@ type()
6263

6364
begins_with_short_option()
6465
{
65-
local first_option all_short_options='otcIhv'
66+
local first_option all_short_options='oitcIhv'
6667
first_option="${1:0:1}"
6768
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
6869
}
@@ -72,6 +73,7 @@ _positionals=()
7273
_arg_input=
7374
# THE DEFAULTS INITIALIZATION - OPTIONALS
7475
_arg_output="-"
76+
_arg_in_place="off"
7577
_arg_type="bash-script"
7678
_arg_library="off"
7779
_arg_strip="none"
@@ -84,9 +86,10 @@ _arg_debug=
8486
print_help()
8587
{
8688
printf '%s\n' "Argbash is an argument parser generator for Bash."
87-
printf 'Usage: %s [-o|--output <arg>] [-t|--type <type>] [--(no-)library] [--strip <content>] [--(no-)check-typos] [-c|--(no-)commented] [-I|--search <arg>] [--debug <arg>] [-h|--help] [-v|--version] <input>\n' "$0"
89+
printf 'Usage: %s [-o|--output <arg>] [-i|--(no-)in-place] [-t|--type <type>] [--(no-)library] [--strip <content>] [--(no-)check-typos] [-c|--(no-)commented] [-I|--search <arg>] [--debug <arg>] [-h|--help] [-v|--version] <input>\n' "$0"
8890
printf '\t%s\n' "<input>: The input template file (pass '-' for stdin)"
8991
printf '\t%s\n' "-o, --output: Name of the output file (pass '-' for stdout) (default: '-')"
92+
printf '\t%s\n' "-i, --in-place, --no-in-place: Update a bash script in-place (off by default)"
9093
printf '\t%s\n' "-t, --type: Output type to generate. Can be one of: 'bash-script', 'posix-script', 'manpage', 'manpage-defs', 'completion' and 'docopt' (default: 'bash-script')"
9194
printf '\t%s\n' "--library, --no-library: Whether the input file if the pure parsing library (off by default)"
9295
printf '\t%s\n' "--strip: Determines what to have in the output. Can be one of: 'none', 'user-content' and 'all' (default: 'none')"
@@ -119,6 +122,18 @@ parse_commandline()
119122
-o*)
120123
_arg_output="${_key##-o}"
121124
;;
125+
-i|--no-in-place|--in-place)
126+
_arg_in_place="on"
127+
test "${1:0:5}" = "--no-" && _arg_in_place="off"
128+
;;
129+
-i*)
130+
_arg_in_place="on"
131+
_next="${_key##-i}"
132+
if test -n "$_next" -a "$_next" != "$_key"
133+
then
134+
{ begins_with_short_option "$_next" && shift && set -- "-i" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
135+
fi
136+
;;
122137
-t|--type)
123138
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
124139
_arg_type="$(type "$2" "type")" || exit 1
@@ -430,6 +445,11 @@ trap cleanup EXIT
430445
# If we are reading from stdout, then create a temp file
431446
if test "$infile" = '-'
432447
then
448+
if test "$_arg_in_place" = 'on'
449+
then
450+
echo "Cannot use stdin input with --in-place option!" >&2
451+
exit 1;
452+
fi
433453
infile=temp_in_$$
434454
_files_to_clean+=("$infile")
435455
cat > "$infile"
@@ -449,6 +469,9 @@ then
449469
fi
450470

451471
test -f "$infile" || _PRINT_HELP=yes die "argument '$infile' is supposed to be a file!" 1
472+
if [ $_arg_in_place = on ]; then
473+
_arg_output=$infile
474+
fi
452475
test -n "$_arg_output" || { echo "The output can't be blank - it is not a legal filename!" >&2; exit 1; }
453476
outfname="$_arg_output"
454477
autom4te --version > "$discard" 2>&1 || { echo "You need the 'autom4te' utility (it comes with 'autoconf'), if you have bash, that one is an easy one to get." 2>&1; exit 1; }
@@ -482,4 +505,5 @@ else
482505
printf "%s\\n" "$output"
483506
fi
484507

485-
# # ] <-- needed because of Argbash
508+
# # <-- needed because of Argbash
509+
# ] <-- needed because of Argbash

0 commit comments

Comments
 (0)