6 ms·
Everyone does this "wrong" because every app does this differently. The core reason windows command line options aren't (or at least shouldn't be) used to pass
by ajross 10y ago
Everyone does this "wrong" because every app does this differently. The core reason windows command line options aren't (or at least shouldn't be) used to pass complicated data is that way back when DOS simply provided a single command line string to the executed program and let it parse it itself. So no two command line parsers are the same. The glitch with escaping here is merely one symptom of a broader problem.
Unix got this right by forcing the shell to provide the kernel a pre-parsed list of strings, so the only insanity the tool integrator needs to understand is the shell's quoting syntax. Which is still insane. But it's only insane in one particular way.
- comex 10y agoOn Unix, most tools don't really need to use the shell at all; it's enough to treat argument lists as lists internally and pass them to exec or posix_spawn (which of course, unlike the Windows _exec and _spawn, aren't broken for arguments with spaces!). However, at minimum it is still useful to shell-escape arguments when displaying them to the user (for ease of copy+paste), so it's unfortunate that many languages don't have any standard library function to do this - including C on POSIX, and Python before version 3.
- iokanuon 10y agoBut in comparasion with MS systems, escaping strings for unix shells is very simple - just prepend and append an ' and change every ' into '\''
- ajross 10y agoAlas, no. Bourne shell syntax allows for double quotes with variable interpolation and some other fancy syntax (including backslash escaping of literal double-quote characters), and a single-quote syntax for "raw" strings with no fancy syntax INCLUDING backslash escaping. So your rule won't work. You can't single-quote a string that itself contains single quotes, which makes for some fun when you have arbitrary strings (file names are the big frustration) that need to be substituted into a parseable command line. But like I said above: Bourne syntax[1] is only one kind of insanity, which is still much better than the DOS/Windows world of a separate parser for every app. [1] We shall not speak of the C shell here.
- skissane 10y agoA method I've used: replace all occurrences of ' with '"'"' and then surround resulting string with ' It's a bit ugly but it works.
- ufo 10y agoTheir rule does work. The single quotes are transformed into '\''. The first ' closes the current single quotes string. The \' adds a single quote character outside a quoted string (and outside single quotes you can use backslash escapes). Finally, the last ' reopens the single quoted string that we closed before.
- d0mine 10y agopipes.quote() is in Python's stdlib since forever. Though it was not documented until it became shlex.quote() in Python 3.
- kazinator 10y ago_exec an _spawn are not Windows; they are functions in the MS Visual C redistributable run-time. (Well, they are also in a system DLL called MSVCRT.DLL. That is an internal library which is undocumented and considered by Microsoft to be off-limits to applications.)
- pwdisswordfish 10y agoUnix got something right in that you can unambiguously pass a list of separate strings to launched processes. However, it does nothing to ensure unambiguous meaning of those strings. This is for example why you should avoid giving your files such cute names as '-rf'.
- sukilot 10y agoWhat does it mean to say that an argument's meaning could be unamibigious regardless of the program it is passed into ? that's a logical impossibility Still, it would be right and proper if Unix programs a little type-safety in their arguments, for example by requiring that ALL arguments be flags, as in this hypothetical smart_rm: "/bin/smart_rm -rf --pattern foo/bar"
- tuukkah 10y agoYou can escape file names that look like options with '--'.
- ufo 10y agoThis depends on the application recognizing the -- convention and also depends on having all the little scripts in your system remembering to use the --.
- kazinator 10y agoEven if the OS kernel provided a process launching API with separated options and arguments, that would not remove the need for the -- syntax to remove the ambiguity at the shell level, and hence your need to use that in scripts. It would remove the problem of programs all being required to implement --.
- quotemstr 10y ago> This is for example why you should avoid giving your files such cute names as '-rf'. The kernel should ban these names. I'm a big fan of dwheeler's proposal for fixing filenames: see http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html http://www.dwheeler.com/essays/fixing-unix-linux-filenames.h... These is no god damn reason why a filename should be able to contain, say, LF, DEL, or BEL. None whatsoever.
- kevin_thibedeau 10y agoMS does provide an alternate startup routine that parses the arguments before entering main().
- kazinator 10y ago> the only insanity the tool integrator needs to understand is the shell's quoting syntax Only if the designer is using command-based functions like the ISO C system or POSIX popen; not when forking and exec'ing programs.