5

I want to split "-r --run" into "-r" and "--run"

for

string split " " "-r --run" 

I get

string split: -r --run: unknown option` as an error

It is mistaking the string for flags. How do I make it treat it as a string to split?

2
  • You might be interested in what $ man getopt tells you.
    – Hannu
    Commented May 21, 2023 at 17:05
  • I'd remove the linux tag, because fish is available for macOS, BSD and Windows as well (fishshell.com/#platform_tabs)
    – myrdd
    Commented Jun 11, 2023 at 0:02

2 Answers 2

4

The string docs say:

Arguments beginning with - are normally interpreted as switches; -- causes the following arguments not to be treated as switches even if they begin with -.

So the command is:

  string split -- " " "-r --run"
4
  • 5
    This is... interesting from the POV of the ban on chatGPT. The answer is perfectly correct, chatGPT gave it to you, and you cite your source whilst self-answering. I've not authority here whatsoever but if I did I think I'd think it was fine. But it it's interesting, as the first decent answer I've seen involving GPT. FWIW the -- convention for separating args from commands is very common: I'm surprised nobody suggested it earlier.
    – 2e0byo
    Commented May 20, 2023 at 21:33
  • 6
    I would say chatGPT mention should not be considered as source here (it's about the same as saying "it appeared to me in dream") and may be even removed. The answer itself is fine and just reproducible example with demonstrated result should be enough.
    – Cthulhu
    Commented May 21, 2023 at 0:05
  • According to the string docs, the options are intended to appear before the separator. string split -- " " $s Commented May 21, 2023 at 21:21
  • I felt free to edit this answer, replacing the ChatGPT citation by the fish docs. Also, I put -- before the separator, making it look more clear (so that the -- comes before all non-switch arguments).
    – myrdd
    Commented Jun 10, 2023 at 18:06
3

Another way to do it would be:

echo "--r -run" | string split " "

So piping the string into that command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .