diff --git a/README.md b/README.md index 16121c5..7c03fdc 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,9 @@ SPDX-License-Identifier: curl # Synopsis - wcurl [--curl-options ]... [--dry-run] [--no-decode-filename] [--] ... - wcurl [--curl-options=]... [--dry-run] [--no-decode-filename] [--] ... + wcurl ... + wcurl [--curl-options ]... [--no-decode-filename] [-o|-O|--output ] [--dry-run] [--] ... + wcurl [--curl-options=]... [--no-decode-filename] [--output=] [--dry-run] [--] ... wcurl -V|--version wcurl -h|--help @@ -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=` + + 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` diff --git a/tests/tests.sh b/tests/tests.sh index 0dfe583..e34db4d 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -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' diff --git a/wcurl b/wcurl index a95480a..a4f9f2e 100755 --- a/wcurl +++ b/wcurl @@ -48,28 +48,34 @@ usage() cat << _EOF_ ${PROGRAM_NAME} -- a simple wrapper around curl to easily download files. -Usage: ${PROGRAM_NAME} [--curl-options ] [--dry-run] [--no-decode-filename] [--] ... - ${PROGRAM_NAME} [--curl-options=] [--dry-run] [--no-decode-filename] [--] ... +Usage: ${PROGRAM_NAME} ... + ${PROGRAM_NAME} [--curl-options ]... [--no-decode-filename] [-o|-O|--output ] [--dry-run] [--] ... + ${PROGRAM_NAME} [--curl-options=]... [--no-decode-filename] [--output=] [--dry-run] [--] ... ${PROGRAM_NAME} -h|--help ${PROGRAM_NAME} -V|--version Options: - --curl-options : Specify extra options to be - passed when invoking curl. May be + --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 : 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. + + : The URL to be downloaded. May be specified more than once. + + : Any option supported by curl can be set here. This is not used by wcurl; it's + instead forwarded to the curl invocation. : The URL to be downloaded. May be specified more than once. _EOF_ @@ -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 \ @@ -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 @@ -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 @@ -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" ;; diff --git a/wcurl.1 b/wcurl.1 index cbaa771..9e29253 100644 --- a/wcurl.1 +++ b/wcurl.1 @@ -27,8 +27,9 @@ - a simple wrapper around curl to easily download files. .SH SYNOPSIS .nf -\fBwcurl [\-\-curl\-options \fI\fP]... [\-\-dry\-run] [\-\-no\-decode\-filename] [\-\-] \fI\fP...\fR -\fBwcurl [\-\-curl\-options=\fI\fP]... [\-\-dry\-run] [\-\-no\-decode\-filename] [\-\-] \fI\fP...\fR +\fBwcurl \fI\fP...\fR +\fBwcurl [\-\-curl\-options \fI\fP]... [\-\-dry\-run] [\-\-no\-decode\-filename] [\-o|\-O|\-\-output ] [\-\-] \fI\fP...\fR +\fBwcurl [\-\-curl\-options=\fI\fP]... [\-\-dry\-run] [\-\-no\-decode\-filename] [\-\-output=] [\-\-] \fI\fP...\fR \fBwcurl \-V|\-\-version\fR \fBwcurl \-h|\-\-help\fR .fi @@ -72,6 +73,11 @@ By default, \fBwcurl\fR will: \fB\-\-curl\-options, \-\-curl\-options=\fI\fR...\fR Specify extra options to be passed when invoking curl. May be specified more than once. .TP +\fB\-o, \-O, \-\-output=\fI\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