Git Product home page Git Product logo

kwargs's Introduction

Kwargs

This is a packet that provides a very easy way to create command line argument parsers using convention over configuration. It does not intend to replace packages like cmdargs but it's scope is quick prototyping of applications. If you are, for example, in the early development of an application, Kwargs allows you to easily create a command line argument parser which you can later replace with something more powerful like cmdargs. With all honesty, I wrote this to improve the quality of homework assignments w/o having to do much.

Usage

  1. Define your settings type, which derives from generic. Make sure the 'DeriveGeneric' compiler flag is turned on:
{-# Language DeriveGeneric -#}
import GHC.Generics

data Config = Config{
    o1 :: String, -- Regular string specified by --o1=value
    o2 :: Bool, -- Flag which is set to True when --o2 is passed and set to False when not passed
    o3 :: Maybe String, -- Same as option1 but it is set to Nothing when --o3 is not present
    o4a_o4b :: Maybe (String, String)  -- It is set to Nothing when neither flag is present, 
                                       -- set to the value 'Just (val1,val2) when --o4a=val1 and --o4b=val2 is present
                                       -- Fails if only one of the two flags is present
} deriving (Generic, Show)
  1. Read the argumetns:
   import System.Console.CmdArgs.Generic (kwargs, getBuilders)
   import Control.Applicative ((<$>)) -- For syntactic convenience

   main = (kwargs (getBuilders :: BaseBuilder Config) <$> getArgs) >>= print
  1. Assuming that the code from step 2 is compiled into an executable called 'args':
> args --o1=v1 --o2 --o3=v2 --o4a=v3 --o4b=v4
Right (Config{o1="v1", o2=True, o3=Just "v3", o4=Just ("v3","v4")})
> args --o1=v1 --o4a=v3 --o4b=v4
Right (Config{o1="v1", o2=False, o3=Nothing, o4=Just ("v3","v4")})
> args --o2 --o3=v2 --o4a=v3 --o4b=v4
Left ("--o1 is missing")

kwargs's People

Contributors

netogallo avatar

Watchers

James Cloos avatar  avatar  avatar

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.