Cmdline and Argument Passing

Running FileBot from the console, Groovy / FileBot scripting, shell scripts, etc
Post Reply
User avatar
rednoah
The Source
Posts: 21746
Joined: 16 Nov 2011, 08:59

Cmdline and Argument Passing

Post by rednoah »

I haven't found a good tutorial that explains argument parsing well, especially how to correctly escape and quote arguments if necessary. If you use cmdline you'll learn these things by trial and error, but lots of new users seem to have massive issues with using cmdline tools and passing more complicated arguments.



⭑⭑ Use the @file syntax for reading command-line arguments from external text files so you don't have to worry about any of this. ;) ⭑⭑



Argument Parsing:
When calling cmdline tools you first specify the command followed by arguments separated by space. In order to call a command by name without specifying the full path to the executable it needs to be in the PATH (by adding the folder where the executable is %PATH% on Windows, or by symlinking to /usr/bin on Linux/Mac).

e.g. command: filebot, args: [-version]

Code: Select all

filebot -version

Quote Arguments:
If you want to pass an argument that itself contains spaces then you need to quote the value with "x"

command: C:/Program Files/FileBot/filebot.exe:, args: [-rename, D:/New Files]

Code: Select all

"C:/Program Files/FileBot/filebot.exe" -rename "D:/New Files"
If you mess up the quotes you usually end up passing multiple arguments which will be processed individually

e.g. calling rm -r /New Media will remove the folder /New as well as the folder ./Media (path relative to the current working directory)

BAD command: filebot:, args: [-rename, D:/New, C:/Program Files/FileBot/Files]

Code: Select all

filebot -rename D:/New Files

Avoid non-ASCII characters:

Never use non-ASCII characters like or and note that these characters are not the same as the " standard quotation mark. If a character is not on the keyboard, then it will not have any special meaning on the command-line.

command: echo, args: [Hello World]

Code: Select all

echo "Hello World"
Hello World
BAD command: echo, args: [“Hello, World”]

Code: Select all

echo “Hello Hello”
“Hello Hello”

Escape Arguments:
If you want to pass an argument that itself contains double-quotes or other special characters that are interpreted by the shell (e.g. * or $ or ! in bash) you may need to further escape these characters with \x.

Escaping arguments correctly is mostly a matter of trial and error. See the following examples to get a feeling for how it works:

How to quote:

Code: Select all

echo "How to \"quote\""
How to "quote"
How NOT to quote:

Code: Select all

echo "How to "quote""
How to quote
$variables will be resolved:

Code: Select all

echo "I like $TEST"
I like
Escape $ with \ to pass in $ literally:

Code: Select all

echo "I like \$TEST"
I like $TEST
Shell expansion (i.e. implicitly pass in multiple arguments):

Code: Select all

echo *.mp4
firefly.101.mp4
Avoid shell expansion using escaping:

Code: Select all

echo \*.mp4
*.mp4
Avoid shell expansion using quoting:

Code: Select all

echo "*.mp4"
*.mp4

Troubleshooting:
If arguments don't get passed as expected, just use trial and error to find the problem. Just break things down step by step and see what works as expected and what does not work as expected.

It helps to see what is actually passed on to the application. Here's a script for that:

Code: Select all

filebot -script fn:sysenv arg1 arg2 ...
e.g.

Code: Select all

filebot -script fn:sysenv Hello World!
...
args[0] = -script
args[1] = fn:sysenv
args[2] = Hello
args[3] = World!
:idea: Please read the FAQ and How to Request Help.
Post Reply