Giter VIP home page Giter VIP logo

readargs's Introduction

ReadArgs provides the readArgs IO action, which lets you tell the compiler to parse the command line arguments to fit the type signature you give.

For example (a :: Int, b :: String, c :: Float) <- readArgs would parse the first runtime argument as an Int, the second as a String (no quotes required) and the third as a float.

If the runtime arguments are incompatible with the type signature, then a simple usage statement is given of the types needed.

Continuing the previous example, if it was used in a program named Example, the error message for the above action would be:

usage: Example Int String Float

Any type that has both Typeable and Read instances can be used. Char, String, and Text are handled specially so that command line arguments for both do not require quotes (as their Read instances do). A special instance is provided for FilePath so that no constructor or quotes are required.

readArgs also supports optional arguments and variadic arguments. Optional arguments are specified using Maybe, and variadic arguments using a list. (a :: Int, b :: Maybe String, c :: [Float]) <- readArgs would successfully parse any of the following sets of command line arguments:

Example 1
Example 1 2 3 4
Example 1 foo
Example 1 foo 2 3 4

But not

Example
Example foo
Example 1.0

Usage statements for optional and variadic arguments use command-line parlance:

usage: Example Int [String] [Float..]

Note that both optional and variadic parsers are greedy by default (so Example 1 2 3 4 was parsed as (1, "2", [3.0,4.0]). They may both be made non-greedy through use of the NonGreedy constructor:

( a :: Int
, NonGreedy b :: NonGreedy Maybe String
, NonGreedy c :: NonGreedy [] Float
) <- readArgs

readargs's People

Contributors

danburton avatar rampion avatar snoyberg avatar sol avatar vandenoever avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

readargs's Issues

usage message for sum types

Not sure if this is possible, but I have a sumtype Data Mode = One | Two.
When there is no argument I get an error message like this:

usage: program Mode

I would like a meesage that explains the possible options of One or Two. Maybe this can be done if it derives Bounded

Please include ReadArgsSpec.hs in the tarball

As we build the tests on Gentoo. Not sure why its not included, as when I do:

cabal sdist

it includes it. This is with:

cabal --version

cabal-install version 1.16.0.1
using version 1.16.0.2 of the Cabal library

hspec 2.2.* support

The test-suite for this package has been disabled in stackage because of this outdated dependency. If a fix is published to hackage please ping me.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.