Using which instead of multiple isfile statement

Signed-off-by: Luca Clementi <luca.clementi@gmail.com>
Added a unit test for the query option

Signed-off-by: Luca Clementi <luca.clementi@gmail.com>