Skip to content

Commit

Permalink
Merge pull request #28 from WGH-/output
Browse files Browse the repository at this point in the history
Add --output/-o flag
  • Loading branch information
sergiodj authored Dec 8, 2024
2 parents e01d578 + 1fbfed7 commit 6bb727c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ SPDX-License-Identifier: curl

# Synopsis

wcurl [--curl-options <CURL_OPTIONS>]... [--dry-run] [--no-decode-filename] [--] <URL>...
wcurl [--curl-options=<CURL_OPTIONS>]... [--dry-run] [--no-decode-filename] [--] <URL>...
wcurl <URL>...
wcurl [--curl-options <CURL_OPTIONS>]... [--no-decode-filename] [-o|-O|--output <PATH>] [--dry-run] [--] <URL>...
wcurl [--curl-options=<CURL_OPTIONS>]... [--no-decode-filename] [--output=<PATH>] [--dry-run] [--] <URL>...
wcurl -V|--version
wcurl -h|--help

Expand Down Expand Up @@ -53,8 +54,15 @@ should be using curl directly if your use case is not covered.

Specify extra options to be passed when invoking curl. May be specified more than once.

* `-o, -O, --output=<PATH>`

Use the provided output path instead of getting it from the URL. If multiple
URLs are provided, all files will have the same name with a number appended to
the end (curl >= 7.83.0).

* `--no-decode-filename`
Don't percent-decode the output filename, even if the percent-encoding in the URL was done by wcurl, e.g.: The URL contained whitespaces.
Don't percent-decode the output filename, even if the percent-encoding in the
URL was done by wcurl, e.g.: The URL contained whitespaces.

* `--dry-run`

Expand Down
8 changes: 8 additions & 0 deletions tests/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ testUrlStartingWithDash()
assertEquals "${ret}" "Unknown option: '-example.com'."
}

testOutputFileName()
{
url='example.com'
ret=$(${WCURL_CMD} -o "test filename" ${url} 2>&1)
assertContains "Verify whether 'wcurl' correctly sets a custom output filename" "${ret}" '--output'
assertContains "Verify whether 'wcurl' correctly sets a custom output filename" "${ret}" 'test filename'
}

testUrlDefaultName()
{
url='example%20with%20spaces.com'
Expand Down
59 changes: 43 additions & 16 deletions wcurl
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,34 @@ usage()
cat << _EOF_
${PROGRAM_NAME} -- a simple wrapper around curl to easily download files.
Usage: ${PROGRAM_NAME} [--curl-options <CURL_OPTIONS>] [--dry-run] [--no-decode-filename] [--] <URL>...
${PROGRAM_NAME} [--curl-options=<CURL_OPTIONS>] [--dry-run] [--no-decode-filename] [--] <URL>...
Usage: ${PROGRAM_NAME} <URL>...
${PROGRAM_NAME} [--curl-options <CURL_OPTIONS>]... [--no-decode-filename] [-o|-O|--output <PATH>] [--dry-run] [--] <URL>...
${PROGRAM_NAME} [--curl-options=<CURL_OPTIONS>]... [--no-decode-filename] [--output=<PATH>] [--dry-run] [--] <URL>...
${PROGRAM_NAME} -h|--help
${PROGRAM_NAME} -V|--version
Options:
--curl-options <CURL_OPTIONS>: Specify extra options to be
passed when invoking curl. May be
--curl-options <CURL_OPTIONS>: Specify extra options to be passed when invoking curl. May be
specified more than once.
--no-decode-filename: Don't percent-decode the output filename,
even if the percent-encoding in the URL was
done by wcurl, e.g.: The URL contained
whitespaces.
-o, -O, --output <PATH>: Use the provided output path instead of getting it from the URL. If
multiple URLs are provided, all files will have the same name with a
number appended to the end (curl >= 7.83.0).
--dry-run: Don't actually execute curl, just print what would be
invoked.
--no-decode-filename: Don't percent-decode the output filename, even if the percent-encoding in
the URL was done by wcurl, e.g.: The URL contained whitespaces.
-V,--version: Print version information.
--dry-run: Don't actually execute curl, just print what would be invoked.
-h,--help: Print this usage message.
-V, --version: Print version information.
-h, --help: Print this usage message.
<URL>: The URL to be downloaded. May be specified more than once.
<CURL_OPTIONS>: Any option supported by curl can be set here. This is not used by wcurl; it's
instead forwarded to the curl invocation.
<URL>: The URL to be downloaded. May be specified more than once.
_EOF_
Expand All @@ -92,6 +98,11 @@ CURL_OPTIONS=""
# The URLs to be downloaded.
URLS=""

# Will be set to the percent-decoded filename parsed from the URL, unless
# --output or --no-decode-filename are used.
OUTPUT_PATH=""
HAS_USER_SET_OUTPUT="false"

# The parameters that will be passed per-URL to curl.
readonly PER_URL_PARAMETERS="\
--fail \
Expand All @@ -112,7 +123,7 @@ sanitize()
error "You must provide at least one URL to download."
fi

readonly CURL_OPTIONS URLS DRY_RUN
readonly CURL_OPTIONS URLS DRY_RUN HAS_USER_SET_OUTPUT
}

