polypush135

polypush135

CLI basics. How to handle input requirements?

I’m trying build a basic cli app for the purpose of learning.

Lets say we are making a basic cli for counting words of one or more txt documents

The requirement of my app is as listed:

  • The program accepts as arguments a list of one or more file paths (e.g. ./wordcount file1.txt file2.txt …).
  • The program also accepts input on stdin (e.g. cat file1.txt | ./wordcount).

First of all here is my current working example of a cli escript app. https://gist.github.com/joshchernoff/4a6678a555f61edc771649576c344f58

One of the things not listed that bothered me was the app would just hang if no input via args or sdtin was given.

I understand the app was just waiting for input from the terminal and if I typed it would read via IO.read, but I would think that it would be more helpful that I gave an error if no input was given and then just stop the app.

I didn’t realize that it’s a common thing for stdio to just wait there.

I guess at this point I’m at a loss for common bast practices.

  • should the app just hang expecting some form of stdin?
  • should the app be smart and see nothing is happening and report according?
  • should I have an explicit flag that denotes how I want to explicitly use the app?

Sorry for such a open ended question, its just that this was also a part of a tech challenge for a job interview that I completely tanked today. Not feeling very good about myself right now. I wish I had a better understanding of standards and this type of utility apps feels very foreign to me as primarily a web developer just starting to make phoenix apps.

Most Liked

cmkarlsson

cmkarlsson

It is very common for command line applications to just wait for input if no arguments are given.

For example: cat, grep, wc, sed, awk, sed and many others. Try these without a file argument and they will just hang there and wait for input. Use Ctrl-D to give eof

None of these applications warn if there is no standard input as that is the normal way they work and it is what make them easy to use in pipes.

Other command line applications have a flag or use dash - instead of the filename to specify that you want them to read from standard in. For example: xmllint, (I know there are many more but can’t think of one now). This way you can show them a help text indicating that they need to provide a filename or - for stdin if they haven’t provided any arguments.

It is also common to write things to standard out, or at least give an option to write to standard out instead of a file. For example wget allows you to specify and outfile with -O filename but the option to write to standard out with -O -. Funnily enough curl works the other way around where the default is to write to standard out but you can save to a file with -o filename.

As I almost exclusively work with command line applications I think a well behaved command line app should

  • Support reading and writing from stdin/stdout (if it makes sense for the application). I don’t mind if I have to provide a flag for this
  • Write errors and warnings to stderr
  • Return correct error codes 0 for success. non zero for failure.
  • Have a man page with good descriptions and examples. This is getting less common
  • Have a short hand help through -h or --help. I like this to be short and fit on a page. The rest is for man.
  • Have a non-interactive flag if normally interactive (so that things can get automated)
  • Likely more which I haven’t thought about :smiley:

So perhaps your cli app can do this (just an example).


$ ./wordcount
Usage: wordcount OPTION... [FILE]...
Counts words in each FILE and print to standard output

When FILE is -, read standard input.

OPTIONS are:

  -i, --ignore=REGEX  Ignore words matching REGEX

$ ./wordcount --help
Usage: wordcount OPTION... [FILE]...
Counts words in each FILE and print to standard output

When FILE is -, read standard input.

OPTIONS are:

  -i, --ignore=REGEX  Ignore words matching REGEX

$ ./wordcount hello.txt
32

$ cat hello.txt | ./wordcount -
32

$ ./wordcount hello.txt world.txt
132

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement