I’m stuck on Stage #NI6
I’ve tried, parsing the arguments on my own, which helped me to pass the echo part, but I can’t pass the cat part.
Here are my logs:
[compile] Moved ./.codecrafters/run.sh → ./your_program.sh
[compile] Compilation successful.
[tester::#NI6] Running tests for Stage #NI6 (Quoting - Single quotes)
[tester::#NI6] [setup] export PATH=/tmp/pineapple/mango/pineapple:$PATH
[tester::#NI6] Running ./your_program.sh
[tester::#NI6] [setup] echo -n "pear raspberry." > "/tmp/cow/f 88"
[tester::#NI6] [setup] echo -n "banana strawberry." > "/tmp/cow/f 11"
[tester::#NI6] [setup] echo "raspberry mango." > "/tmp/cow/f 42"
[your-program] $ echo 'example shell'
[your-program] example shell
[tester::#NI6] ✓ Received expected response
[your-program] $ echo shell world
[your-program] shell world
[tester::#NI6] ✓ Received expected response
[your-program] $ echo 'hello script' 'world''example' shell''test
[your-program] hello script worldexample shelltest
[tester::#NI6] ✓ Received expected response
[your-program] $ cat '/tmp/cow/f 88' '/tmp/cow/f 11' '/tmp/cow/f 42'
[your-program] cat: /tmp/cow/f 88 /tmp/cow/f 11 /tmp/cow/f 42: No such file or directory
[tester::#NI6] ^ Line does not match expected value.
[tester::#NI6] Expected: "pear raspberry.banana strawberry.raspberry mango."
[tester::#NI6] Received: "cat: /tmp/cow/f 88 /tmp/cow/f 11 /tmp/cow/f 42: No such file or directory"
[your-program] 2026/01/03 08:16:48 exit status 1
[tester::#NI6] Assertion failed.
[tester::#NI6] Test failed
And here’s a snippet of my code:
data, err := Echo(cmdList)
if err != nil {
log.Fatal(err)
}
execute := exec.Command(cmdList[0], data)
this is how i handle cat and other commands, not echo, echo one is handled separately below, but I can create for cat, echo:
if cmdList[0] == "echo" {
if cmd == "echo" {
continue
}
data, err := Echo(cmdList)
if err != nil {
log.Fatal("Error", err)
}
fmt.Println(data)
continue
}
And here is my Echo function
func Echo(cmdList []string) (string, error) {
content := fmt.Sprint(cmdList[1:])
realContent := content[1 : len(content)-1]
opened := false
// doubleOpened := false
var result string
var quotesContent string
var withoutQuotesContent string
emitted := false
inArgument := false
for _, v := range realContent {
if v == '\'' {
//fmt.Println("bar")
opened = !opened
}
// if v == 39 {
// doubleOpened = !doubleOpened
// }
if opened && v != '\'' {
if withoutQuotesContent != "" {
if emitted && !inArgument {
result += " "
}
result += strings.Join(strings.Fields(withoutQuotesContent), " ")
withoutQuotesContent = ""
emitted = true
inArgument = true
}
quotesContent += string(rune(v))
} else if !opened {
if v == '\'' {
// then it is just closed
if quotesContent != "" {
if emitted && !inArgument {
result += " "
}
result += quotesContent
quotesContent = ""
emitted = true
inArgument = true
// fmt.Printf("--->%v\n", result)
// fmt.Println("baz")
}
} else {
//treat it as normal thing
if v == ' ' {
inArgument = false
}
withoutQuotesContent += string(rune(v))
// fmt.Println("foo")
// fmt.Println(withoutQuotesContent)
}
}
}
// fmt.Println(quotesContent)
// fmt.Printf("%q\n", withoutQuotesContent)
if withoutQuotesContent != "" {
if emitted && !inArgument {
result += " "
}
result += strings.Join(strings.Fields(withoutQuotesContent), " ")
}
return result, nil
}
Any help will appreciated, if above is not clear, please let me know in replies. Thanks.