# Indicate via exit code whether the string given in the first parameter
Expand Down Expand Up @@ -213,10 +224,14 @@ exec_curl()

NEXT_PARAMETER=""
for url in ${URLS}; do
filename="$(get_url_filename "${url}")"
[ -z "${filename}" ] && filename=index.html
# If the user did not provide an output path, define one.
if [ "${HAS_USER_SET_OUTPUT}" = "false" ]; then
OUTPUT_PATH="$(get_url_filename "${url}")"
# If we could not get a path from the URL, use the default: index.html.
[ -z "${OUTPUT_PATH}" ] && OUTPUT_PATH=index.html
fi
# shellcheck disable=SC2086
set -- "$@" ${NEXT_PARAMETER} ${PER_URL_PARAMETERS} ${CURL_HAS_NO_CLOBBER} ${CURL_OPTIONS} --output "${filename}" "${url}"
set -- "$@" ${NEXT_PARAMETER} ${PER_URL_PARAMETERS} ${CURL_HAS_NO_CLOBBER} ${CURL_OPTIONS} --output "${OUTPUT_PATH}" "${url}"
NEXT_PARAMETER="--next"
done

Expand Down Expand Up @@ -247,6 +262,18 @@ while [ -n "${1-}" ]; do
DRY_RUN="true"
;;

--output=*)
opt=$(printf "%s\n" "${1}" | sed 's/^--output=//')
HAS_USER_SET_OUTPUT="true"
OUTPUT_PATH="${opt}"
;;

-o|-O|--output)
shift
HAS_USER_SET_OUTPUT="true"
OUTPUT_PATH="${1}"
;;

--no-decode-filename)
DECODE_FILENAME="false"
;;
Expand Down
10 changes: 8 additions & 2 deletions wcurl.1
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
- a simple wrapper around curl to easily download files.
.SH SYNOPSIS
.nf
\fBwcurl [\-\-curl\-options \fI<CURL_OPTIONS>\fP]... [\-\-dry\-run] [\-\-no\-decode\-filename] [\-\-] \fI<URL>\fP...\fR
\fBwcurl [\-\-curl\-options=\fI<CURL_OPTIONS>\fP]... [\-\-dry\-run] [\-\-no\-decode\-filename] [\-\-] \fI<URL>\fP...\fR
\fBwcurl \fI<URL>\fP...\fR
\fBwcurl [\-\-curl\-options \fI<CURL_OPTIONS>\fP]... [\-\-dry\-run] [\-\-no\-decode\-filename] [\-o|\-O|\-\-output <PATH>] [\-\-] \fI<URL>\fP...\fR
\fBwcurl [\-\-curl\-options=\fI<CURL_OPTIONS>\fP]... [\-\-dry\-run] [\-\-no\-decode\-filename] [\-\-output=<PATH>] [\-\-] \fI<URL>\fP...\fR
\fBwcurl \-V|\-\-version\fR
\fBwcurl \-h|\-\-help\fR
.fi
Expand Down Expand Up @@ -72,6 +73,11 @@ By default, \fBwcurl\fR will:
\fB\-\-curl\-options, \-\-curl\-options=\fI<CURL_OPTIONS>\fR...\fR
Specify extra options to be passed when invoking curl. May be specified more than once.
.TP
\fB\-o, \-O, \-\-output=\fI<PATH>\fR...\fR
Use the provided output path instead of getting it from the URL. If multiple
URLs are provided, all files will have the same name with a number appended to
the end (curl >= 7.83.0).
.TP
\fB\-\-dry\-run\fR
Don't actually execute curl, just print what would be invoked.
.TP
Expand Down

0 comments on commit 6bb727c

Please sign in to comment.