Unix-style command line arguments and file input?

Hello all, Punchline first: What's the "best practice" way of doing unix-style command line argument and file input processing in Haskell? Background: I'm using Haskell to write programs that act like well-behaved, pipe-friendly unix tools. i.e., the following are all equivalent: % ./prog file % ./prog < file % cat file | ./prog Thus far, I've done this by directly inspecting the first element of System.Environment.getArgs, which has been fine thus far. I'd also like to be able to take simple command line arguments (boolean flags and numeric parameters) and the above doesn't adapt well to that case. I'd like to do this in the idiomatic, "standard" way (a la getopt() in C). Browsing through the wiki page on command line argument parsers [1] gave me a bewildering array of options. I'm not really sure where to start, though I remember reading a blanket endorsement of optparse-applicative somewhere. Any pointers or examples that address my use-case would be much appreciated. -vale [1]: https://wiki.haskell.org/Command_line_option_parsers

On 05/05/2015 01:43 PM, Vale Cofer-Shabica wrote:
Hello all,
Punchline first: What's the "best practice" way of doing unix-style command line argument and file input processing in Haskell?
Background: I'm using Haskell to write programs that act like well-behaved, pipe-friendly unix tools. i.e., the following are all equivalent:
% ./prog file % ./prog < file % cat file | ./prog
Thus far, I've done this by directly inspecting the first element of System.Environment.getArgs, which has been fine thus far.
I use CmdArgs for this. I think the simplest example is, https://hackage.haskell.org/package/email-validator And one with multiple modes: https://hackage.haskell.org/package/hath It's not *quite* what you asked for: I use e.g. `hath -i file` to mean the same thing as `hath < file`, but that's as close as I got.

On Tue, May 5, 2015 at 12:58 PM, Michael Orlitzky
I use CmdArgs for this. I think the simplest example is,
https://hackage.haskell.org/package/email-validator
And one with multiple modes:
https://hackage.haskell.org/package/hath
It's not *quite* what you asked for: I use e.g. `hath -i file` to mean the same thing as `hath < file`, but that's as close as I got.
I also use CmdArgs, though I'm not positive it's still the best choice. eddie (http://chiselapp.com/user/mwm/repository/eddie/doc/tip/README.md) correctly handles the cases you want, and in additions makes: $ cat cat file1 file2 file3 | ./prog $ ./prog file1 file2 file3 do the same thing. However, it's a bit complicated, as eddie has multiple interacting options, an accumulating option, and defaults the first argument to an option and the rest to files to be processed. Figure out the simpler examples first.

Hi there, I've had fun using cmdargs[1]. Here are several sites that I found to be very helpful in learning how to use the package: - This was probably the single most helpful bit of example code[2] that I found because it illustrates how to have multiple modes that take different option flags better than other tutorials. - Neil Mitchell, the author of cmdargs, has some nice example[3] code[4] that is also useful for seeing how the library works. - Here is another an example[5] that shows how to make Hello World take a few basic command arguments. Hope this helps, -- Rianna Morgan rmorgan@trystero.is On Tue, May 5, 2015, at 10:43 AM, Vale Cofer-Shabica wrote:
Hello all,
Punchline first: What's the "best practice" way of doing unix-style command line argument and file input processing in Haskell?
Background: I'm using Haskell to write programs that act like well-behaved, pipe-friendly unix tools. i.e., the following are all equivalent:
% ./prog file ./prog < file cat file | ./prog
Thus far, I've done this by directly inspecting the first element of System.Environment.getArgs, which has been fine thus far.
I'd also like to be able to take simple command line arguments (boolean flags and numeric parameters) and the above doesn't adapt well to that case. I'd like to do this in the idiomatic, "standard" way (a la getopt() in C). Browsing through the wiki page on command line argument parsers [1] gave me a bewildering array of options. I'm not really sure where to start, though I remember reading a blanket endorsement of optparse-applicative somewhere.
Any pointers or examples that address my use-case would be much appreciated.
-vale
[1]: https://wiki.haskell.org/Command_line_option_parsers _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Links: 1. https://hackage.haskell.org/package/cmdargs 2. https://zuttobenkyou.wordpress.com/2011/04/19/haskell-using-cmdargs-single-a... 3. http://neilmitchell.blogspot.com/2010/08/cmdargs-example.html 4. http://community.haskell.org/~ndm/cmdargs/ 5. http://spin.atomicobject.com/2012/12/13/using-haskells-cmdargs-package/

While I used to use cmdargs, at some point I switched to optparse-applicative and never strayed. My only complain about it is that it uses strings everywhere instead of text. On Tue, May 5, 2015 at 1:43 PM, Vale Cofer-Shabica < vale.cofershabica@gmail.com> wrote:
Hello all,
Punchline first: What's the "best practice" way of doing unix-style command line argument and file input processing in Haskell?
Background: I'm using Haskell to write programs that act like well-behaved, pipe-friendly unix tools. i.e., the following are all equivalent:
% ./prog file % ./prog < file % cat file | ./prog
Thus far, I've done this by directly inspecting the first element of System.Environment.getArgs, which has been fine thus far.
I'd also like to be able to take simple command line arguments (boolean flags and numeric parameters) and the above doesn't adapt well to that case. I'd like to do this in the idiomatic, "standard" way (a la getopt() in C). Browsing through the wiki page on command line argument parsers [1] gave me a bewildering array of options. I'm not really sure where to start, though I remember reading a blanket endorsement of optparse-applicative somewhere.
Any pointers or examples that address my use-case would be much appreciated.
-vale
[1]: https://wiki.haskell.org/Command_line_option_parsers _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Wow! A big thank you to everyone who responded. I was able to get up
and running very rapidly with cmdArgs. The two resources that really
got me going were:
* http://spin.atomicobject.com/2012/12/13/using-haskells-cmdargs-package/
* https://zuttobenkyou.wordpress.com/2011/04/19/haskell-using-cmdargs-single-a...
I'm going to read through:
https://hackage.haskell.org/package/email-validator
for ideas now that I understand the basics.
eddie looks very interesting; I was longing for something similar recently.
Many thanks again,
vale
On Tue, May 5, 2015 at 2:12 PM, David McBride
While I used to use cmdargs, at some point I switched to optparse-applicative and never strayed. My only complain about it is that it uses strings everywhere instead of text.
On Tue, May 5, 2015 at 1:43 PM, Vale Cofer-Shabica
wrote: Hello all,
Punchline first: What's the "best practice" way of doing unix-style command line argument and file input processing in Haskell?
Background: I'm using Haskell to write programs that act like well-behaved, pipe-friendly unix tools. i.e., the following are all equivalent:
% ./prog file % ./prog < file % cat file | ./prog
Thus far, I've done this by directly inspecting the first element of System.Environment.getArgs, which has been fine thus far.
I'd also like to be able to take simple command line arguments (boolean flags and numeric parameters) and the above doesn't adapt well to that case. I'd like to do this in the idiomatic, "standard" way (a la getopt() in C). Browsing through the wiki page on command line argument parsers [1] gave me a bewildering array of options. I'm not really sure where to start, though I remember reading a blanket endorsement of optparse-applicative somewhere.
Any pointers or examples that address my use-case would be much appreciated.
-vale
[1]: https://wiki.haskell.org/Command_line_option_parsers _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (5)
-
David McBride
-
Michael Orlitzky
-
Mike Meyer
-
Rianna Morgan
-
Vale Cofer-Shabica