in reply to Re: Parsing a command-line string into an array
in thread Parsing a command-line string into an array

Hey! that works quite good despite its humble name - I never knew it existed.

use Text::ParseWords; my @x = shellwords("-x '1 2 3' -y 4 5"); print join("\n", @x); -x 1 2 3 -y 4 5

Replies are listed 'Best First'.
Re^3: Parsing a command-line string into an array
by ikegami (Patriarch) on Dec 23, 2023 at 06:14 UTC
    The down side is that
    -x '1 2 3' -y 4 5

    would have to be passed as

    --extra-ffmpeg-param='-x '\''1 2 3'\'' -y 4 5'

    You'd need nested quoting, which is very tricky.

    This can probably be mitigated by mixing quotes.

    --extra-ffmpeg-param='-x "1 2 3" -y 4 5'

    And this isn't just tricky to build manually; this is also tricky to build programmatically from the shell. Like, what if you wanted to wanted to do the equivalent of ffmpeg -x "$x"? It would look something like

    # to_shell_lit() - Creates a shell literal # Usage: printf '%s\n' "$( to_shell_lit "..." "..." "..." )" to_shell_lit() { local prefix='' local p for p in "$@" ; do printf "$prefix"\' printf %s "$p" | sed "s/'/'\\\\''/g" printf \' prefix=' ' done } --extra-ffmpeg-param="$( to_shell_lit -x "$x" )"

    (Source)

    This is part of the reason I suggested using multiple args.

    -X '-x 1 2 3' -X '-y 4'
    -X "-x $x" -X '-y 4'
    or
    -X 'x=1 2 3' -X 'y=4'
    -X "x=$x" -X 'y=4'