From f8bd0eff4a4d46ecfd2f9b6fc49e94cdfbf9c661 Mon Sep 17 00:00:00 2001 From: WGH Date: Fri, 15 Nov 2024 20:25:00 +0300 Subject: [PATCH 1/2] Add --output/-o flag --remote-name-all is good default choice, but overriding the default inferred file name is still useful. One could argue that one should use plain curl at this point, but wcurl still has lots of good defaults that are missing in plain curl binary and tedious to specify manually. --curl-options=-owhatever is an option, but that's also very verbose. Compare sudo curl -fLOp /usr/local/sbin/wcurl https://raw.githubusercontent.com/curl/wcurl/main/wcurl sudo wcurl --curl-options=-o/usr/local/sbin/wcurl https://raw.githubusercontent.com/curl/wcurl/main/wcurl sudo wcurl -o /usr/local/sbin/wcurl https://raw.githubusercontent.com/curl/wcurl/main/wcurl And the first one with plain curl still misses a lot of good wcurl defaults. --- wcurl | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/wcurl b/wcurl index 39e643e..fc9aa25 100755 --- a/wcurl +++ b/wcurl @@ -49,8 +49,8 @@ usage() cat << _EOF_ ${PROGRAM_NAME} -- a simple wrapper around curl to easily download files. -Usage: ${PROGRAM_NAME} [--curl-options ] [--dry-run] [--] ... - ${PROGRAM_NAME} [--curl-options=] [--dry-run] [--] ... +Usage: ${PROGRAM_NAME} [--curl-options ] [--output ] [--dry-run] [--] ... + ${PROGRAM_NAME} [--curl-options=] [--output=PATH] [--dry-run] [--] ... ${PROGRAM_NAME} -h|--help ${PROGRAM_NAME} -V|--version @@ -60,6 +60,9 @@ Options: passed when invoking curl. May be specified more than once. + -o,--output: Use explicit output path instead of curl's --remote-name-all + logic. + --dry-run: Don't actually execute curl, just print what would be invoked. @@ -88,13 +91,16 @@ CURL_OPTIONS="" # The URLs to be downloaded. URLS="" +# Defaults to --remote-name-all. Can be overriden to explicit file name using +# -o/--output. +OUTPUT="--remote-name-all" + # The parameters that will be passed per-URL to curl. readonly PER_URL_PARAMETERS="\ --fail \ --globoff \ --location \ --proto-default https \ - --remote-name-all \ --remote-time \ --retry 10 \ --retry-max-time 10 " @@ -163,7 +169,7 @@ exec_curl() { NEXT_PARAMETER="" for url in ${URLS}; do # shellcheck disable=SC2086 - set -- "$@" ${NEXT_PARAMETER} ${PER_URL_PARAMETERS} ${CURL_HAS_NO_CLOBBER} ${CURL_OPTIONS} "${url}" + set -- "$@" ${NEXT_PARAMETER} ${PER_URL_PARAMETERS} ${CURL_HAS_NO_CLOBBER} ${CURL_OPTIONS} "${OUTPUT}" "${url}" NEXT_PARAMETER="--next" done @@ -191,6 +197,16 @@ while [ -n "${1-}" ]; do DRY_RUN="true" ;; + --output=*) + opt=$(printf "%s\n" "${1}" | sed 's/^--output=//') + OUTPUT="-o${opt}" + ;; + + -o|--output) + shift + OUTPUT="-o${1}" + ;; + -h|--help) usage exit 0 From c0d31c7506fa7b13f4095544a077abe884e7b0a6 Mon Sep 17 00:00:00 2001 From: Samuel Henrique Date: Wed, 4 Dec 2024 23:08:43 +0000 Subject: [PATCH 2/2] docs, tests and bugfix for -o/-O/--output option --- README.md | 10 ++++++++-- tests/tests.sh | 8 ++++++++ wcurl | 39 ++++++++++++++++++++++----------------- wcurl.1 | 5 +++++ 4 files changed, 43 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 977bf82..64b97ba 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ SPDX-License-Identifier: curl # Synopsis - wcurl [--curl-options ]... [--dry-run] [--] ... - wcurl [--curl-options=]... [--dry-run] [--] ... + wcurl [--curl-options ]... [-o|-O|--output ] [--dry-run] [--] ... + wcurl [--curl-options=]... [--output=] [--dry-run] [--] ... wcurl -V|--version wcurl -h|--help @@ -49,6 +49,12 @@ 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 explicit output path instead of curl's --remote-name logic. 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. diff --git a/tests/tests.sh b/tests/tests.sh index 5354d2e..ff335fc 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -111,6 +111,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' +} + ## Ideas for tests: ## ## - URL with whitespace diff --git a/wcurl b/wcurl index fc9aa25..7708fcd 100755 --- a/wcurl +++ b/wcurl @@ -49,28 +49,30 @@ usage() cat << _EOF_ ${PROGRAM_NAME} -- a simple wrapper around curl to easily download files. -Usage: ${PROGRAM_NAME} [--curl-options ] [--output ] [--dry-run] [--] ... - ${PROGRAM_NAME} [--curl-options=] [--output=PATH] [--dry-run] [--] ... +Usage: ${PROGRAM_NAME} [--curl-options ]... [-o|-O|--output ] [--dry-run] [--] ... + ${PROGRAM_NAME} [--curl-options=]... [--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. - -o,--output: Use explicit output path instead of curl's --remote-name-all - logic. + -o, -O, --output : Use explicit output path instead of curl's --remote-name logic. 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. + --dry-run: Don't actually execute curl, just print what would be invoked. - -V,--version: Print version information. + -V, --version: Print version information. - -h,--help: Print this usage message. + -h, --help: Print this usage message. - : The URL to be downloaded. May be specified more than once. + : 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. _EOF_ } @@ -92,8 +94,9 @@ CURL_OPTIONS="" URLS="" # Defaults to --remote-name-all. Can be overriden to explicit file name using -# -o/--output. -OUTPUT="--remote-name-all" +# -o/-O/--output. +OUTPUT_PARAMETER="--remote-name-all" +OUTPUT_PATH="" # The parameters that will be passed per-URL to curl. readonly PER_URL_PARAMETERS="\ @@ -169,7 +172,7 @@ exec_curl() { NEXT_PARAMETER="" for url in ${URLS}; do # shellcheck disable=SC2086 - set -- "$@" ${NEXT_PARAMETER} ${PER_URL_PARAMETERS} ${CURL_HAS_NO_CLOBBER} ${CURL_OPTIONS} "${OUTPUT}" "${url}" + set -- "$@" ${NEXT_PARAMETER} ${PER_URL_PARAMETERS} ${CURL_HAS_NO_CLOBBER} ${CURL_OPTIONS} ${OUTPUT_PARAMETER} "${OUTPUT_PATH}" "${url}" NEXT_PARAMETER="--next" done @@ -199,12 +202,14 @@ while [ -n "${1-}" ]; do --output=*) opt=$(printf "%s\n" "${1}" | sed 's/^--output=//') - OUTPUT="-o${opt}" + OUTPUT_PARAMETER="--output" + OUTPUT_PATH="${opt}" ;; - -o|--output) + -o|-O|--output) shift - OUTPUT="-o${1}" + OUTPUT_PARAMETER="--output" + OUTPUT_PATH="${1}" ;; -h|--help) diff --git a/wcurl.1 b/wcurl.1 index 3d7f3dc..0256e2f 100644 --- a/wcurl.1 +++ b/wcurl.1 @@ -68,6 +68,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 explicit output path instead of curl's --remote-name logic. 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