From 865dca5a7d38fa63aa8d497ab89a3be9ea0e444d Mon Sep 17 00:00:00 2001 From: defanor Date: Sun, 3 Dec 2017 15:14:07 +0300 Subject: Update README and Example.hs --- Example.hs | 72 +++++++++++++++++++++-------------------------------------- README.md | 48 ++++++++++++++++++++++++++------------- coalpit.cabal | 11 ++------- 3 files changed, 60 insertions(+), 71 deletions(-) diff --git a/Example.hs b/Example.hs index 1811fc4..9230d97 100644 --- a/Example.hs +++ b/Example.hs @@ -6,54 +6,32 @@ import GHC.Generics import Text.Megaparsec import Coalpit -data RecTest = RecTest { a :: Maybe Int - , b :: Maybe Double - , c :: Maybe Int } - deriving (Generic, Show) - -instance ArgParser RecTest -instance ToArgs RecTest - -data Foo = Bar Int - | Baz Int - | Qux [Int] (Maybe Int) (Either String Int) RecTest (Maybe Double) - deriving (Generic, Show) - -instance ArgParser Foo -instance ToArgs Foo - -data Wrap = Wrap { foo :: Maybe Foo, num :: Maybe Int } - deriving (Generic, Show) - -instance ArgParser Wrap -instance ToArgs Wrap +data FooArgs = FooArgs { arg1 :: Int + , arg2 :: String + } deriving (Generic, Show) +instance ArgParser FooArgs +instance ToArgs FooArgs + +data FooBar = Foo FooArgs + | Bar + deriving (Generic, Show) +instance ArgParser FooBar +instance ToArgs FooBar + +data Input = Input { something :: Maybe String + , fooBar :: Maybe FooBar + , fooBar2 :: FooBar + } deriving (Generic, Show) +instance ArgParser Input +instance ToArgs Input main :: IO () main = do - let val = Wrap (Just $ Qux [1,2,3] Nothing (Left "foo bar") - (RecTest Nothing (Just 2.3) Nothing) Nothing) (Just 1) - a = toArgs defMod val + let val = Input { something = Nothing + , fooBar = Just (Foo (FooArgs { arg1 = 1 + , arg2 = "a string"})) + , fooBar2 = Bar} + args = toArgs defMod val print val - print a - print $ parse (argParser defMod :: Parser Wrap) "test" a - - -data Record = Record { maybeInt :: Maybe Int - , maybeDouble :: Maybe Double - , str :: String - , listOfStrings :: [String] - , maybeListOfNumbers :: Maybe [Int] - , otherString :: String - } deriving (Generic, Eq, Show) -instance ArgParser Record -instance ToArgs Record - - -data NestedRecursiveRecord = - NestedRecursiveRecord { record1 :: Maybe Record - , recursiveRecord :: Maybe NestedRecursiveRecord - , record2 :: Maybe Record - } deriving (Generic, Eq, Show) -instance ArgParser NestedRecursiveRecord -instance ToArgs NestedRecursiveRecord - + print args + print $ parse (argParser defMod :: Parser Input) "test" args diff --git a/README.md b/README.md index f941909..e46037d 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,44 @@ # Coalpit -Coalpit is a library for building command-line program -interfaces. They are like command-line user interfaces, but for -programs. +Coalpit is a library for +building +[command-line program interfaces](https://defanor.uberspace.net/notes/command-line-program-interface.html): +the goal is to get interfaces between programs quickly and easily, +while keeping them language-agnostic and more user- and shell +scripting-friendly than JSON and similar formats. Given a type, it derives instances to print and parse it as -command-line arguments. The resulting serialization wouldn't be as +command-line arguments. The resulting deserialization wouldn't be as nice as that of e.g. [optparse-generic](https://hackage.haskell.org/package/optparse-generic), -but the aim here is to handle arbitrary types. - -The goal is to -faciliate -[the KISS principle](https://en.wikipedia.org/wiki/KISS_principle) -preservation for interfaces between system components in certain -(rather [unixy](https://en.wikipedia.org/wiki/Unix_philosophy)) -architectures. Described in more detail in -the -[command-line program interface](https://defanor.uberspace.net/notes/command-line-program-interface.html) note. +but the aim here is to handle more or less arbitrary types. Warning: it is currently possible to run into ambiguity by defining a recursive structure with optional named elements. -Far from production-ready yet, merely a prototype. +Not production-ready yet, merely a prototype. + +## Example + +An example is available in `Example.hs`. Given the following Haskell +value: + +```haskell +Input { something = Nothing + , fooBar = Just (Foo (FooArgs { arg1 = 1 + , arg2 = "a string"})) + , fooBar2 = Bar} +``` + +Its serialized version should look like this: + +```haskell +["--foobar","foo","--arg1","1","--arg2","a string","--foobar2","bar"] +``` + +What would look like this in a shell: + +```sh +--foobar foo --arg1 1 --arg2 'a string' --foobar2 bar +``` diff --git a/coalpit.cabal b/coalpit.cabal index 0578897..91e175a 100644 --- a/coalpit.cabal +++ b/coalpit.cabal @@ -13,16 +13,10 @@ maintainer: defanor@uberspace.net category: Console build-type: Simple extra-source-files: ChangeLog.md + , Example.hs + , README.md cabal-version: >=1.10 -executable coalpit-example - main-is: Example.hs - -- other-modules: - other-extensions: TemplateHaskell - build-depends: base >=4.9 && <4.10 - , megaparsec >= 6.2.0 - default-language: Haskell2010 - library exposed-modules: Coalpit other-extensions: TemplateHaskell, FlexibleInstances @@ -36,7 +30,6 @@ test-suite test-coalpit main-is: Test.hs build-depends: base >= 4.9 && < 5 , megaparsec >= 6.2 && < 7 - , QuickCheck >= 2.10 && < 3 , generic-random >= 1 && < 2 , tasty >= 0.12 && < 1 , tasty-quickcheck >= 0.9 && < 1 -- cgit v1.2.3