# Is there any equivalent to C's main() or python's if-main statement?

**URL:** <https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450>\
**Category:** Help & Support\
**Created:** [February 21, 2019, 5:42am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450 "2019-02-21T05:42:22Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [February 21, 2019, 5:42am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/1 "2019-02-21T05:42:22Z")

</div>

Is there some equivalent to C’s `int main()` or python’s `if __name__ == " __main__":`? Basically I want to call a method when my shard is executed but not if the shard has been required as a library. Is the only solution to create a separate file, say, `cli.cr` with contents like this…?

```crystal
require "library"
Library.command_line_interface ARGV

```

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [February 21, 2019, 9:48am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/2 "2019-02-21T09:48:29Z")

</div>

I created a PR to implement this. There you will soon find opinions about this: [https://github.com/crystal-lang/crystal/pull/7466](https://github.com/crystal-lang/crystal/pull/7466)

Edit: added the word “soon” because just right now there are no opinions 🙂

---

<div class="post-metadata">

**Author:** ![girng](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng/32/127_2.png) [@girng](https://forum.crystal-lang.org/u/girng)\
**Post date:** [February 23, 2019, 5:44pm UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/3 "2019-02-23T17:44:59Z")

</div>

One of the core developers of the language creates a PR and gets 6~ downvotes. All because he listened to a user on the forum and wanted to help. That’s pretty sad and doesn’t sit well with me at all.

---

<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 23, 2019, 8:51pm UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/4 "2019-02-23T20:51:32Z")

</div>

He explicitly stated that the PR was meant as an experiment to ask for opinions. And I’m pretty sure he was well aware that it was relatively unlikely that this feature would make it into the compiler. So, I don’t think there has been any harm done.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [February 23, 2019, 9:17pm UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/5 "2019-02-23T21:17:24Z")

</div>

I also didn’t want to merge it. But if many people liked it then there was a chance to merge it (probably as a specific macro call).

I just wanted to have real opinions/reactions against a working PR: if it was liked then we could merge it. If not then it serves as a document with reasons for future people asking about this feature.

---

<div class="post-metadata">

**Author:** ![girng](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng/32/127_2.png) [@girng](https://forum.crystal-lang.org/u/girng)\
**Post date:** [February 23, 2019, 9:18pm UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/6 "2019-02-23T21:18:03Z")

</div>

Oh I agree, giving opinions and criticism about the PR is great! But I believe downvoting it is not appropriate or deserved. Just my 2 cents

---

<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 23, 2019, 10:06pm UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/7 "2019-02-23T22:06:00Z")

</div>

A 👎 reaction is just an expression of opinion about the subject (which was explicitly asked for), and not defaming the author’s effort. I don’t think that should be interpreted as rude.

---

<div class="post-metadata">

**Author:** ![Sija](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sija/32/737_2.png) [@Sija](https://forum.crystal-lang.org/u/Sija)\
**Post date:** [February 24, 2019, 12:21am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/8 "2019-02-24T00:21:55Z")

</div>

I totally agree with @straight-shoota, I take it as an expression of an opinion regarding the **subject** of the given PR, there’s no need to take it to yourself.

---

<div class="post-metadata">

**Author:** ![girng](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng/32/127_2.png) [@girng](https://forum.crystal-lang.org/u/girng)\
**Post date:** [February 24, 2019, 6:51am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/9 "2019-02-24T06:51:05Z")

</div>

I don’t think it’s necessarily defaming, I just think it’s not appropriate / deserved. Especially considering the core developer listened to a user’s problem and wanted to implement a solution. That’s commendable behavior, and certainly doesn’t warrant a thumbs down.

---

<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 24, 2019, 7:35am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/10 "2019-02-24T07:35:48Z")

</div>

Please don’t misinterpret the meaning. It’s simply an expression of opinion.  
Do you now any practical alternative to convey this meaning differently? (Without everyone posting a comment saying “I’m against this proposal”)

---

<div class="post-metadata">

**Author:** ![girng](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng/32/127_2.png) [@girng](https://forum.crystal-lang.org/u/girng)\
**Post date:** [February 24, 2019, 7:10pm UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/11 "2019-02-24T19:10:24Z")

</div>

I’m not misinterpreting the meaning :/

The practical / alternative way is to respond politely / formally / professionally and show criticism about the proposal? Which is what happened! But to downvote, why?

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [February 25, 2019, 9:34am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/12 "2019-02-25T09:34:17Z")

</div>

Because it gives a much better overview. If everybody comments, likely with the same arguments as before, also often more than once with a changed opinion, it’s very hard to keep track and a good overview of the community’s feeling towards a particular issue.

So for example for me I did both, downvote and, given my arguments were not presented yet by anybody else, add a comment explaining why I’m against this. If somebody already had presented these same arguments I wouldn’t have commented and just kept to the downvote.

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [February 26, 2019, 2:06am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/13 "2019-02-26T02:06:31Z")

</div>

Downvotes on reddit mean “this is not a quality post”. Thumbs down reactions on github mean “I disagree with this comment”.

---

<div class="post-metadata">

**Author:** ![girng](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng/32/127_2.png) [@girng](https://forum.crystal-lang.org/u/girng)\
**Post date:** [February 26, 2019, 3:24am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/14 "2019-02-26T03:24:34Z")

</div>

There is nothing to “disagree with” though. The PR was made in light of a user having a problem. That’s commendable behavior, not “oh, I can disagree with this, let’s thumb down a core developer’s post!”. That’s not right. Doesn’t have to be a core developer either, anyone for that matter.

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/scott/32/948_2.png) [@scott](https://forum.crystal-lang.org/u/scott)\
**Post date:** [February 26, 2019, 3:27am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/15 "2019-02-26T03:27:21Z")

</div>

But my “problem” was simply not knowing the conventions of the language. The disagreement was with adding a new feature to the language and how that feature would be implemented. I look forward to their being a convention for this idea, but I respect people’s decision that one convention doesn’t fit with the rest of the language.

I would definitely prefer it if there were a conventional place to put files that _will be_ compiled to binaries and for “shards build” to create said binary, but I kinda agree that the way I suggested doesn’t fit very well.

---

<div class="post-metadata">

**Author:** ![girng](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/girng/32/127_2.png) [@girng](https://forum.crystal-lang.org/u/girng)\
**Post date:** [February 26, 2019, 3:39am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/16 "2019-02-26T03:39:30Z")

</div>

> [@scott](#):
>
> The disagreement was with adding a new feature to the language and how that feature would be implemented

Oh for sure, but taking in the context of the entire post within the PR, where it specifically links to this forum thread… and then seeing it have -6+ thumbs down… feels weird.

I would never in a million years thumbs down a core developer in any repo; merely because the PR came to fruition over trying to implement a solution from a question / problem a user expressed on _their forum_.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [February 26, 2019, 10:09am UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/17 "2019-02-26T10:09:29Z")

</div>

Don’t worry too much. I expected downvotes. I don’t want this feature in the language. So each downvote makes me slightly happier. Also, downvotes are just bits in a server. And you can always flip your head and they become upvotes. It all depends on how you take it.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [March 2, 2019, 5:51pm UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/18 "2019-03-02T17:51:55Z")

</div>

Ruby has the somewhat awkward

```
if __FILE__ == $0

```

FWIW :)

> <https://stackoverflow.com/questions/487086/run-a-ruby-library-from-the-command-line/487134#487134>

---

<div class="post-metadata">

**Author:** ![RespiteSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/respitesage/32/793_2.png) [@RespiteSage](https://forum.crystal-lang.org/u/RespiteSage)\
**Post date:** [March 13, 2019, 5:18pm UTC](https://forum.crystal-lang.org/t/is-there-any-equivalent-to-cs-main-or-pythons-if-main-statement/450/19 "2019-03-13T17:18:13Z")

</div>

At the risk of performing some minor thread necromancy, I’d like to share the convention for this kind of thing that I’ve been using. This was influenced by looking through the code in [Scry](https://github.com/crystal-lang-tools/scry), specifically [scry.cr](https://github.com/crystal-lang-tools/scry/blob/master/src/scry.cr).

The project directory structure has the following form:

- src/
  - library\_module\_name/
    - …

  - project\_name.cr (or maybe “driver.cr”)

- spec/
  - …

- …

Then in your `project_name.cr` (again, or `driver.cr` or whatever):

```auto
require "./library_module_name/*"

module LibraryModuleName
  def self.run
    # do special things like show your CLI
  end
end

LibraryModuleName.run

```

This works really well when using the `OptionParser` to create a CLI that can go in another file, like `src/configuration.cr`, and be required and called in the driver.

I don’t know if this is actually the kind of solution you’re looking for, but I’ve found that it allows for pretty nice separation of the library from the standalone executable.
