0

I'm writing a utility that accepts a secondary shell command as its final parameter. My utility then runs the supplied shell command on behalf of the user. The user will invoke it as follows:

util [options] command with args

I want QCommandLineParser to parse the initial options, then leave the tail-end stuff alone (or parse it into a pure value list). Instead, QCommandLineParser complains that the user has entered invalid arguments.

For example, if the user were to enter

util -r ls -l

then ls -l is a valid command that I want to accept but Qt tells the user that -l is an invalid argument for my application. What's the best way to deal with this?

  • Traditionally you would either quote the complete subcommand (including its arguments), or place the while thing after --, so that it's obvious to both a human reader and the parser that the things that look like flags (your -l) are not really flags. Doesn't that work for you? QCommandLineParser handles double dash (--) correctly. – Alan Stokes Mar 07 '15 at 09:24
  • I didn't know about the double-dash thing; so that's an option I guess. Quoting the subcommand as a single argument and then whitespace-splitting it up into an argument list again to pass to an `exec` call might work well enough for simple commands but would break any command that itself requires quoted arguments. – Username Obfuscation Mar 07 '15 at 10:48
  • You can quote quotes if need be. And it's the same with any command; it's not putting an unusual burden on the user. util -r -- ls -l is pretty normal too. – Alan Stokes Mar 07 '15 at 13:13
  • While you can quote quotes, you can't duplicate everything the shell might have done with those arguments had you _not_ quoted the whole command (unless you basically duplicate everything the shell does inside your application, or rely on the rather limiting `system()` function). `ls *` and `"ls *"` result in very different argument lists being passed to the application. The latter results in the wildcard character being taken literally which is almost certainly not what the user wants. – Username Obfuscation Mar 08 '15 at 05:09

0 Answers0