Handle command-line arguments in ClojureScript

Alan Shaw
InstructorAlan Shaw
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

In this lesson I show you how to get and manipulate command-line arguments in Clojure/ClojureScript by writing a small program that greets you by name. I also show how comments are delimited, how to get a value out of a sequence, how to bind a symbol to a value, and how to define a function.

Instructor: [00:00] In this lesson, we'll write a ClojureScript program that handles command line arguments, a program called greet, that when invoked with your name, will say hello to you. Let's open a file called greet.

[00:15] We'll start off like this. The first line sets Lumo as the interpreter for our script. If you don't have Lumo installed, you can refer to this webpage for several ways to install it. I chose to use NPM. Here's our documentation.

[00:33] Clojure and ClojureScript ignore anything from a semicolon to the end of the line. It's common to introduce a whole line comment with two semicolons. When a Clojure or ClojureScript program starts up, a var named command-line-arguments, spelled like this with dashes and stars on the sides, is bound to a sequence of the command line arguments.

[00:58] They're called vars, rather than variables, because they don't vary. They're immutable. Let's see what it looks like if I print it out. We invoke the built-in function, printLine, by enclosing it and any arguments in parentheses.

[01:15] First, we have to make the file executable. Now, run the program with my name, Allen, as an argument. The output is a list, indicated by parentheses, containing a single element, the string Allen. What I want to do is to get that string and use it to greet myself.

[01:41] I can get the first item in the list with the first function. Let's run it again. We have Allen by itself. Now, rather than immediately printing out that value, I'd like to give it a name that I can call it by. The easiest way to do that in Clojure is with def.

[02:02] def binds the value obtained by evaluating the first command line args to the symbol user. Now, let's define a function that returns a greeting for a user. For that, we defn, define function, instead of def. We define the function greeting to expect one parameter, user, indicated by the square brackets.

[02:28] Greeting calls the str function, which concatenates the string representations of its arguments. There's no need to say return in a Clojure function. The function returns whatever its last expression evaluates to.

[02:42] Having defined the symbol user and the function greeting, we can now say printLine, greeting, user. That's our program. We run it, and it says, "Hello, Allen."

~ 6 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today