Hi Kurtis, On Wed, Nov 23, 2022, 4:57 PM Kurtis Rader <kra...@skepticism.us> wrote:
> On Wed, Nov 23, 2022 at 7:33 AM Christian Stewart <christ...@aperture.us> > wrote: > >> This is what I'm trying to accomplish: >> >> Start delve (dlv) with os/exec passing a set of []string os.Args as >> --build-flags >> >> Let's say I have this build arguments set: []string{"-gcflags", "-N -l"} >> >> - How can I pass this to --build-flags in Delve? strings.Join(flags, >> " ") is wrong. >> > > Note that there are two levels of quoting involved in your example and a > package that does "shell quoting" isn't going to handle this scenario > correctly except for uninteresting trivial cases. > Please explain? > - How can I pass this to "go" with os/exec correctly? The quotes are >> not put properly. >> > > I'm not sure I understand that question. If you're asking how to run `go > -gcflags="-N -l"` via a program written in Go the answer is to call os.exec > with the literal string "-gcflags=-N -l" as an argument. Note that if you > execute the `go` command directly, rather than indirectly via a shell, you > don't need quotes around the "-N -l" portion of that string since a shell > isn't parsing the command. > That doesn't work, from my tests This is why you should avoid running external commands indirectly through a > shell. Doing so greatly, and usually needlessly, complicates constructing > the command to be run. > > >> There are some cross-platform cases where the shell quoting is used, >> and the general "quote something" mechanics are similar on Windows and >> Linux. >> > > Similar is not the same as identical. The rules for how quoted strings, > and other punctuation, are handled differs enough that only trivial, > uninteresting, cases are likely to work correctly on both platforms. Heck, > even on UNIX like systems if the user has set their login shell to a > non-POSIX shell (like Fish or Elvish) and your code uses the login shell > rather than something like /bin/sh you will be in for a surprise. :-) > I'm not calling out to the shell, just passing args to another process that expects them to be shell quoted. Thanks, Christian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CA%2Bh8R2o8k%2BwLq%3DYPBLsaCePYP9de1q%2BvRk267Rnnbn7xyU_X0w%40mail.gmail.com.