curl / trurl

trurl is a command line tool for URL parsing and manipulation.
https://curl.se/trurl/
Other
3.15k stars 102 forks source link

trurl: run --trim query before --append query #225

Closed emanuele6 closed 1 year ago

emanuele6 commented 1 year ago

Before this patch there was not a way to add a query parameter to a URL, removing the previous value if the parameter was already present in the base URL.

I tried to use:

x () {
  trurl --verify --trim query=foo --append "query=foo=$2" -- "$1"
}

But that didn't work since --trim query runs after --append query, so the new value value added with --append also gets trimmed:

$ x 'https://example.org/?foo=hello&baz=boo' 'howdy'
https://example.org/?baz=boo

This patch makes trurl run --trim before --append so that using --trim query=foo --append query=foo=bar works to set foo to bar overwriting previous values of foo.

$ x 'https://example.org/?foo=hello&baz=boo' 'howdy'
https://example.org/?baz=boo&foo=howdy

I added a test for this, and I also added a test that tests that --sort-query and --append query work together, since I noticed it was missing.