Created
October 31, 2022 02:42
-
-
Save pete-murphy/2d852d33feba93fbab446135b54e9096 to your computer and use it in GitHub Desktop.
TIL (WIP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Prelude | |
import ArgParse.Basic (ArgParser) | |
import ArgParse.Basic as ArgParse | |
import Data.Array as Array | |
import Data.Either (Either(..)) | |
import Data.Maybe (Maybe(..)) | |
import Data.String (Pattern(..)) | |
import Data.String as String | |
import Debug as Debug | |
import Effect (Effect) | |
import Effect.Aff as Aff | |
import Effect.Class (liftEffect) | |
import Effect.Class.Console as Console | |
import Node.Process as Process | |
data Command | |
= Build | |
| Sync | |
| Edit | |
( Maybe | |
{ title :: String | |
, mediaFilePaths :: Array String | |
} | |
) | |
parser :: ArgParser Command | |
parser = ArgParse.choose "command" | |
[ ArgParse.command [ "build" ] | |
"Build all documents to HTML files for distribution" | |
do | |
Build <$ ArgParse.fromRecord {} | |
<* ArgParse.flagHelp | |
, ArgParse.command [ "sync" ] | |
"Ensure the local environment has all known changes" | |
do | |
Sync <$ ArgParse.fromRecord {} | |
<* ArgParse.flagHelp | |
, ArgParse.command [ "edit" ] | |
"Edit and publish a new document" | |
do | |
Edit | |
<$> ArgParse.optional | |
( ArgParse.fromRecord | |
{ title | |
, mediaFilePaths | |
} | |
) | |
<* ArgParse.flagHelp | |
] | |
where | |
title = do | |
let parts = ArgParse.unfolded (ArgParse.anyNotFlag "TITLE" "Title") | |
String.joinWith " " <$> parts | |
mediaFilePaths = do | |
let | |
filePaths = ArgParse.argument [ "--files", "-f" ] "Paths to media files" | |
# ArgParse.separated "FILE" (Pattern ",") | |
filePaths | |
main :: Effect Unit | |
main = Aff.launchAff_ do | |
args <- Array.drop 2 <$> liftEffect Process.argv | |
let | |
parsedCommand = | |
ArgParse.parseArgs | |
"til" | |
"Publish short articles from the command line." | |
parser | |
args | |
case parsedCommand of | |
Left error -> do | |
Console.log (ArgParse.printArgError error) | |
liftEffect do | |
case error of | |
ArgParse.ArgError _ ArgParse.ShowHelp -> do | |
Process.exit 0 | |
ArgParse.ArgError _ (ArgParse.ShowInfo _) -> do | |
Process.exit 0 | |
_ -> do | |
Process.exit 1 | |
Right command -> | |
case command of | |
Build -> do | |
pure unit | |
Sync -> do | |
pure unit | |
Edit Nothing -> do | |
pure unit | |
Edit (Just x) -> do | |
Debug.traceM x | |
pure unit | |
pure unit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment