# Problem with command gets

**URL:** <https://forum.crystal-lang.org/t/problem-with-command-gets/6561>\
**Category:** Help & Support\
**Created:** [February 10, 2024, 7:24am UTC](https://forum.crystal-lang.org/t/problem-with-command-gets/6561 "2024-02-10T07:24:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [February 10, 2024, 7:24am UTC](https://forum.crystal-lang.org/t/problem-with-command-gets/6561/1 "2024-02-10T07:24:23Z")

</div>

Hi guys, I have a simple question. I tried recently to fix a small graphic bug with my command line program I am coding. After I ask a question to the user, the program is waiting for a yes or no from it. It just need to type y | yes | n | no basically.

But how can I avoid gets to make a new line return ? I tried to enable the chomp, but change nothing

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [February 10, 2024, 12:14pm UTC](https://forum.crystal-lang.org/t/problem-with-command-gets/6561/2 "2024-02-10T12:14:51Z")

</div>

[`IO#gets`](https://crystal-lang.org/api/IO.html#gets%28limit%3AInt%2Cchomp%3Dfalse%29%3AString%7CNil-instance-method) always reads until it finds a line feed as delimiter. `chomp` only configures whether that trailing line feed is part of the returned string or not.

If you want to read individual characters, you can try [`IO#read_char`](https://crystal-lang.org/api/IO.html#read_char%3AChar%7CNil-instance-method).  
Note that you may have to do some extra work there, though.

If you want to build an interactive CLI, I’d recommend using a library for terminal UIs. [GitHub - I3oris/reply: A term reader for your REPL](https://github.com/I3oris/reply) is a good choice.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [February 10, 2024, 3:38pm UTC](https://forum.crystal-lang.org/t/problem-with-command-gets/6561/3 "2024-02-10T15:38:20Z")

</div>

In this particular case, doing `gets chomp: true` might be enough.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [February 10, 2024, 6:02pm UTC](https://forum.crystal-lang.org/t/problem-with-command-gets/6561/4 "2024-02-10T18:02:56Z")

</div>

> [@Blacksmoke16](#):
>
> gets chomp: true

Infortunately no. When I press enter, the program continue to make new line.

This is my code portion:

```auto
def getUserAgreement : Bool
            userInput = ""
            userAgreement = false

            loop do
                userInput = gets

                if userInput == ISM::Default::CommandLine::YesReplyOption
                    return true
                end
                if userInput == ISM::Default::CommandLine::NoReplyOption
                    return false
                end
            end
        end

```

Called like that for example:

```auto
Ism.showCalculationDoneMessage
Ism.showSoftwares(neededSoftwares)
Ism.showInstallationQuestion(neededSoftwares.size)

userAgreement = Ism.getUserAgreement

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [February 10, 2024, 6:04pm UTC](https://forum.crystal-lang.org/t/problem-with-command-gets/6561/5 "2024-02-10T18:04:19Z")

</div>

> [@Fulgurance](#):
>
> When I press enter, the program continue to make new line.

Right, your code is doing `gets` not `gets chomp: true`…

EDIT: Seems `true` is the default on the argless overload of `#gets`.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [February 10, 2024, 6:05pm UTC](https://forum.crystal-lang.org/t/problem-with-command-gets/6561/6 "2024-02-10T18:05:15Z")

</div>

No, I tried with the other solution you gave me, it’s the same result

I just past my code to make sure you can see how I implement (before I tried)

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [February 10, 2024, 6:07pm UTC](https://forum.crystal-lang.org/t/problem-with-command-gets/6561/7 "2024-02-10T18:07:12Z")

</div>

Are you sure? I tried with:

```crystal
print "With chomp: "

with_chomp = gets chomp: true

p with_chomp

print "Without chomp: "

without_chomp = gets chomp: false

p without_chomp

```

And output was:

```txt
With chomp: foo
"foo"
Without chomp: bar
"bar\n"

```

If you try the same program does it output the same for you?
