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. > - 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. 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. :-) On Sun, Nov 6, 2022 at 9:11 PM Kurtis Rader <kra...@skepticism.us> wrote: > > > > On Sun, Nov 6, 2022 at 8:48 PM 'Christian Stewart' via golang-nuts < > golang-nuts@googlegroups.com> wrote: > >> > >> There have been several discussions about shellquote in go-nuts in the > past: > >> > >> https://github.com/kballard/go-shellquote > >> https://github.com/gonuts/go-shellquote > >> > >> I've found it necessary to use both Split and Join in some cases when > working with exec.Command. > >> > >> This seems like the type of thing that ought to be part of the stdlib, > has there been any consideration towards including it or a similar > implementation? > >> > >> This is useful for processing os.Args especially in CLI or emulated > shell environments. > > > > > > No, IMHO, because such quoting is OS specific. For example, the quoting > for UNIX and Windows in this context are different. Furthermore, given the > vagaries of the POSIX shell standard I am skeptical that either of the > projects you linked to correctly manipulates such strings in every case. > > > > It would help if you provided a concrete example of the problem you need > help with. > > > > -- > > Kurtis Rader > > Caretaker of the exceptional canines Junior and Hank > -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank -- 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/CABx2%3DD-E2Ut1A-prNgmURATLz%3DS6771jvDW0Z8X3Y3xwKtUR1g%40mail.gmail.com.