0

I'm trying to build a Haskell CLI that can either read in strings from stdin or from arguments like so:

cat colors-multi-line | colorshift -d 10

colorshift -d 10 "#FFFFFF"

How would I accomplish this?

  • You do what you just said. You either read something from the command line using `getArgs` or you read something from stdin using `getLine`. You've not said when you want it to be one or the other - but basically, just apply whatever criteria you have for this. – Cubic Jul 07 '18 at 23:27
  • If the user doesn't provide anything to stdin when the program starts, I'd like it to expect an argument with their input. If no input is found from either, the help screen should display. – Carolyn Knight-Serrano Jul 07 '18 at 23:29
  • You can import `getArgs` `System.Environment`, and do pattern matching on the result of `getArgs`. ``` import System.Environment main = do args <- getArgs case args of [flag, n] -> _ [flag, n, color] -> _ -> _ ``` – Yuan Wang Jul 08 '18 at 03:18
  • @CarolynSaunders You can't check if anything is on STDIN - it could come with any amount of delay. Imagine `incredibly_long_running_application | your_program` – Cubic Jul 08 '18 at 08:03

1 Answers1

1

There are several libraries to handle CLI argument parsing. optparse-applicative is the one with which I'm most familiar. getLine and getContents in Preluderead from STDIN.

bergey
  • 3,041
  • 11
  • 17